File indexing completed on 2024-04-28 03:50:24

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Guillaume Martres <smarter@ubuntu.com>
0004 //
0005 
0006 #include "TrackerPluginModel.h"
0007 
0008 #include "CacheStoragePolicy.h"
0009 #include "HttpDownloadManager.h"
0010 #include "GeoDataDocument.h"
0011 #include "GeoDataPlacemark.h"
0012 #include "GeoDataTreeModel.h"
0013 #include "MarbleDebug.h"
0014 #include "MarbleDirs.h"
0015 #include "MarbleModel.h"
0016 #include "TrackerPluginItem.h"
0017 
0018 namespace Marble
0019 {
0020 
0021 class TrackerPluginModelPrivate
0022 {
0023 public:
0024     TrackerPluginModelPrivate( TrackerPluginModel *parent, GeoDataTreeModel *treeModel )
0025         : m_parent( parent ),
0026           m_enabled( false ),
0027           m_treeModel( treeModel ),
0028           m_document( new GeoDataDocument() ),
0029           m_storagePolicy(MarbleDirs::localPath() + QLatin1String("/cache/")),
0030           m_downloadManager( nullptr )
0031     {
0032     }
0033 
0034     ~TrackerPluginModelPrivate()
0035     {
0036         delete m_document;
0037         qDeleteAll( m_itemVector );
0038         delete m_downloadManager;
0039     }
0040 
0041     void downloaded(const QString &relativeUrlString, const QString &id)
0042     {
0043         Q_UNUSED( relativeUrlString );
0044 
0045         m_parent->parseFile( id, m_storagePolicy.data( id ) );
0046     }
0047 
0048     void update()
0049     {
0050         for( TrackerPluginItem *item: m_itemVector ) {
0051             item->update();
0052         }
0053     }
0054 
0055     void updateDocument()
0056     {
0057         // we cannot use ->clear() since its implementation
0058         // will delete all items
0059         for( TrackerPluginItem *item: m_itemVector ) {
0060             int idx = m_document->childPosition( item->placemark() );
0061             if( item->isEnabled() && idx == -1 ) {
0062                 m_document->append( item->placemark() );
0063             }
0064             if( !item->isEnabled() && idx > -1 ) {
0065                 m_document->remove( idx );
0066             }
0067         }
0068     }
0069 
0070     TrackerPluginModel *m_parent;
0071     bool m_enabled;
0072     GeoDataTreeModel *m_treeModel;
0073     GeoDataDocument *m_document;
0074     CacheStoragePolicy m_storagePolicy;
0075     HttpDownloadManager *m_downloadManager;
0076     QVector<TrackerPluginItem *> m_itemVector;
0077 };
0078 
0079 TrackerPluginModel::TrackerPluginModel( GeoDataTreeModel *treeModel )
0080     : d( new TrackerPluginModelPrivate( this, treeModel ) )
0081 {
0082     d->m_document->setDocumentRole( TrackingDocument );
0083     d->m_document->setName(QStringLiteral("Satellites"));
0084     if( d->m_enabled ) {
0085         d->m_treeModel->addDocument( d->m_document );
0086     }
0087 
0088     d->m_downloadManager = new HttpDownloadManager( &d->m_storagePolicy );
0089     connect( d->m_downloadManager, SIGNAL(downloadComplete(QString,QString)),
0090              this, SLOT(downloaded(QString,QString)) );
0091 }
0092 
0093 TrackerPluginModel::~TrackerPluginModel()
0094 {
0095     if( d->m_enabled ) {
0096         d->m_treeModel->removeDocument( d->m_document );
0097     }
0098     delete d;
0099 }
0100 
0101 void TrackerPluginModel::enable( bool enabled )
0102 {
0103     if( enabled == d->m_enabled ) {
0104         return;
0105     }
0106     if( enabled ) {
0107         d->m_treeModel->addDocument( d->m_document );
0108     } else {
0109         d->m_treeModel->removeDocument( d->m_document );
0110     }
0111     d->m_enabled = enabled;
0112 }
0113 
0114 void TrackerPluginModel::addItem( TrackerPluginItem *mark )
0115 {
0116     d->m_document->append( mark->placemark() );
0117     d->m_itemVector.append( mark );
0118 }
0119 
0120 QVector<TrackerPluginItem*> TrackerPluginModel::items() const
0121 {
0122     return d->m_itemVector;
0123 }
0124 
0125 void TrackerPluginModel::clear()
0126 {
0127     beginUpdateItems();
0128     qDeleteAll( d->m_itemVector );
0129     d->m_itemVector.clear();
0130     d->m_itemVector.squeeze();
0131     d->m_document->clear();
0132     endUpdateItems();
0133 }
0134 
0135 void TrackerPluginModel::beginUpdateItems()
0136 {
0137     if( d->m_enabled ) {
0138         d->m_treeModel->removeDocument( d->m_document );
0139     }
0140 
0141     emit itemUpdateStarted();
0142 }
0143 
0144 void TrackerPluginModel::endUpdateItems()
0145 {
0146     if( d->m_enabled ) {
0147         d->updateDocument();
0148         d->m_treeModel->addDocument( d->m_document );
0149     }
0150 
0151     emit itemUpdateEnded();
0152 }
0153 
0154 void TrackerPluginModel::downloadFile(const QUrl &url, const QString &id)
0155 {
0156     d->m_downloadManager->addJob( url, id, id, DownloadBrowse );
0157 }
0158 
0159 void TrackerPluginModel::parseFile( const QString &id, const QByteArray &file )
0160 {
0161     Q_UNUSED( id );
0162     Q_UNUSED( file );
0163 }
0164 
0165 } // namespace Marble
0166 
0167 #include "moc_TrackerPluginModel.cpp"