7#include "BookmarkManager.h"
8#include "BookmarkManager_p.h"
9#include "GeoDataContainer.h"
10#include "GeoDataDocument.h"
11#include "GeoDataDocumentWriter.h"
12#include "GeoDataFolder.h"
13#include "GeoDataIconStyle.h"
14#include "GeoDataParser.h"
15#include "GeoDataPlacemark.h"
16#include "GeoDataTreeModel.h"
17#include "MarbleDebug.h"
18#include "MarbleDirs.h"
19#include "StyleBuilder.h"
25BookmarkManagerPrivate::BookmarkManagerPrivate(GeoDataTreeModel *treeModel)
26 : m_treeModel(treeModel)
27 , m_bookmarkDocument(nullptr)
28 , m_bookmarkFileRelativePath(QStringLiteral(
"bookmarks/bookmarks.kml"))
29 , m_styleBuilder(nullptr)
31 resetBookmarkDocument();
34BookmarkManagerPrivate::~BookmarkManagerPrivate()
36 Q_ASSERT(m_bookmarkDocument &&
"BookmarkManagerPrivate::m_bookmarkDocument is 0. Please report a Marble bug at https://bugs.kde.org");
37 if (m_bookmarkDocument) {
38 m_treeModel->removeDocument(m_bookmarkDocument);
40 delete m_bookmarkDocument;
43void BookmarkManagerPrivate::resetBookmarkDocument()
45 if (m_bookmarkDocument) {
46 m_treeModel->removeDocument(m_bookmarkDocument);
47 delete m_bookmarkDocument;
50 auto folder =
new GeoDataFolder;
53 m_bookmarkDocument =
new GeoDataDocument;
54 m_bookmarkDocument->setDocumentRole(BookmarkDocument);
55 m_bookmarkDocument->setName(
QObject::tr(
"Bookmarks"));
56 m_bookmarkDocument->append(folder);
57 m_treeModel->addDocument(m_bookmarkDocument);
60void BookmarkManagerPrivate::setVisualCategory(GeoDataContainer *container)
62 for (GeoDataFolder *folder : container->folderList()) {
63 setVisualCategory(folder);
65 for (GeoDataPlacemark *placemark : container->placemarkList()) {
66 placemark->setVisualCategory(GeoDataPlacemark::Bookmark);
67 placemark->setZoomLevel(1);
71BookmarkManager::BookmarkManager(GeoDataTreeModel *treeModel,
QObject *parent)
73 , d(new BookmarkManagerPrivate(treeModel))
77BookmarkManager::~BookmarkManager()
82QString BookmarkManager::bookmarkFile()
const
84 return MarbleDirs::path(d->m_bookmarkFileRelativePath);
87bool BookmarkManager::loadFile(
const QString &relativeFilePath)
89 d->m_bookmarkFileRelativePath = relativeFilePath;
90 QString absoluteFilePath = bookmarkFile();
92 mDebug() <<
"Loading Bookmark File:" << absoluteFilePath;
97 if (relativeFilePath.
isNull())
101 bool recover =
false;
103 mDebug() <<
"Could not parse file" << absoluteFilePath;
104 mDebug() <<
"This could be caused by a previous broken bookmark file. Trying to recover.";
110 d->m_treeModel->removeDocument(d->m_bookmarkDocument);
111 delete d->m_bookmarkDocument;
112 d->m_bookmarkDocument = document;
115 d->resetBookmarkDocument();
116 updateBookmarkFile();
118 Q_ASSERT(d->m_bookmarkDocument &&
"d->m_bookmarkDocument is 0 but must not be. Please report a bug at https://bugs.kde.org");
119 d->m_treeModel->addDocument(d->m_bookmarkDocument);
121 ensureDefaultFolder();
123 Q_EMIT bookmarksChanged();
130 bookmark->setVisualCategory(GeoDataPlacemark::Bookmark);
131 bookmark->setZoomLevel(1);
132 if (bookmark->name().isEmpty()) {
133 bookmark->setName(bookmark->coordinate().toString(GeoDataCoordinates::Decimal).trimmed());
135 if (d->m_styleBuilder && bookmark->style()->iconStyle().iconPath().isEmpty()) {
136 StyleParameters style;
137 style.placemark = bookmark;
140 d->m_treeModel->addFeature(container, bookmark);
142 updateBookmarkFile();
147 d->m_treeModel->updateFeature(bookmark);
150void BookmarkManager::removeBookmark(GeoDataPlacemark *bookmark)
152 d->m_treeModel->removeFeature(bookmark);
154 updateBookmarkFile();
166 if (placemark->coordinate() == coordinate)
175 return d->m_bookmarkDocument;
178const GeoDataDocument *BookmarkManager::document()
const
180 return d->m_bookmarkDocument;
183bool BookmarkManager::showBookmarks()
const
185 return d->m_bookmarkDocument->
isVisible();
188void BookmarkManager::setShowBookmarks(
bool visible)
190 d->m_bookmarkDocument->setVisible(visible);
191 d->m_treeModel->updateFeature(d->m_bookmarkDocument);
196 return d->m_bookmarkDocument->folderList();
203 mDebug() <<
"Folder with empty name is not acceptable, please give it another name";
212 for (; i != end; ++i) {
213 if (name == (*i)->name()) {
214 mDebug() <<
"Folder with same name already exist, please give it another name";
220 bookmarkFolder->setName(name);
222 d->m_treeModel->addFeature(container, bookmarkFolder);
223 updateBookmarkFile();
225 return bookmarkFolder;
231 d->m_treeModel->updateFeature(folder);
234void BookmarkManager::removeBookmarkFolder(GeoDataFolder *folder)
236 d->m_treeModel->removeFeature(folder);
240void BookmarkManager::ensureDefaultFolder()
242 if (d->m_bookmarkDocument->size() == 0) {
243 addNewBookmarkFolder(d->m_bookmarkDocument, tr(
"Default"));
247void BookmarkManager::removeAllBookmarks()
249 d->resetBookmarkDocument();
250 updateBookmarkFile();
253void BookmarkManager::setStyleBuilder(
const StyleBuilder *styleBuilder)
255 d->m_styleBuilder = styleBuilder;
258bool BookmarkManager::updateBookmarkFile()
260 const QString absoluteLocalFilePath = MarbleDirs::localPath() +
QLatin1Char(
'/') + d->m_bookmarkFileRelativePath;
262 if (!d->m_bookmarkFileRelativePath.isNull()) {
263 QFile file(absoluteLocalFilePath);
264 if (!file.exists()) {
266 QFileInfo fileInfo(absoluteLocalFilePath);
267 QString directoryPath = fileInfo.path();
270 QDir directory(MarbleDirs::localPath());
271 directory.mkpath(directoryPath);
274 if (!GeoDataDocumentWriter::write(absoluteLocalFilePath, *d->m_bookmarkDocument)) {
275 mDebug() <<
"Could not write the bookmarks file" << absoluteLocalFilePath;
279 Q_EMIT bookmarksChanged();
286GeoDataDocument *BookmarkManager::openFile(
const QString &fileName)
288 GeoDataParser parser(GeoData_KML);
289 QFile file(fileName);
291 if (!file.exists()) {
296 mDebug() <<
"Could not open/parse file" << fileName;
300 auto result =
dynamic_cast<GeoDataDocument *
>(parser.releaseDocument());
305 result->setDocumentRole(BookmarkDocument);
306 for (GeoDataFolder *folder : result->folderList()) {
307 BookmarkManagerPrivate::setVisualCategory(folder);
315#include "moc_BookmarkManager.cpp"
A base class that can hold GeoDataFeatures.
QList< GeoDataFolder * > folderList() const
A convenience function that returns all folders in this container.
QList< GeoDataPlacemark * > placemarkList() const
A convenience function that returns all placemarks in this container.
A 3d point representation.
A container for Features, Styles and in the future Schemas.
void setStyle(const QSharedPointer< GeoDataStyle > &style)
Sets the style of the placemark.
void setName(const QString &value)
Set a new name for this feature.
bool isVisible() const
Return whether this feature is visible or not.
A container that is used to arrange other GeoDataFeatures.
a class representing a point of interest on the map
an addressable style group
Binds a QML item to a specific geodetic location in screen coordinates.
const_iterator constBegin() const const
const_iterator constEnd() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isEmpty() const const
bool isNull() const const