• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
BookmarkManager.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Gaurav Gupta <1989.gaurav@googlemail.com>
9 // Copyright 2012 Thibaut Gridel <tgridel@free.fr>
10 //
11 
12 #include "BookmarkManager.h"
13 #include "BookmarkManager_p.h"
14 #include "GeoDataParser.h"
15 #include "GeoDataContainer.h"
16 #include "GeoDataDocument.h"
17 #include "GeoDataFolder.h"
18 #include "GeoDataPlacemark.h"
19 #include "GeoDataTreeModel.h"
20 #include "GeoWriter.h"
21 #include "KmlElementDictionary.h"
22 #include "MarbleDebug.h"
23 #include "MarbleDirs.h"
24 #include <QFile>
25 
26 namespace Marble
27 {
28 
29 BookmarkManagerPrivate::BookmarkManagerPrivate( GeoDataTreeModel *treeModel ) :
30  m_treeModel( treeModel ),
31  m_bookmarkDocument( 0 ),
32  m_bookmarkFileRelativePath( "bookmarks/bookmarks.kml" )
33 {
34  resetBookmarkDocument();
35 }
36 
37 BookmarkManagerPrivate::~BookmarkManagerPrivate()
38 {
39  Q_ASSERT( m_bookmarkDocument && "BookmarkManagerPrivate::m_bookmarkDocument is 0. Please report a Marble bug at http://bugs.kde.org" );
40  if ( m_bookmarkDocument )
41  {
42  m_treeModel->removeDocument( m_bookmarkDocument );
43  }
44  delete m_bookmarkDocument;
45 }
46 
47 void BookmarkManagerPrivate::resetBookmarkDocument()
48 {
49  if ( m_bookmarkDocument ) {
50  m_treeModel->removeDocument( m_bookmarkDocument );
51  delete m_bookmarkDocument;
52  }
53 
54  GeoDataFolder* folder = new GeoDataFolder;
55  folder->setName( QObject::tr( "Default" ) );
56 
57  m_bookmarkDocument = new GeoDataDocument;
58  m_bookmarkDocument->setDocumentRole( BookmarkDocument );
59  m_bookmarkDocument->setName( QObject::tr("Bookmarks") );
60  m_bookmarkDocument->append( folder );
61  m_treeModel->addDocument( m_bookmarkDocument );
62 }
63 
64 void BookmarkManagerPrivate::setVisualCategory( GeoDataContainer *container ) {
65  foreach( GeoDataFolder* folder, container->folderList() ) {
66  setVisualCategory( folder );
67  }
68  foreach( GeoDataPlacemark* placemark, container->placemarkList() ) {
69  placemark->setVisualCategory( GeoDataFeature::Bookmark );
70  placemark->setZoomLevel( 1 );
71  }
72 
73 }
74 
75 BookmarkManager::BookmarkManager( GeoDataTreeModel *treeModel, QObject *parent ) :
76  QObject( parent ),
77  d( new BookmarkManagerPrivate( treeModel ) )
78 {
79 }
80 
81 BookmarkManager::~BookmarkManager()
82 {
83  delete d;
84 }
85 
86 QString BookmarkManager::bookmarkFile() const
87 {
88  return MarbleDirs::path( d->m_bookmarkFileRelativePath );
89 }
90 
91 bool BookmarkManager::loadFile( const QString &relativeFilePath )
92 {
93  d->m_bookmarkFileRelativePath = relativeFilePath;
94  QString absoluteFilePath = bookmarkFile();
95 
96  mDebug() << Q_FUNC_INFO << "Loading Bookmark File:" << absoluteFilePath;
97 
98  if (absoluteFilePath.isEmpty())
99  return false;
100 
101  if ( relativeFilePath.isNull() )
102  return false;
103 
104  GeoDataDocument *document = openFile( absoluteFilePath );
105  bool recover = false;
106  if ( !document ) {
107  mDebug() << "Could not parse file" << absoluteFilePath;
108  mDebug() << "This could be caused by a previous broken bookmark file. Trying to recover.";
110  recover = true;
111  // return false;
112  }
113 
114  d->m_treeModel->removeDocument( d->m_bookmarkDocument );
115  delete d->m_bookmarkDocument;
116  d->m_bookmarkDocument = document;
117 
118  if ( recover ) {
119  d->resetBookmarkDocument();
120  updateBookmarkFile();
121  } else {
122  Q_ASSERT( d->m_bookmarkDocument && "d->m_bookmarkDocument is 0 but must not be. Please report a bug at http://bugs.kde.org" );
123  d->m_treeModel->addDocument( d->m_bookmarkDocument );
124  }
125  ensureDefaultFolder();
126 
127  emit bookmarksChanged();
128  return true;
129 }
130 
131 
132 void BookmarkManager::addBookmark( GeoDataContainer *container, const GeoDataPlacemark &placemark )
133 {
134  GeoDataPlacemark *bookmark = new GeoDataPlacemark( placemark );
135  bookmark->setVisualCategory( GeoDataDocument::Bookmark );
136  bookmark->setZoomLevel( 1 );
137  d->m_treeModel->addFeature( container, bookmark );
138 
139  updateBookmarkFile();
140 }
141 
142 void BookmarkManager::updateBookmark( GeoDataPlacemark *bookmark )
143 {
144  d->m_treeModel->updateFeature( bookmark );
145 }
146 
147 void BookmarkManager::removeBookmark( GeoDataPlacemark *bookmark )
148 {
149  d->m_treeModel->removeFeature( bookmark );
150  delete bookmark;
151  updateBookmarkFile();
152 }
153 
154 GeoDataDocument * BookmarkManager::document()
155 {
156  return d->m_bookmarkDocument;
157 }
158 
159 const GeoDataDocument * BookmarkManager::document() const
160 {
161  return d->m_bookmarkDocument;
162 }
163 
164 bool BookmarkManager::showBookmarks() const
165 {
166  return d->m_bookmarkDocument->isVisible();
167 }
168 
169 void BookmarkManager::setShowBookmarks( bool visible )
170 {
171  d->m_bookmarkDocument->setVisible( visible );
172  d->m_treeModel->updateFeature( d->m_bookmarkDocument );
173 }
174 
175 QVector<GeoDataFolder*> BookmarkManager::folders() const
176 {
177  return d->m_bookmarkDocument->folderList();
178 }
179 
180 void BookmarkManager::addNewBookmarkFolder( GeoDataContainer *container, const QString &name )
181 {
182  //If name is empty string
183  if ( name.isEmpty() ) {
184  mDebug() << "Folder with empty name is not acceptable, please give it another name" ;
185  return;
186  }
187 
188  //If folder with same name already exist
189  QVector<GeoDataFolder*> folderList = container->folderList();
190 
191  QVector<GeoDataFolder*>::const_iterator i = folderList.constBegin();
192  QVector<GeoDataFolder*>::const_iterator end = folderList.constEnd();
193  for ( ; i != end; ++i ) {
194  if ( name == ( *i )->name() ) {
195  mDebug() << "Folder with same name already exist, please give it another name";
196  return;
197  }
198  }
199 
200  GeoDataFolder *bookmarkFolder = new GeoDataFolder();
201  bookmarkFolder->setName( name );
202 
203  d->m_treeModel->addFeature( container, bookmarkFolder );
204  updateBookmarkFile();
205 }
206 
207 void BookmarkManager::renameBookmarkFolder( GeoDataFolder *folder, const QString &name )
208 {
209  folder->setName( name );
210  d->m_treeModel->updateFeature( folder );
211 }
212 
213 void BookmarkManager::removeBookmarkFolder( GeoDataFolder *folder )
214 {
215  d->m_treeModel->removeFeature( folder );
216  delete folder;
217 }
218 
219 void BookmarkManager::ensureDefaultFolder()
220 {
221  if ( d->m_bookmarkDocument->size() == 0 ) {
222  addNewBookmarkFolder( d->m_bookmarkDocument, "Default" );
223  }
224 }
225 
226 void BookmarkManager::removeAllBookmarks()
227 {
228  d->resetBookmarkDocument();
229  updateBookmarkFile();
230 }
231 
232 bool BookmarkManager::updateBookmarkFile()
233 {
234  QString absoluteLocalFilePath = MarbleDirs::localPath() + '/' + d->m_bookmarkFileRelativePath ;
235 
236  if ( ! d->m_bookmarkFileRelativePath.isNull() ) {
237  GeoWriter writer;
238  writer.setDocumentType( kml::kmlTag_nameSpaceOgc22 );
239 
240  QFile file( absoluteLocalFilePath );
241 
242  if ( !file.exists() ) {
243  // Extracting directory of file : for bookmarks it will be MarbleDirs::localPath()+/bookmarks/
244  QFileInfo fileInfo( absoluteLocalFilePath );
245  QString directoryPath = fileInfo.path();
246 
247  //Creating all directories, which doesn't exist
248  QDir directory( MarbleDirs::localPath() );
249  directory.mkpath( directoryPath );
250  }
251 
252  file.open( QIODevice::WriteOnly );
253 
254  if ( !writer.write( &file, d->m_bookmarkDocument ) ) {
255  mDebug() << "Could not write the bookmarks file" << absoluteLocalFilePath;
256  file.close();
257  return false;
258  }
259  emit bookmarksChanged();
260  file.close();
261  return true;
262  }
263  return false;
264 }
265 
266 GeoDataDocument* BookmarkManager::openFile( const QString &fileName )
267 {
268  GeoDataParser parser( GeoData_KML );
269  QFile file( fileName );
270 
271  if ( !file.exists() ) {
272  return 0;
273  }
274 
275  if ( !file.open( QIODevice::ReadOnly ) || !parser.read( &file ) ) {
276  mDebug() << "Could not open/parse file" << fileName;
277  return 0;
278  }
279 
280  GeoDataDocument *result = dynamic_cast<GeoDataDocument*>( parser.releaseDocument() );
281  if ( !result ) {
282  return 0;
283  }
284 
285  result->setDocumentRole( BookmarkDocument );
286  foreach( GeoDataFolder* folder, result->folderList() ) {
287  BookmarkManagerPrivate::setVisualCategory( folder );
288  }
289 
290  return result;
291 }
292 
293 }
294 
295 #include "BookmarkManager.moc"
GeoDataDocument.h
Marble::BookmarkManagerPrivate::m_bookmarkFileRelativePath
QString m_bookmarkFileRelativePath
Definition: BookmarkManager_p.h:38
Marble::GeoDataTreeModel::addFeature
int addFeature(GeoDataContainer *parent, GeoDataFeature *feature, int row=-1)
Definition: GeoDataTreeModel.cpp:792
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:65
Marble::BookmarkManager::renameBookmarkFolder
void renameBookmarkFolder(GeoDataFolder *folder, const QString &name)
Definition: BookmarkManager.cpp:207
Marble::BookmarkManager::loadFile
bool loadFile(const QString &relativeFilePath)
load bookmark file as GeoDataDocument and return true if loaded successfully else false ...
Definition: BookmarkManager.cpp:91
Marble::GeoDataTreeModel
The representation of GeoData in a model This class represents all available data given by kml-data f...
Definition: GeoDataTreeModel.h:32
Marble::BookmarkManager::addBookmark
void addBookmark(GeoDataContainer *folder, const GeoDataPlacemark &bookmark)
add bookmark in a folder
Definition: BookmarkManager.cpp:132
Marble::GeoDataDocument::setDocumentRole
void setDocumentRole(DocumentRole role)
Definition: GeoDataDocument.cpp:86
Marble::MarbleDirs::path
static QString path(const QString &relativePath)
Definition: MarbleDirs.cpp:59
GeoDataContainer.h
Marble::GeoDataFeature::setVisualCategory
void setVisualCategory(GeoDataVisualCategory category)
Sets the symbol index of the placemark.
Definition: GeoDataFeature.cpp:770
Marble::BookmarkManager::updateBookmark
void updateBookmark(GeoDataPlacemark *bookmark)
Definition: BookmarkManager.cpp:142
QVector::constEnd
const_iterator constEnd() const
Marble::GeoDataTreeModel::removeFeature
bool removeFeature(GeoDataContainer *parent, int index)
Definition: GeoDataTreeModel.cpp:826
Marble::GeoDataFeature::isVisible
bool isVisible() const
Return whether this feature is visible or not.
Definition: GeoDataFeature.cpp:656
Marble::GeoDataContainer
A base class that can hold GeoDataFeatures.
Definition: GeoDataContainer.h:47
Marble::BookmarkManagerPrivate::m_bookmarkDocument
GeoDataDocument * m_bookmarkDocument
Definition: BookmarkManager_p.h:36
GeoDataParser.h
Marble::MarbleDirs::localPath
static QString localPath()
Definition: MarbleDirs.cpp:223
Marble::GeoDataFeature::Bookmark
Definition: GeoDataFeature.h:134
MarbleDebug.h
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
Marble::BookmarkDocument
Definition: GeoDataDocument.h:44
Marble::GeoDataTreeModel::addDocument
int addDocument(GeoDataDocument *document)
Definition: GeoDataTreeModel.cpp:821
QFile
BookmarkManager.h
Marble::BookmarkManager::bookmarksChanged
void bookmarksChanged()
One or more bookmarks were added or removed.
GeoWriter.h
QString::isNull
bool isNull() const
Marble::BookmarkManager::setShowBookmarks
void setShowBookmarks(bool visible)
Definition: BookmarkManager.cpp:169
Marble::GeoData_KML
Definition: GeoDataParser.h:36
BookmarkManager_p.h
Marble::BookmarkManagerPrivate::~BookmarkManagerPrivate
~BookmarkManagerPrivate()
Definition: BookmarkManager.cpp:37
Marble::BookmarkManagerPrivate::setVisualCategory
static void setVisualCategory(GeoDataContainer *container)
Definition: BookmarkManager.cpp:64
Marble::BookmarkManager::removeBookmark
void removeBookmark(GeoDataPlacemark *bookmark)
Definition: BookmarkManager.cpp:147
Marble::GeoDataFeature::setName
void setName(const QString &value)
Set a new name for this feature.
Definition: GeoDataFeature.cpp:549
Marble::BookmarkManager::removeAllBookmarks
void removeAllBookmarks()
remove all folders and bookmarks except default folder
Definition: BookmarkManager.cpp:226
KmlElementDictionary.h
Marble::GeoDataTreeModel::removeDocument
void removeDocument(int index)
Definition: GeoDataTreeModel.cpp:872
QObject
Marble::BookmarkManager::document
GeoDataDocument * document()
Definition: BookmarkManager.cpp:154
Marble::BookmarkManager::folders
QVector< GeoDataFolder * > folders() const
return Vector of folders
Definition: BookmarkManager.cpp:175
MarbleDirs.h
Marble::GeoDataContainer::folderList
QVector< GeoDataFolder * > folderList() const
A convenience function that returns all folders in this container.
Definition: GeoDataContainer.cpp:197
QString::isEmpty
bool isEmpty() const
Marble::GeoWriter
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition: GeoWriter.h:29
Marble::BookmarkManagerPrivate::m_treeModel
GeoDataTreeModel *const m_treeModel
Definition: BookmarkManager_p.h:34
Marble::GeoDataContainer::size
int size() const
size of the container
Definition: GeoDataContainer.cpp:286
QString
Marble::GeoDataTreeModel::updateFeature
void updateFeature(GeoDataFeature *feature)
Definition: GeoDataTreeModel.cpp:864
Marble::GeoDataFolder
Definition: GeoDataFolder.h:50
GeoDataPlacemark.h
GeoDataTreeModel.h
Marble::BookmarkManager::ensureDefaultFolder
void ensureDefaultFolder()
checks that there is at least one folder
Definition: BookmarkManager.cpp:219
Marble::GeoDataContainer::append
void append(GeoDataFeature *other)
add an element
Definition: GeoDataContainer.cpp:272
QFileInfo
Marble::kml::kmlTag_nameSpaceOgc22
const char * kmlTag_nameSpaceOgc22
Definition: KmlElementDictionary.cpp:34
Marble::GeoWriter::setDocumentType
void setDocumentType(const QString &documentType)
Set the current document type.
Definition: GeoWriter.cpp:79
Marble::BookmarkManager::BookmarkManager
BookmarkManager(GeoDataTreeModel *treeModel, QObject *parent=0)
Definition: BookmarkManager.cpp:75
Marble::BookmarkManagerPrivate::BookmarkManagerPrivate
BookmarkManagerPrivate(GeoDataTreeModel *treeModel)
Definition: BookmarkManager.cpp:29
Marble::GeoDataFeature::setVisible
void setVisible(bool value)
Set a new value for visibility.
Definition: GeoDataFeature.cpp:661
QDir
GeoDataFolder.h
QVector::constBegin
const_iterator constBegin() const
Marble::BookmarkManager::~BookmarkManager
~BookmarkManager()
Definition: BookmarkManager.cpp:81
QVector
Marble::BookmarkManagerPrivate
Definition: BookmarkManager_p.h:23
Marble::BookmarkManagerPrivate::resetBookmarkDocument
void resetBookmarkDocument()
Definition: BookmarkManager.cpp:47
Marble::BookmarkManager::bookmarkFile
QString bookmarkFile() const
return bookmark file path
Definition: BookmarkManager.cpp:86
Marble::GeoWriter::write
bool write(QIODevice *device, const GeoNode *feature)
The main API call to use the XML writer.
Definition: GeoWriter.cpp:28
Marble::GeoDataFeature::setZoomLevel
void setZoomLevel(int index)
Sets the popularity index of the placemark.
Definition: GeoDataFeature.cpp:802
Marble::BookmarkManager::removeBookmarkFolder
void removeBookmarkFolder(GeoDataFolder *folder)
Definition: BookmarkManager.cpp:213
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::BookmarkManager::showBookmarks
bool showBookmarks() const
Definition: BookmarkManager.cpp:164
Marble::BookmarkManager::addNewBookmarkFolder
void addNewBookmarkFolder(GeoDataContainer *folder, const QString &name)
add a folder
Definition: BookmarkManager.cpp:180
Marble::GeoDataContainer::placemarkList
QVector< GeoDataPlacemark * > placemarkList() const
A convenience function that returns all placemarks in this container.
Definition: GeoDataContainer.cpp:214
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal