File indexing completed on 2024-07-21 12:04:37

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Dennis Nienhüser <nienhueser@kde.org>
0004 // SPDX-FileCopyrightText: 2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0005 //
0006 
0007 #include "LocalOsmSearchPlugin.h"
0008 #include "LocalOsmSearchRunner.h"
0009 #include "MarbleDirs.h"
0010 
0011 #include <QDirIterator>
0012 
0013 namespace Marble
0014 {
0015 
0016 LocalOsmSearchPlugin::LocalOsmSearchPlugin( QObject *parent ) :
0017     SearchRunnerPlugin( parent ),
0018     m_databaseFiles()
0019 {
0020     setSupportedCelestialBodies(QStringList(QStringLiteral("earth")));
0021     setCanWorkOffline( true );
0022 
0023     QString const path = MarbleDirs::localPath() + QLatin1String("/maps/earth/placemarks/");
0024     QFileInfo pathInfo( path );
0025     if ( !pathInfo.exists() ) {
0026         QDir("/").mkpath( pathInfo.absolutePath() );
0027         pathInfo.refresh();
0028     }
0029     if ( pathInfo.exists() ) {
0030         m_watcher.addPath( path );
0031     }
0032     connect( &m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(updateDirectory(QString)) );
0033     connect( &m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFile(QString)) );
0034 
0035     updateDatabase();
0036 }
0037 
0038 QString LocalOsmSearchPlugin::name() const
0039 {
0040     return tr( "Local OSM Search" );
0041 }
0042 
0043 QString LocalOsmSearchPlugin::guiString() const
0044 {
0045     return tr( "Offline OpenStreetMap Search" );
0046 }
0047 
0048 QString LocalOsmSearchPlugin::nameId() const
0049 {
0050     return QStringLiteral("local-osm-search");
0051 }
0052 
0053 QString LocalOsmSearchPlugin::version() const
0054 {
0055     return QStringLiteral("1.0");
0056 }
0057 
0058 QString LocalOsmSearchPlugin::description() const
0059 {
0060     return tr( "Searches for addresses and points of interest in offline maps." );
0061 }
0062 
0063 QString LocalOsmSearchPlugin::copyrightYears() const
0064 {
0065     return QStringLiteral("2011");
0066 }
0067 
0068 QVector<PluginAuthor> LocalOsmSearchPlugin::pluginAuthors() const
0069 {
0070     return QVector<PluginAuthor>()
0071             << PluginAuthor(QStringLiteral("Dennis Nienhüser"), QStringLiteral("nienhueser@kde.org"));
0072 }
0073 
0074 SearchRunner* LocalOsmSearchPlugin::newRunner() const
0075 {
0076     return new LocalOsmSearchRunner( m_databaseFiles );
0077 }
0078 
0079 void LocalOsmSearchPlugin::addDatabaseDirectory( const QString &path )
0080 {
0081     QDir directory( path );
0082     QStringList const nameFilters = QStringList() << "*.sqlite";
0083     QStringList const files( directory.entryList( nameFilters, QDir::Files ) );
0084     for( const QString &file: files ) {
0085         m_databaseFiles << directory.filePath( file );
0086     }
0087 }
0088 
0089 void LocalOsmSearchPlugin::updateDirectory( const QString & )
0090 {
0091     updateDatabase();
0092 }
0093 
0094 void LocalOsmSearchPlugin::updateFile( const QString &file )
0095 {
0096     if ( file.endsWith( QLatin1String( ".sqlite" ) ) ) {
0097         updateDatabase();
0098     }
0099 }
0100 
0101 void LocalOsmSearchPlugin::updateDatabase()
0102 {
0103     m_databaseFiles.clear();
0104     QStringList const baseDirs = QStringList() << MarbleDirs::systemPath() << MarbleDirs::localPath();
0105     for ( const QString &baseDir: baseDirs ) {
0106         const QString base = baseDir + QLatin1String("/maps/earth/placemarks/");
0107         addDatabaseDirectory( base );
0108         QDir::Filters filters = QDir::AllDirs | QDir::Readable | QDir::NoDotAndDotDot;
0109         QDirIterator::IteratorFlags flags = QDirIterator::Subdirectories | QDirIterator::FollowSymlinks;
0110         QDirIterator iter( base, filters, flags );
0111         while ( iter.hasNext() ) {
0112             iter.next();
0113             addDatabaseDirectory( iter.filePath() );
0114         }
0115     }
0116 }
0117 
0118 }
0119 
0120 #include "moc_LocalOsmSearchPlugin.cpp"