Marble

CloudRouteModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2013 Utku Aydın <utkuaydin34@gmail.com>
4//
5
6#include "CloudRouteModel.h"
7
8#include "RouteItem.h"
9
10#include "MarbleDirs.h"
11
12#include <QIcon>
13#include <QList>
14#include <QNetworkAccessManager>
15#include <QNetworkReply>
16#include <QNetworkRequest>
17#include <QSet>
18#include <QUrl>
19
20namespace Marble
21{
22
23class Q_DECL_HIDDEN CloudRouteModel::Private
24{
25public:
26 Private();
27
28 QList<RouteItem> m_items;
29 QString m_cacheDir;
30 QPersistentModelIndex m_downloading;
31 qint64 m_totalSize;
32 qint64 m_downloadedSize;
33
34 QNetworkAccessManager m_network;
35 QMap<QNetworkReply *, int> m_previewQueue;
36 QSet<QString> m_requestedPreviews;
37};
38
39CloudRouteModel::Private::Private()
40 : m_totalSize(-1)
41 , m_downloadedSize(0)
42{
43 m_cacheDir = MarbleDirs::localPath() + QLatin1StringView("/cloudsync/cache/routes/");
44}
45
46CloudRouteModel::CloudRouteModel(QObject *parent)
47 : QAbstractListModel(parent)
48 , d(new Private())
49{
50 connect(&(d->m_network), &QNetworkAccessManager::finished, this, &CloudRouteModel::setPreview);
51}
52
53CloudRouteModel::~CloudRouteModel() = default;
54
55QVariant CloudRouteModel::data(const QModelIndex &index, int role) const
56{
57 Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
58 switch (role) {
60 return preview(index);
61 case Timestamp:
62 return d->m_items.at(index.row()).identifier();
63 case Name:
64 return d->m_items.at(index.row()).name();
65 case PreviewUrl:
66 return d->m_items.at(index.row()).previewUrl();
67 case Distance:
68 return d->m_items.at(index.row()).distance();
69 case Duration:
70 return d->m_items.at(index.row()).duration();
71 case IsCached:
72 return isCached(index);
73 case IsDownloading:
74 return isDownloading(index);
75 case IsOnCloud:
76 return d->m_items.at(index.row()).onCloud();
77 default:
78 return {};
79 }
80}
81
82int CloudRouteModel::rowCount(const QModelIndex &parent) const
83{
84 return parent.isValid() ? 0 : d->m_items.count();
85}
86
87QHash<int, QByteArray> CloudRouteModel::roleNames() const
88{
90 roles[Name] = "name";
91 roles[Timestamp] = "identifier";
92 roles[PreviewUrl] = "previewUrl";
93 roles[Distance] = "distance";
94 roles[Duration] = "duration";
95 roles[IsCached] = "isCached";
96 roles[IsDownloading] = "isDownloading";
97 roles[IsOnCloud] = "isOnCloud";
98 return roles;
99}
100
101void CloudRouteModel::setItems(const QList<RouteItem> &items)
102{
103 beginResetModel();
104 d->m_items = items;
105 d->m_previewQueue.clear();
106 d->m_requestedPreviews.clear();
107 endResetModel();
108}
109
110bool CloudRouteModel::isCached(const QModelIndex &index) const
111{
112 return QFileInfo::exists(d->m_cacheDir + index.data(Timestamp).toString() + QLatin1StringView(".kml"));
113}
114
115QPersistentModelIndex CloudRouteModel::downloadingItem() const
116{
117 return d->m_downloading;
118}
119
120void CloudRouteModel::setDownloadingItem(const QPersistentModelIndex &index)
121{
122 d->m_downloading = index;
123}
124
125bool CloudRouteModel::isDownloading(const QModelIndex &index) const
126{
127 return d->m_downloading == index;
128}
129
130qint64 CloudRouteModel::totalSize() const
131{
132 return d->m_totalSize;
133}
134
135qint64 CloudRouteModel::downloadedSize() const
136{
137 return d->m_downloadedSize;
138}
139
140QIcon CloudRouteModel::preview(const QModelIndex &index) const
141{
142 QString timestamp = d->m_items.at(index.row()).identifier();
143 if (d->m_items.at(index.row()).preview().isNull() && !d->m_requestedPreviews.contains(timestamp)) {
144 QUrl url(d->m_items.at(index.row()).previewUrl());
145 QNetworkRequest request(url);
146 QNetworkReply *reply = d->m_network.get(request);
147 d->m_previewQueue.insert(reply, index.row());
148 d->m_requestedPreviews.insert(timestamp);
149 }
150
151 return d->m_items.at(index.row()).preview();
152}
153
154void CloudRouteModel::setPreview(QNetworkReply *reply)
155{
156 int position = d->m_previewQueue.take(reply);
157
158 if (position >= d->m_items.count()) {
159 return;
160 }
161
162 RouteItem &route = d->m_items[position];
164 route.setPreview(icon);
165 d->m_requestedPreviews.remove(route.identifier());
166}
167
168void CloudRouteModel::updateProgress(qint64 currentSize, qint64 totalSize)
169{
170 d->m_totalSize = totalSize;
171 d->m_downloadedSize = currentSize;
172 dataChanged(d->m_downloading, d->m_downloading);
173 if (currentSize == totalSize) {
174 d->m_downloading = QPersistentModelIndex();
175 d->m_totalSize = -1;
176 d->m_downloadedSize = 0;
177 }
178}
179
180}
181
182#include "moc_CloudRouteModel.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
virtual QHash< int, QByteArray > roleNames() const const
bool exists() const const
QImage fromData(QByteArrayView data, const char *format)
QByteArray readAll()
void clear()
QVariant data(int role) const const
bool isValid() const const
int row() const const
void finished(QNetworkReply *reply)
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
const QChar at(qsizetype position) const const
DecorationRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.