6#include "RouteSyncManager.h"
8#include "CloudRouteModel.h"
9#include "CloudRoutesDialog.h"
10#include "CloudSyncManager.h"
11#include "GeoDataDocument.h"
12#include "GeoDataFolder.h"
13#include "GeoDataParser.h"
14#include "GeoDataPlacemark.h"
15#include "MarbleDebug.h"
16#include "MarbleDirs.h"
17#include "OwncloudSyncBackend.h"
19#include "RoutingManager.h"
33class Q_DECL_HIDDEN RouteSyncManager::Private
36 Private(CloudSyncManager *cloudSyncManager);
39 bool m_routeSyncEnabled;
40 CloudSyncManager *
const m_cloudSyncManager;
41 RoutingManager *m_routingManager =
nullptr;
42 CloudRouteModel *
const m_model;
45 OwncloudSyncBackend m_owncloudBackend;
49RouteSyncManager::Private::Private(CloudSyncManager *cloudSyncManager)
50 : m_routeSyncEnabled(false)
51 , m_cloudSyncManager(cloudSyncManager)
52 , m_model(new CloudRouteModel())
53 , m_owncloudBackend(cloudSyncManager)
58RouteSyncManager::Private::~Private()
63RouteSyncManager::RouteSyncManager(CloudSyncManager *cloudSyncManager)
64 : d(new Private(cloudSyncManager))
66 connect(&d->m_owncloudBackend, &OwncloudSyncBackend::routeUploadProgress,
this, &RouteSyncManager::updateUploadProgressbar);
67 connect(&d->m_owncloudBackend, &OwncloudSyncBackend::routeListDownloaded,
this, &RouteSyncManager::setRouteModelItems);
68 connect(&d->m_owncloudBackend, &OwncloudSyncBackend::routeListDownloadProgress,
this, &RouteSyncManager::routeListDownloadProgress);
69 connect(&d->m_owncloudBackend, &OwncloudSyncBackend::routeDownloadProgress, d->m_model, &CloudRouteModel::updateProgress);
70 connect(&d->m_owncloudBackend, &OwncloudSyncBackend::routeDownloaded,
this, &RouteSyncManager::prepareRouteList);
71 connect(&d->m_owncloudBackend, &OwncloudSyncBackend::routeDeleted,
this, &RouteSyncManager::prepareRouteList);
72 connect(&d->m_owncloudBackend, &OwncloudSyncBackend::removedFromCache,
this, &RouteSyncManager::prepareRouteList);
75RouteSyncManager::~RouteSyncManager()
80void RouteSyncManager::setRoutingManager(RoutingManager *routingManager)
82 d->m_routingManager = routingManager;
85bool RouteSyncManager::isRouteSyncEnabled()
const
87 return d->m_routeSyncEnabled && d->m_cloudSyncManager && d->m_cloudSyncManager->isSyncEnabled();
90void RouteSyncManager::setRouteSyncEnabled(
bool enabled)
92 if (d->m_routeSyncEnabled != enabled) {
93 d->m_routeSyncEnabled = enabled;
94 Q_EMIT routeSyncEnabledChanged(d->m_routeSyncEnabled);
98CloudRouteModel *RouteSyncManager::model()
103QString RouteSyncManager::generateTimestamp()
const
109QString RouteSyncManager::saveDisplayedToCache()
const
111 if (!d->m_routingManager) {
112 qWarning() <<
"RoutingManager instance not set in RouteSyncManager. Cannot save current route.";
116 d->m_cacheDir.mkpath(d->m_cacheDir.absolutePath());
118 const QString timestamp = generateTimestamp();
120 d->m_routingManager->saveRoute(filename);
124void RouteSyncManager::uploadRoute()
126 if (!d->m_cloudSyncManager->workOffline()) {
127 d->m_owncloudBackend.uploadRoute(saveDisplayedToCache());
135 for (
const QString &routeFilename : std::as_const(cachedRoutes)) {
136 QFile file(d->m_cacheDir.absolutePath() +
QLatin1Char(
'/') + routeFilename);
139 GeoDataParser parser(GeoData_KML);
140 if (!parser.read(&file)) {
147 GeoDocument *geoDoc = parser.releaseDocument();
148 auto container =
dynamic_cast<GeoDataDocument *
>(geoDoc);
149 if (container && !container->isEmpty()) {
150 GeoDataFolder *folder = container->folderList().at(0);
151 for (GeoDataPlacemark *placemark : folder->placemarkList()) {
156 routeName = routeName.
left(routeName.
length() - 3);
157 QString timestamp = routeFilename.
left(routeFilename.length() - 4);
161 QString previewPath = QStringLiteral(
"%0/preview/%1.jpg").
arg(d->m_cacheDir.absolutePath(), timestamp);
164 if (
QFile(previewPath).exists()) {
165 preview =
QIcon(previewPath);
169 QUrl previewUrl(QStringLiteral(
"file://%0").arg(previewPath));
172 item.setIdentifier(timestamp);
173 item.setName(routeName);
174 item.setDistance(distance);
175 item.setDistance(duration);
176 item.setPreview(preview);
177 item.setPreviewUrl(previewUrl);
178 item.setOnCloud(
false);
185void RouteSyncManager::uploadRoute(
const QString ×tamp)
187 if (!d->m_cloudSyncManager->workOffline()) {
188 d->m_owncloudBackend.uploadRoute(timestamp);
192void RouteSyncManager::prepareRouteList()
194 d->m_routeList.
clear();
197 for (
const RouteItem &item : std::as_const(cachedRoutes)) {
198 d->m_routeList.append(item);
201 if (!d->m_cloudSyncManager->workOffline()) {
202 d->m_owncloudBackend.downloadRouteList();
206 d->m_model->setItems(d->m_routeList);
210void RouteSyncManager::downloadRoute(
const QString ×tamp)
212 d->m_owncloudBackend.downloadRoute(timestamp);
215void RouteSyncManager::openRoute(
const QString ×tamp)
217 if (!d->m_routingManager) {
218 qWarning() <<
"RoutingManager instance not set in RouteSyncManager. Cannot open route " << timestamp;
222 d->m_routingManager->loadRoute(QStringLiteral(
"%0/%1.kml").arg(d->m_cacheDir.absolutePath()).arg(timestamp));
225void RouteSyncManager::deleteRoute(
const QString ×tamp)
227 d->m_owncloudBackend.deleteRoute(timestamp);
230void RouteSyncManager::removeRouteFromCache(
const QString ×tamp)
232 d->m_owncloudBackend.removeFromCache(d->m_cacheDir, timestamp);
235void RouteSyncManager::updateUploadProgressbar(qint64 sent, qint64 total)
237 Q_EMIT routeUploadProgress(sent, total);
245 if (d->m_routeList.count() > 0) {
247 for (
const RouteItem &item : routeList) {
248 cloudRoutes.
append(item.identifier());
251 for (
int position = 0; position < d->m_routeList.count(); position++) {
252 if (cloudRoutes.
contains(d->m_routeList.at(position).identifier())) {
253 d->m_routeList[position].setOnCloud(
true);
258 for (
const RouteItem &item : std::as_const(d->m_routeList)) {
259 cachedRoutes.
append(item.identifier());
262 for (
const RouteItem &item : routeList) {
263 if (!cachedRoutes.
contains(item.identifier())) {
264 d->m_routeList.append(item);
268 for (
const RouteItem &item : routeList) {
269 d->m_routeList.append(item);
273 d->m_model->setItems(d->m_routeList);
278#include "moc_RouteSyncManager.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
KOSM_EXPORT double distance(const std::vector< const OSM::Node * > &path, Coordinate coord)
qint64 currentMSecsSinceEpoch()
void append(QList< T > &&value)
QString arg(Args &&... args) const const
QString left(qsizetype n) const const
qsizetype length() const const
QString number(double n, char format, int precision)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)