Skip to content

Commit e424139

Browse files
committed
Dispatch project and package files through registered handlers
1 parent c050b70 commit e424139

26 files changed

Lines changed: 910 additions & 121 deletions

‎src/plugins/coreplugin/core/CoreInterface.cpp‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <QFileDialog>
1717
#include <QFontDatabase>
1818
#include <QMessageBox>
19+
#include <QPointer>
1920
#include <QQmlComponent>
2021
#include <QQmlEngine>
2122
#include <QQuickWindow>
@@ -79,6 +80,7 @@ namespace Core {
7980
DefaultLyricManager *defaultLyricManager;
8081
SingerRegistry *singerRegistry;
8182
MixedSingerPresets *mixedSingerPresets;
83+
FileOpenDispatcher *fileOpenDispatcher;
8284
Internal::NotificationCenter *notificationCenter;
8385

8486
void init() {
@@ -92,6 +94,7 @@ namespace Core {
9294
defaultLyricManager = new DefaultLyricManager(q);
9395
singerRegistry = new SingerRegistry(q);
9496
mixedSingerPresets = new MixedSingerPresets(q);
97+
fileOpenDispatcher = new FileOpenDispatcher(q);
9598
notificationCenter = new Internal::NotificationCenter(q);
9699
QObject::connect(q, &CoreInterface::resetAllDoNotShowAgainRequested,
97100
notificationCenter, &Internal::NotificationCenter::clearHiddenMessageIdentifiers);
@@ -144,6 +147,11 @@ namespace Core {
144147
return instance()->d_func()->mixedSingerPresets;
145148
}
146149

150+
FileOpenDispatcher *CoreInterface::fileOpenDispatcher() {
151+
Q_ASSERT(instance());
152+
return instance()->d_func()->fileOpenDispatcher;
153+
}
154+
147155
static Internal::NotificationManager::NotificationBubbleMode toInternalNotificationBubbleMode(CoreInterface::NotificationBubbleMode mode) {
148156
return static_cast<Internal::NotificationManager::NotificationBubbleMode>(mode);
149157
}
@@ -436,17 +444,28 @@ namespace Core {
436444
return windowInterface;
437445
}
438446

439-
ProjectWindowInterface *CoreInterface::openFile(const QString &filePath_, QWindow *parent) {
440-
qCInfo(lcCoreInterface) << "Open file" << filePath_;
447+
void CoreInterface::openFiles(const QStringList &filePaths, QWindow *parent) {
448+
if (!filePaths.isEmpty()) {
449+
fileOpenDispatcher()->openFiles(filePaths, parent);
450+
return;
451+
}
452+
const QPointer<QWindow> parentWindow(parent);
453+
OpenSaveProjectFileScenario scenario;
454+
scenario.setWindow(parent);
455+
const auto filePath = scenario.openProjectFile();
456+
if (!filePath.isEmpty()) {
457+
fileOpenDispatcher()->openFiles({filePath}, parentWindow);
458+
}
459+
}
460+
461+
ProjectWindowInterface *CoreInterface::openProjectFile(const QString &filePath, QWindow *parent) {
462+
if (filePath.isEmpty()) {
463+
return nullptr;
464+
}
465+
qCInfo(lcCoreInterface) << "Open project file" << filePath;
441466
Internal::ProjectStartupTimerAddOn::startTimer();
442467
auto projectDocumentContext = std::make_unique<ProjectDocumentContext>();
443468
projectDocumentContext->openSaveProjectFileScenario()->setWindow(parent);
444-
auto filePath = filePath_;
445-
if (filePath.isEmpty()) {
446-
filePath = projectDocumentContext->openSaveProjectFileScenario()->openProjectFile();
447-
if (filePath.isEmpty())
448-
return nullptr;
449-
}
450469

451470
auto windows = windowSystem()->windows();
452471
auto openedWindow = std::ranges::find_if(windows, [filePath](WindowInterface *windowInterface) {

‎src/plugins/coreplugin/core/CoreInterface.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
#define DIFFSCOPE_COREPLUGIN_COREINTERFACE_H
66

77
#include <QObject>
8+
#include <QStringList>
89
#include <qqmlintegration.h>
910

1011
#include <CoreApi/coreinterfacebase.h>
1112

1213
#include <SVSCraftCore/SVSCraftNamespace.h>
1314

15+
#include <coreplugin/FileOpenDispatcher.h>
1416
#include <coreplugin/coreglobal.h>
1517

1618
class QQmlEngine;
@@ -51,6 +53,7 @@ namespace Core {
5153
Q_PROPERTY(DefaultLyricManager *defaultLyricManager READ defaultLyricManager CONSTANT)
5254
Q_PROPERTY(SingerRegistry *singerRegistry READ singerRegistry CONSTANT)
5355
Q_PROPERTY(MixedSingerPresets *mixedSingerPresets READ mixedSingerPresets CONSTANT)
56+
Q_PROPERTY(FileOpenDispatcher *fileOpenDispatcher READ fileOpenDispatcher CONSTANT)
5457
public:
5558
static CoreInterface *instance();
5659
static inline CoreInterface *create(QQmlEngine *, QJSEngine *) { return instance(); }
@@ -68,6 +71,8 @@ namespace Core {
6871

6972
static MixedSingerPresets *mixedSingerPresets();
7073

74+
static FileOpenDispatcher *fileOpenDispatcher();
75+
7176
static constexpr const char *dspxEditorId() {
7277
return "org.diffscope.diffscope";
7378
}
@@ -92,7 +97,8 @@ namespace Core {
9297
public:
9398
Q_INVOKABLE static ProjectWindowInterface *newFile(QWindow *parent = nullptr);
9499
Q_INVOKABLE static ProjectWindowInterface *newFileFromTemplate(const QString &templateFilePath, QWindow *parent = nullptr);
95-
Q_INVOKABLE static ProjectWindowInterface *openFile(const QString &filePath, QWindow *parent = nullptr);
100+
Q_INVOKABLE static void openFiles(const QStringList &filePaths, QWindow *parent = nullptr);
101+
Q_INVOKABLE static ProjectWindowInterface *openProjectFile(const QString &filePath, QWindow *parent = nullptr);
96102
Q_INVOKABLE static ProjectWindowInterface *createProjectWindow(ProjectDocumentContext *projectDocumentContext);
97103

98104
Q_SIGNALS:
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "FileOpenDispatcher.h"
5+
#include "FileOpenDispatcher_p.h"
6+
7+
#include <utility>
8+
9+
#include <QDir>
10+
#include <QLoggingCategory>
11+
#include <QQmlEngine>
12+
#include <QScopedValueRollback>
13+
#include <QThread>
14+
15+
#include <coreplugin/private/FileOpenPlan_p.h>
16+
17+
namespace Core {
18+
19+
Q_STATIC_LOGGING_CATEGORY(lcFileOpenDispatcher, "diffscope.core.fileopendispatcher")
20+
21+
namespace {
22+
23+
QStringList absoluteFilePaths(const QStringList &filePaths) {
24+
const QDir directory;
25+
QStringList paths;
26+
for (const auto &path : filePaths) {
27+
if (!path.isEmpty()) {
28+
paths.append(QDir::cleanPath(directory.absoluteFilePath(path)));
29+
}
30+
}
31+
return paths;
32+
}
33+
34+
QSet<qsizetype> checkedIndexes(const QSet<qsizetype> &indexes, qsizetype size) {
35+
QSet<qsizetype> result;
36+
for (const auto index : indexes) {
37+
if (index < 0 || index >= size) {
38+
qCWarning(lcFileOpenDispatcher) << "File handler returned an invalid index:" << index;
39+
continue;
40+
}
41+
result.insert(index);
42+
}
43+
return result;
44+
}
45+
46+
}
47+
48+
bool FileOpenRequest::isValid() const {
49+
if (hadParentWindow && !parentWindow) {
50+
return false;
51+
}
52+
if (planned) {
53+
for (const auto &registration : handlers) {
54+
if (!registration->active || !registration->handler) {
55+
return false;
56+
}
57+
}
58+
}
59+
return true;
60+
}
61+
62+
void FileOpenDispatcherPrivate::unregisterRegistration(QSharedPointer<FileOpenHandlerRegistration> registration) {
63+
if (!registration->active) {
64+
return;
65+
}
66+
registration->active = false;
67+
QObject::disconnect(registration->destructionConnection);
68+
handlers.removeOne(registration);
69+
const auto currentPlans = plans;
70+
for (const auto &plan : currentPlans) {
71+
if (plan && !plan->isValid()) {
72+
plan->invalidate();
73+
}
74+
}
75+
}
76+
77+
void FileOpenDispatcherPrivate::processRequests() {
78+
if (!ready || processing) {
79+
return;
80+
}
81+
QScopedValueRollback guard(processing, true);
82+
while (ready && !pendingRequests.isEmpty()) {
83+
auto request = pendingRequests.dequeue();
84+
if (!request->planned) {
85+
request->handlers = handlers;
86+
}
87+
if (request->isValid()) {
88+
dispatch(request);
89+
}
90+
}
91+
}
92+
93+
void FileOpenDispatcherPrivate::dispatch(const QSharedPointer<FileOpenRequest> &request) {
94+
QList<qsizetype> remaining;
95+
for (qsizetype index = 0; index < request->filePaths.size(); ++index) {
96+
remaining.append(index);
97+
}
98+
for (qsizetype handlerIndex = 0; handlerIndex < request->handlers.size() && !remaining.isEmpty(); ++handlerIndex) {
99+
const auto registration = request->handlers.at(handlerIndex);
100+
if (!registration->active || !registration->handler) {
101+
continue;
102+
}
103+
QStringList batch;
104+
QList<qsizetype> originalIndexes;
105+
for (const auto index : remaining) {
106+
if (!request->planned || request->owners.at(index) < 0 || request->owners.at(index) == handlerIndex) {
107+
batch.append(request->filePaths.at(index));
108+
originalIndexes.append(index);
109+
}
110+
}
111+
if (batch.isEmpty()) {
112+
continue;
113+
}
114+
const auto claimed = checkedIndexes(registration->handler->openFiles(batch, request->parentWindow), batch.size());
115+
QSet<qsizetype> consumed;
116+
for (qsizetype index = 0; index < originalIndexes.size(); ++index) {
117+
const auto originalIndex = originalIndexes.at(index);
118+
if (claimed.contains(index) || (request->planned && request->owners.at(originalIndex) == handlerIndex)) {
119+
consumed.insert(originalIndex);
120+
}
121+
}
122+
remaining.removeIf([&consumed](qsizetype index) { return consumed.contains(index); });
123+
}
124+
}
125+
126+
bool FileOpenDispatcherPrivate::submit(const QSharedPointer<FileOpenRequest> &request) {
127+
Q_Q(FileOpenDispatcher);
128+
if (!request->isValid()) {
129+
return false;
130+
}
131+
pendingRequests.enqueue(request);
132+
QMetaObject::invokeMethod(q, [this] { processRequests(); }, Qt::QueuedConnection);
133+
return true;
134+
}
135+
136+
FileOpenDispatcher::FileOpenDispatcher(QObject *parent) : QObject(parent), d_ptr(new FileOpenDispatcherPrivate) {
137+
Q_D(FileOpenDispatcher);
138+
d->q_ptr = this;
139+
}
140+
141+
FileOpenDispatcher::~FileOpenDispatcher() = default;
142+
143+
bool FileOpenDispatcher::registerHandler(IFileOpenHandler *handler) {
144+
Q_D(FileOpenDispatcher);
145+
Q_ASSERT(QThread::currentThread() == thread());
146+
if (!handler || handler->thread() != thread()) {
147+
return false;
148+
}
149+
for (const auto &registration : d->handlers) {
150+
if (registration->handler == handler) {
151+
return false;
152+
}
153+
}
154+
auto registration = QSharedPointer<FileOpenHandlerRegistration>::create();
155+
registration->handler = handler;
156+
registration->destructionConnection = connect(handler, &QObject::destroyed, this, [this, registration] {
157+
Q_D(FileOpenDispatcher);
158+
d->unregisterRegistration(registration);
159+
});
160+
d->handlers.append(registration);
161+
return true;
162+
}
163+
164+
bool FileOpenDispatcher::unregisterHandler(IFileOpenHandler *handler) {
165+
Q_D(FileOpenDispatcher);
166+
Q_ASSERT(QThread::currentThread() == thread());
167+
if (!handler) {
168+
return false;
169+
}
170+
for (const auto &registration : std::as_const(d->handlers)) {
171+
if (registration->handler == handler) {
172+
const auto removedRegistration = registration;
173+
d->unregisterRegistration(removedRegistration);
174+
return true;
175+
}
176+
}
177+
return false;
178+
}
179+
180+
void FileOpenDispatcher::openFiles(const QStringList &filePaths, QWindow *parentWindow) {
181+
Q_D(FileOpenDispatcher);
182+
Q_ASSERT(QThread::currentThread() == thread());
183+
auto request = QSharedPointer<FileOpenRequest>::create();
184+
request->filePaths = absoluteFilePaths(filePaths);
185+
if (request->filePaths.isEmpty()) {
186+
return;
187+
}
188+
request->parentWindow = parentWindow;
189+
request->hadParentWindow = parentWindow != nullptr;
190+
d->pendingRequests.enqueue(request);
191+
d->processRequests();
192+
}
193+
194+
FileOpenPlan *FileOpenDispatcher::prepareDrop(const QList<QUrl> &urls, QWindow *parentWindow) {
195+
Q_D(FileOpenDispatcher);
196+
Q_ASSERT(QThread::currentThread() == thread());
197+
auto request = QSharedPointer<FileOpenRequest>::create();
198+
QStringList paths;
199+
for (const auto &url : urls) {
200+
if (url.isValid() && url.isLocalFile()) {
201+
paths.append(url.toLocalFile());
202+
}
203+
}
204+
request->filePaths = absoluteFilePaths(paths);
205+
request->parentWindow = parentWindow;
206+
request->hadParentWindow = parentWindow != nullptr;
207+
request->planned = true;
208+
request->handlers = d->handlers;
209+
for (qsizetype index = 0; index < request->filePaths.size(); ++index) {
210+
request->owners.append(-1);
211+
}
212+
bool accepted = false;
213+
if (d->ready) {
214+
for (qsizetype handlerIndex = 0; handlerIndex < request->handlers.size(); ++handlerIndex) {
215+
if (!request->isValid()) {
216+
break;
217+
}
218+
QStringList batch;
219+
QList<qsizetype> originalIndexes;
220+
for (qsizetype index = 0; index < request->filePaths.size(); ++index) {
221+
if (request->owners.at(index) < 0) {
222+
batch.append(request->filePaths.at(index));
223+
originalIndexes.append(index);
224+
}
225+
}
226+
if (batch.isEmpty()) {
227+
break;
228+
}
229+
const auto &registration = request->handlers.at(handlerIndex);
230+
const auto indexes = checkedIndexes(registration->handler->preflight(batch, request->parentWindow), batch.size());
231+
for (const auto index : indexes) {
232+
request->owners[originalIndexes.at(index)] = handlerIndex;
233+
accepted = true;
234+
}
235+
}
236+
}
237+
auto planData = new FileOpenPlanPrivate;
238+
planData->dispatcher = this;
239+
planData->request = request;
240+
planData->acceptsDrop = accepted;
241+
auto plan = new FileOpenPlan(*planData, this);
242+
QQmlEngine::setObjectOwnership(plan, QQmlEngine::CppOwnership);
243+
if (parentWindow) {
244+
connect(parentWindow, &QObject::destroyed, plan, &FileOpenPlan::invalidate);
245+
}
246+
d->plans.append(plan);
247+
connect(plan, &QObject::destroyed, this, [this] {
248+
Q_D(FileOpenDispatcher);
249+
d->plans.removeIf([](const auto &item) { return item.isNull(); });
250+
});
251+
return plan;
252+
}
253+
254+
void FileOpenDispatcher::setReady(bool ready) {
255+
Q_D(FileOpenDispatcher);
256+
d->ready = ready;
257+
d->processRequests();
258+
}
259+
260+
}
261+
262+
#include "moc_FileOpenDispatcher.cpp"

0 commit comments

Comments
 (0)