Marble

ParsingRunnerManager.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2008 Henry de Valence <hdevalence@gmail.com>
4// SPDX-FileCopyrightText: 2010 Dennis Nienhüser <nienhueser@kde.org>
5// SPDX-FileCopyrightText: 2010-2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
6// SPDX-FileCopyrightText: 2011 Thibaut Gridel <tgridel@free.fr>
7
8#include "ParsingRunnerManager.h"
9
10#include "MarbleDebug.h"
11#include "PluginManager.h"
12#include "ParseRunnerPlugin.h"
13#include "RunnerTask.h"
14
15#include <QFileInfo>
16#include <QList>
17#include <QThreadPool>
18#include <QTimer>
19#include <QMutex>
20
21namespace Marble
22{
23
24class MarbleModel;
25
26class Q_DECL_HIDDEN ParsingRunnerManager::Private
27{
28public:
29 Private( ParsingRunnerManager *parent, const PluginManager *pluginManager );
30
31 ~Private();
32
33 void cleanupParsingTask();
34 void addParsingResult(GeoDataDocument *document, const QString &error);
35
36 ParsingRunnerManager *const q;
37 const PluginManager *const m_pluginManager;
38 QMutex m_parsingTasksMutex;
39 int m_parsingTasks;
40 GeoDataDocument *m_fileResult;
41};
42
43ParsingRunnerManager::Private::Private( ParsingRunnerManager *parent, const PluginManager *pluginManager ) :
44 q( parent ),
45 m_pluginManager( pluginManager ),
46 m_parsingTasks(0),
47 m_fileResult( nullptr )
48{
49 qRegisterMetaType<GeoDataDocument*>( "GeoDataDocument*" );
50}
51
52ParsingRunnerManager::Private::~Private()
53{
54 // nothing to do
55}
56
57void ParsingRunnerManager::Private::cleanupParsingTask()
58{
59 QMutexLocker locker(&m_parsingTasksMutex);
60 m_parsingTasks = qMax(0, m_parsingTasks-1);
61 if (m_parsingTasks == 0) {
62 emit q->parsingFinished();
63 }
64}
65
66ParsingRunnerManager::ParsingRunnerManager( const PluginManager *pluginManager, QObject *parent ) :
67 QObject( parent ),
68 d( new Private( this, pluginManager ) )
69{
70 if ( QThreadPool::globalInstance()->maxThreadCount() < 4 ) {
72 }
73}
74
75ParsingRunnerManager::~ParsingRunnerManager()
76{
77 delete d;
78}
79
80void ParsingRunnerManager::parseFile( const QString &fileName, DocumentRole role )
81{
82 QList<const ParseRunnerPlugin*> plugins = d->m_pluginManager->parsingRunnerPlugins();
83 const QFileInfo fileInfo( fileName );
84 const QString suffix = fileInfo.suffix().toLower();
85 const QString completeSuffix = fileInfo.completeSuffix().toLower();
86
87 d->m_parsingTasks = 0;
88 for( const ParseRunnerPlugin *plugin: plugins ) {
89 QStringList const extensions = plugin->fileExtensions();
90 if ( extensions.isEmpty() || extensions.contains( suffix ) || extensions.contains( completeSuffix ) ) {
91 ParsingTask *task = new ParsingTask( plugin->newRunner(), this, fileName, role );
92 connect( task, SIGNAL(finished()), this, SLOT(cleanupParsingTask()) );
93 mDebug() << "parse task " << plugin->nameId() << " " << (quintptr)task;
94 ++d->m_parsingTasks;
96 }
97 }
98
99 if (d->m_parsingTasks == 0) {
100 emit parsingFinished();
101 }
102}
103
104GeoDataDocument *ParsingRunnerManager::openFile( const QString &fileName, DocumentRole role, int timeout )
105{
106 d->m_fileResult = nullptr;
107 QEventLoop localEventLoop;
108 QTimer watchdog;
109 watchdog.setSingleShot(true);
110 connect( &watchdog, SIGNAL(timeout()),
111 &localEventLoop, SLOT(quit()));
112 connect(this, SIGNAL(parsingFinished()),
113 &localEventLoop, SLOT(quit()), Qt::QueuedConnection );
114
115 watchdog.start( timeout );
116 parseFile( fileName, role);
117 localEventLoop.exec();
118 return d->m_fileResult;
119}
120
121void ParsingRunnerManager::Private::addParsingResult(GeoDataDocument *document, const QString &error)
122{
123 if ( document || !error.isEmpty() ) {
124 if (document) {
125 m_fileResult = document;
126 }
127 emit q->parsingFinished( document, error );
128 }
129}
130
131}
132
133#include "moc_ParsingRunnerManager.cpp"
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem quit()
Binds a QML item to a specific geodetic location in screen coordinates.
int exec(ProcessEventsFlags flags)
bool isEmpty() const const
QString toLower() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QueuedConnection
QTaskBuilder< Task > task(Task &&task)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QThreadPool * globalInstance()
void setMaxThreadCount(int maxThreadCount)
void start(Callable &&callableToRun, int priority)
void setSingleShot(bool singleShot)
void start()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.