+ QVector<QPair<QSharedPointer<QDBusInterface>, QStringList>> dolphinServices;
+ if (!preferredService.isEmpty()) {
+ QSharedPointer<QDBusInterface> preferred(
+ new QDBusInterface(preferredService,
+ QStringLiteral("/dolphin/Dolphin_1"),
+ QStringLiteral("org.kde.dolphin.MainWindow"))
+ );
+ if (preferred->isValid() && !preferred->lastError().isValid()) {
+ dolphinServices.append(qMakePair(preferred, QStringList()));
+ }
+ }
+
+ // find all dolphin instances
+ const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
+ // Don't match the service without trailing "-" (unique instance)
+ const QString pattern = QStringLiteral("org.kde.dolphin-");
+ // Don't match the pid without leading "-"
+ const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
+ for (const QString& service : services) {
+ if (service.startsWith(pattern) && !service.endsWith(myPid)) {
+ // Check if instance can handle our URLs
+ QSharedPointer<QDBusInterface> instance(
+ new QDBusInterface(service,
+ QStringLiteral("/dolphin/Dolphin_1"),
+ QStringLiteral("org.kde.dolphin.MainWindow"))
+ );
+ if (instance->isValid() && !instance->lastError().isValid()) {
+ dolphinServices.append(qMakePair(instance, QStringList()));
+ }
+ }
+ }
+
+ if (dolphinServices.isEmpty()) {
+ return false;
+ }
+
+ QStringList newUrls;
+
+ // check to see if any instances already have any of the given URLs open
+ const auto urls = QUrl::toStringList(inputUrls);
+ for (const QString& url : urls) {
+ bool urlFound = false;
+ for (auto& service: dolphinServices) {
+ QDBusReply<bool> isUrlOpen = service.first->call(QStringLiteral("isUrlOpen"), url);
+ if (isUrlOpen.isValid() && isUrlOpen.value()) {
+ service.second.append(url);
+ urlFound = true;
+ break;
+ }
+ }
+ if (!urlFound) {
+ newUrls.append(url);
+ }
+ }
+ dolphinServices.front().second << newUrls;
+
+ for (const auto& service: dolphinServices) {
+ if (!service.second.isEmpty()) {
+ service.first->call(openFiles ? QStringLiteral("openFiles") : QStringLiteral("openDirectories"), service.second, splitView);
+ service.first->call(QStringLiteral("activateWindow"));
+ }
+ }
+ return true;