2 * SPDX-FileCopyrightText: 2015 Ashish Bansal <bansal.ashish096@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "dolphin_generalsettings.h"
10 #include "dolphindebug.h"
11 #include "dolphinmainwindowinterface.h"
13 #include <KDialogJobUiDelegate>
14 #include <KIO/ApplicationLauncherJob>
16 #include <KWindowSystem>
18 #include <QApplication>
21 QList
<QUrl
> Dolphin::validateUris(const QStringList
& uriList
)
23 const QString currentDir
= QDir::currentPath();
25 foreach (const QString
& str
, uriList
) {
26 const QUrl url
= QUrl::fromUserInput(str
, currentDir
, QUrl::AssumeLocalFile
);
30 qCWarning(DolphinDebug
) << "Invalid URL: " << str
;
36 QUrl
Dolphin::homeUrl()
38 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile
);
41 void Dolphin::openNewWindow(const QList
<QUrl
> &urls
, QWidget
*window
, const OpenNewWindowFlags
&flags
)
43 QString command
= QStringLiteral("dolphin --new-window");
45 if (flags
.testFlag(OpenNewWindowFlag::Select
)) {
46 command
.append(QLatin1String(" --select"));
49 if (!urls
.isEmpty()) {
50 command
.append(QLatin1String(" %U"));
52 KService::Ptr
service(new KService(QApplication::applicationDisplayName(), command
, QApplication::windowIcon().name()));
53 auto *job
= new KIO::ApplicationLauncherJob(service
, window
);
55 job
->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, window
));
59 bool Dolphin::attachToExistingInstance(const QList
<QUrl
>& inputUrls
, bool openFiles
, bool splitView
, const QString
& preferredService
)
61 bool attached
= false;
63 // TODO: once Wayland clients can raise or activate themselves remove check from conditional
64 if (KWindowSystem::isPlatformWayland() || inputUrls
.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
68 auto dolphinInterfaces
= dolphinGuiInstances(preferredService
);
69 if (dolphinInterfaces
.isEmpty()) {
75 // check to see if any instances already have any of the given URLs open
76 const auto urls
= QUrl::toStringList(inputUrls
);
77 for (const QString
& url
: urls
) {
78 bool urlFound
= false;
79 for (auto& interface
: dolphinInterfaces
) {
80 auto isUrlOpenReply
= interface
.first
->isUrlOpen(url
);
81 isUrlOpenReply
.waitForFinished();
82 if (!isUrlOpenReply
.isError() && isUrlOpenReply
.value()) {
83 interface
.second
.append(url
);
92 dolphinInterfaces
.front().second
<< newUrls
;
94 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
95 if (!interface
.second
.isEmpty()) {
96 auto reply
= openFiles
? interface
.first
->openFiles(interface
.second
, splitView
) : interface
.first
->openDirectories(interface
.second
, splitView
);
97 reply
.waitForFinished();
98 if (!reply
.isError()) {
99 interface
.first
->activateWindow();
107 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> Dolphin::dolphinGuiInstances(const QString
& preferredService
)
109 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> dolphinInterfaces
;
110 if (!preferredService
.isEmpty()) {
111 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> preferredInterface(
112 new OrgKdeDolphinMainWindowInterface(preferredService
,
113 QStringLiteral("/dolphin/Dolphin_1"),
114 QDBusConnection::sessionBus()));
115 if (preferredInterface
->isValid() && !preferredInterface
->lastError().isValid()) {
116 dolphinInterfaces
.append(qMakePair(preferredInterface
, QStringList()));
120 // Look for dolphin instances among all available dbus services.
121 const QStringList dbusServices
= QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
122 // Don't match the service without trailing "-" (unique instance)
123 const QString pattern
= QStringLiteral("org.kde.dolphin-");
124 // Don't match the pid without leading "-"
125 const QString myPid
= QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
126 for (const QString
& service
: dbusServices
) {
127 if (service
.startsWith(pattern
) && !service
.endsWith(myPid
)) {
128 // Check if instance can handle our URLs
129 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> interface(
130 new OrgKdeDolphinMainWindowInterface(service
,
131 QStringLiteral("/dolphin/Dolphin_1"),
132 QDBusConnection::sessionBus()));
133 if (interface
->isValid() && !interface
->lastError().isValid()) {
134 dolphinInterfaces
.append(qMakePair(interface
, QStringList()));
139 return dolphinInterfaces
;