|
| 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 ®istration : 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 ®istration : 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 ®istration : 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 ®istration = 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