2 * Copyright 2015 Ashish Bansal<bansal.ashish096@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "dolphin_generalsettings.h"
23 #include "dolphindebug.h"
24 #include "dolphinmainwindowinterface.h"
26 #include <KDialogJobUiDelegate>
27 #include <KIO/ApplicationLauncherJob>
29 #include <KWindowSystem>
31 #include <QApplication>
34 QList
<QUrl
> Dolphin::validateUris(const QStringList
& uriList
)
36 const QString currentDir
= QDir::currentPath();
38 foreach (const QString
& str
, uriList
) {
39 const QUrl url
= QUrl::fromUserInput(str
, currentDir
, QUrl::AssumeLocalFile
);
43 qCWarning(DolphinDebug
) << "Invalid URL: " << str
;
49 QUrl
Dolphin::homeUrl()
51 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile
);
54 void Dolphin::openNewWindow(const QList
<QUrl
> &urls
, QWidget
*window
, const OpenNewWindowFlags
&flags
)
56 QString command
= QStringLiteral("dolphin --new-window");
58 if (flags
.testFlag(OpenNewWindowFlag::Select
)) {
59 command
.append(QLatin1String(" --select"));
62 if (!urls
.isEmpty()) {
63 command
.append(QLatin1String(" %U"));
65 KService::Ptr
service(new KService(QApplication::applicationDisplayName(), command
, QApplication::windowIcon().name()));
66 auto *job
= new KIO::ApplicationLauncherJob(service
, window
);
68 job
->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, window
));
72 bool Dolphin::attachToExistingInstance(const QList
<QUrl
>& inputUrls
, bool openFiles
, bool splitView
, const QString
& preferredService
)
74 bool attached
= false;
76 // TODO: once Wayland clients can raise or activate themselves remove check from conditional
77 if (KWindowSystem::isPlatformWayland() || inputUrls
.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
81 auto dolphinInterfaces
= dolphinGuiInstances(preferredService
);
82 if (dolphinInterfaces
.isEmpty()) {
88 // check to see if any instances already have any of the given URLs open
89 const auto urls
= QUrl::toStringList(inputUrls
);
90 for (const QString
& url
: urls
) {
91 bool urlFound
= false;
92 for (auto& interface
: dolphinInterfaces
) {
93 auto isUrlOpenReply
= interface
.first
->isUrlOpen(url
);
94 isUrlOpenReply
.waitForFinished();
95 if (!isUrlOpenReply
.isError() && isUrlOpenReply
.value()) {
96 interface
.second
.append(url
);
105 dolphinInterfaces
.front().second
<< newUrls
;
107 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
108 if (!interface
.second
.isEmpty()) {
109 auto reply
= openFiles
? interface
.first
->openFiles(interface
.second
, splitView
) : interface
.first
->openDirectories(interface
.second
, splitView
);
110 reply
.waitForFinished();
111 if (!reply
.isError()) {
112 interface
.first
->activateWindow();
120 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> Dolphin::dolphinGuiInstances(const QString
& preferredService
)
122 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> dolphinInterfaces
;
123 if (!preferredService
.isEmpty()) {
124 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> preferredInterface(
125 new OrgKdeDolphinMainWindowInterface(preferredService
,
126 QStringLiteral("/dolphin/Dolphin_1"),
127 QDBusConnection::sessionBus()));
128 if (preferredInterface
->isValid() && !preferredInterface
->lastError().isValid()) {
129 dolphinInterfaces
.append(qMakePair(preferredInterface
, QStringList()));
133 // Look for dolphin instances among all available dbus services.
134 const QStringList dbusServices
= QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
135 // Don't match the service without trailing "-" (unique instance)
136 const QString pattern
= QStringLiteral("org.kde.dolphin-");
137 // Don't match the pid without leading "-"
138 const QString myPid
= QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
139 for (const QString
& service
: dbusServices
) {
140 if (service
.startsWith(pattern
) && !service
.endsWith(myPid
)) {
141 // Check if instance can handle our URLs
142 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> interface(
143 new OrgKdeDolphinMainWindowInterface(service
,
144 QStringLiteral("/dolphin/Dolphin_1"),
145 QDBusConnection::sessionBus()));
146 if (interface
->isValid() && !interface
->lastError().isValid()) {
147 dolphinInterfaces
.append(qMakePair(interface
, QStringList()));
152 return dolphinInterfaces
;