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 "ParseRunnerPlugin.h"
12#include "PluginManager.h"
13#include "RunnerTask.h"
14
15#include <QFileInfo>
16#include <QList>
17#include <QMutex>
18#include <QThreadPool>
19#include <QTimer>
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 Q_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 : std::as_const(plugins)) {
89 QStringList const extensions = plugin->fileExtensions();
90 if (extensions.isEmpty() || extensions.contains(suffix) || extensions.contains(completeSuffix)) {
91 auto 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 Q_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, &QTimer::timeout, &localEventLoop, &QEventLoop::quit);
111 connect(this, SIGNAL(parsingFinished()), &localEventLoop, SLOT(quit()), Qt::QueuedConnection);
112
113 watchdog.start(timeout);
114 parseFile(fileName, role);
115 localEventLoop.exec();
116 return d->m_fileResult;
117}
118
119void ParsingRunnerManager::Private::addParsingResult(GeoDataDocument *document, const QString &error)
120{
121 if (document || !error.isEmpty()) {
122 if (document) {
123 m_fileResult = document;
124 }
125 Q_EMIT q->parsingFinished(document, error);
126 }
127}
128
129}
130
131#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)
void quit()
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()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.