7#include "BookmarkManager.h"
8#include "BookmarkManager_p.h"
9#include "GeoDataParser.h"
10#include "GeoDataContainer.h"
11#include "GeoDataDocument.h"
12#include "GeoDataFolder.h"
13#include "GeoDataPlacemark.h"
14#include "GeoDataTreeModel.h"
15#include "GeoDataDocumentWriter.h"
16#include "GeoDataIconStyle.h"
17#include "KmlElementDictionary.h"
18#include "MarbleDebug.h"
19#include "MarbleDirs.h"
20#include "StyleBuilder.h"
26BookmarkManagerPrivate::BookmarkManagerPrivate( GeoDataTreeModel *treeModel ) :
27 m_treeModel( treeModel ),
28 m_bookmarkDocument( nullptr ),
29 m_bookmarkFileRelativePath(
"bookmarks/bookmarks.kml" ),
30 m_styleBuilder(nullptr)
32 resetBookmarkDocument();
35BookmarkManagerPrivate::~BookmarkManagerPrivate()
37 Q_ASSERT( m_bookmarkDocument &&
"BookmarkManagerPrivate::m_bookmarkDocument is 0. Please report a Marble bug at https://bugs.kde.org" );
38 if ( m_bookmarkDocument )
40 m_treeModel->removeDocument( m_bookmarkDocument );
42 delete m_bookmarkDocument;
45void BookmarkManagerPrivate::resetBookmarkDocument()
47 if ( m_bookmarkDocument ) {
48 m_treeModel->removeDocument( m_bookmarkDocument );
49 delete m_bookmarkDocument;
52 GeoDataFolder* folder =
new GeoDataFolder;
55 m_bookmarkDocument =
new GeoDataDocument;
56 m_bookmarkDocument->setDocumentRole( BookmarkDocument );
57 m_bookmarkDocument->setName(
QObject::tr(
"Bookmarks") );
58 m_bookmarkDocument->append( folder );
59 m_treeModel->addDocument( m_bookmarkDocument );
62void BookmarkManagerPrivate::setVisualCategory( GeoDataContainer *container ) {
63 for( GeoDataFolder* folder: container->folderList() ) {
64 setVisualCategory( folder );
66 for( GeoDataPlacemark* placemark: container->placemarkList() ) {
67 placemark->setVisualCategory(GeoDataPlacemark::Bookmark);
68 placemark->setZoomLevel( 1 );
73BookmarkManager::BookmarkManager( GeoDataTreeModel *treeModel,
QObject *parent ) :
75 d( new BookmarkManagerPrivate( treeModel ) )
79BookmarkManager::~BookmarkManager()
84QString BookmarkManager::bookmarkFile()
const
86 return MarbleDirs::path( d->m_bookmarkFileRelativePath );
89bool BookmarkManager::loadFile(
const QString &relativeFilePath )
91 d->m_bookmarkFileRelativePath = relativeFilePath;
92 QString absoluteFilePath = bookmarkFile();
94 mDebug() <<
"Loading Bookmark File:" << absoluteFilePath;
99 if ( relativeFilePath.
isNull() )
103 bool recover =
false;
105 mDebug() <<
"Could not parse file" << absoluteFilePath;
106 mDebug() <<
"This could be caused by a previous broken bookmark file. Trying to recover.";
112 d->m_treeModel->removeDocument( d->m_bookmarkDocument );
113 delete d->m_bookmarkDocument;
114 d->m_bookmarkDocument = document;
117 d->resetBookmarkDocument();
118 updateBookmarkFile();
120 Q_ASSERT( d->m_bookmarkDocument &&
"d->m_bookmarkDocument is 0 but must not be. Please report a bug at https://bugs.kde.org" );
121 d->m_treeModel->addDocument( d->m_bookmarkDocument );
123 ensureDefaultFolder();
125 emit bookmarksChanged();
137 if (d->m_styleBuilder && bookmark->
style()->iconStyle().iconPath().isEmpty()) {
138 StyleParameters style;
139 style.placemark = bookmark;
142 d->m_treeModel->addFeature( container, bookmark );
144 updateBookmarkFile();
149 d->m_treeModel->updateFeature( bookmark );
152void BookmarkManager::removeBookmark( GeoDataPlacemark *bookmark )
154 d->m_treeModel->removeFeature( bookmark );
156 updateBookmarkFile();
168 if ( placemark->coordinate() == coordinate )
177 return d->m_bookmarkDocument;
180const GeoDataDocument * BookmarkManager::document()
const
182 return d->m_bookmarkDocument;
185bool BookmarkManager::showBookmarks()
const
187 return d->m_bookmarkDocument->
isVisible();
190void BookmarkManager::setShowBookmarks(
bool visible )
192 d->m_bookmarkDocument->setVisible( visible );
193 d->m_treeModel->updateFeature( d->m_bookmarkDocument );
198 return d->m_bookmarkDocument->folderList();
205 mDebug() <<
"Folder with empty name is not acceptable, please give it another name" ;
214 for ( ; i != end; ++i ) {
215 if ( name == ( *i )->name() ) {
216 mDebug() <<
"Folder with same name already exist, please give it another name";
222 bookmarkFolder->
setName( name );
224 d->m_treeModel->addFeature( container, bookmarkFolder );
225 updateBookmarkFile();
227 return bookmarkFolder;
233 d->m_treeModel->updateFeature( folder );
236void BookmarkManager::removeBookmarkFolder( GeoDataFolder *folder )
238 d->m_treeModel->removeFeature( folder );
242void BookmarkManager::ensureDefaultFolder()
244 if ( d->m_bookmarkDocument->size() == 0 ) {
245 addNewBookmarkFolder( d->m_bookmarkDocument, tr(
"Default") );
249void BookmarkManager::removeAllBookmarks()
251 d->resetBookmarkDocument();
252 updateBookmarkFile();
255void BookmarkManager::setStyleBuilder(
const StyleBuilder *styleBuilder)
257 d->m_styleBuilder = styleBuilder;
260bool BookmarkManager::updateBookmarkFile()
262 const QString absoluteLocalFilePath = MarbleDirs::localPath() +
QLatin1Char(
'/') + d->m_bookmarkFileRelativePath;
264 if ( ! d->m_bookmarkFileRelativePath.isNull() ) {
265 QFile file( absoluteLocalFilePath );
266 if ( !file.exists() ) {
268 QFileInfo fileInfo( absoluteLocalFilePath );
269 QString directoryPath = fileInfo.path();
272 QDir directory( MarbleDirs::localPath() );
273 directory.mkpath( directoryPath );
276 if (!GeoDataDocumentWriter::write(absoluteLocalFilePath, *d->m_bookmarkDocument)) {
277 mDebug() <<
"Could not write the bookmarks file" << absoluteLocalFilePath;
281 emit bookmarksChanged();
288GeoDataDocument* BookmarkManager::openFile(
const QString &fileName )
290 GeoDataParser parser( GeoData_KML );
291 QFile file( fileName );
293 if ( !file.exists() ) {
298 mDebug() <<
"Could not open/parse file" << fileName;
302 GeoDataDocument *result =
dynamic_cast<GeoDataDocument*
>( parser.releaseDocument() );
307 result->setDocumentRole( BookmarkDocument );
308 for( GeoDataFolder* folder: result->folderList() ) {
309 BookmarkManagerPrivate::setVisualCategory( folder );
317#include "moc_BookmarkManager.cpp"
A base class that can hold GeoDataFeatures.
QVector< GeoDataFolder * > folderList() const
A convenience function that returns all folders in this container.
QVector< GeoDataPlacemark * > placemarkList() const
A convenience function that returns all placemarks in this container.
A 3d point representation.
QString toString() const
return a string representation of the coordinate this is a convenience function which uses the defaul...
A container for Features, Styles and in the future Schemas.
void setStyle(const QSharedPointer< GeoDataStyle > &style)
Sets the style of the placemark.
QString name() const
The name of the feature.
void setZoomLevel(int index)
Sets the popularity index of the placemark.
QSharedPointer< const GeoDataStyle > style() const
Return the style assigned to the placemark, or a default style if none has been set.
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
void setVisualCategory(GeoDataVisualCategory index)
Sets the symbol category of the placemark.
GeoDataCoordinates coordinate(const QDateTime &dateTime=QDateTime(), bool *iconAtCoordinates=nullptr) const
Return the coordinates of the placemark at time dateTime as a GeoDataCoordinates.
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
QString trimmed() const const