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 <KConfigWatcher>
14 #include <KDialogJobUiDelegate>
15 #include <KIO/ApplicationLauncherJob>
17 #include <KWindowSystem>
19 #include <QApplication>
22 QList
<QUrl
> Dolphin::validateUris(const QStringList
& uriList
)
24 const QString currentDir
= QDir::currentPath();
26 for (const QString
& str
: uriList
) {
27 const QUrl url
= QUrl::fromUserInput(str
, currentDir
, QUrl::AssumeLocalFile
);
31 qCWarning(DolphinDebug
) << "Invalid URL: " << str
;
37 QUrl
Dolphin::homeUrl()
39 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile
);
42 void Dolphin::openNewWindow(const QList
<QUrl
> &urls
, QWidget
*window
, const OpenNewWindowFlags
&flags
)
44 QString command
= QStringLiteral("dolphin --new-window");
46 if (flags
.testFlag(OpenNewWindowFlag::Select
)) {
47 command
.append(QLatin1String(" --select"));
50 if (!urls
.isEmpty()) {
51 command
.append(QLatin1String(" %U"));
53 KService::Ptr
service(new KService(QApplication::applicationDisplayName(), command
, QApplication::windowIcon().name()));
54 auto *job
= new KIO::ApplicationLauncherJob(service
, window
);
56 job
->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, window
));
60 bool Dolphin::attachToExistingInstance(const QList
<QUrl
>& inputUrls
, bool openFiles
, bool splitView
, const QString
& preferredService
)
62 bool attached
= false;
64 // TODO: once Wayland clients can raise or activate themselves remove check from conditional
65 if (KWindowSystem::isPlatformWayland() || inputUrls
.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
69 auto dolphinInterfaces
= dolphinGuiInstances(preferredService
);
70 if (dolphinInterfaces
.isEmpty()) {
76 // check to see if any instances already have any of the given URLs open
77 const auto urls
= QUrl::toStringList(inputUrls
);
78 for (const QString
& url
: urls
) {
79 bool urlFound
= false;
80 for (auto& interface
: dolphinInterfaces
) {
81 auto isUrlOpenReply
= interface
.first
->isUrlOpen(url
);
82 isUrlOpenReply
.waitForFinished();
83 if (!isUrlOpenReply
.isError() && isUrlOpenReply
.value()) {
84 interface
.second
.append(url
);
93 dolphinInterfaces
.front().second
<< newUrls
;
95 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
96 if (!interface
.second
.isEmpty()) {
97 auto reply
= openFiles
? interface
.first
->openFiles(interface
.second
, splitView
) : interface
.first
->openDirectories(interface
.second
, splitView
);
98 reply
.waitForFinished();
99 if (!reply
.isError()) {
100 interface
.first
->activateWindow();
108 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> Dolphin::dolphinGuiInstances(const QString
& preferredService
)
110 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> dolphinInterfaces
;
111 if (!preferredService
.isEmpty()) {
112 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> preferredInterface(
113 new OrgKdeDolphinMainWindowInterface(preferredService
,
114 QStringLiteral("/dolphin/Dolphin_1"),
115 QDBusConnection::sessionBus()));
116 if (preferredInterface
->isValid() && !preferredInterface
->lastError().isValid()) {
117 dolphinInterfaces
.append(qMakePair(preferredInterface
, QStringList()));
121 // Look for dolphin instances among all available dbus services.
122 const QStringList dbusServices
= QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
123 // Don't match the service without trailing "-" (unique instance)
124 const QString pattern
= QStringLiteral("org.kde.dolphin-");
125 // Don't match the pid without leading "-"
126 const QString myPid
= QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
127 for (const QString
& service
: dbusServices
) {
128 if (service
.startsWith(pattern
) && !service
.endsWith(myPid
)) {
129 // Check if instance can handle our URLs
130 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> interface(
131 new OrgKdeDolphinMainWindowInterface(service
,
132 QStringLiteral("/dolphin/Dolphin_1"),
133 QDBusConnection::sessionBus()));
134 if (interface
->isValid() && !interface
->lastError().isValid()) {
135 dolphinInterfaces
.append(qMakePair(interface
, QStringList()));
140 return dolphinInterfaces
;
143 double GlobalConfig::animationDurationFactor()
145 if (s_animationDurationFactor
>= 0.0) {
146 return s_animationDurationFactor
;
148 // This is the first time this method is called.
149 auto kdeGlobalsConfig
= KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
150 updateAnimationDurationFactor(kdeGlobalsConfig
, {"AnimationDurationFactor"});
152 KConfigWatcher::Ptr configWatcher
= KConfigWatcher::create(KSharedConfig::openConfig());
153 connect(configWatcher
.data(), &KConfigWatcher::configChanged
,
154 &GlobalConfig::updateAnimationDurationFactor
);
155 return s_animationDurationFactor
;
158 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup
&group
, const QByteArrayList
&names
)
160 if (group
.name() == QLatin1String("KDE") &&
161 names
.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
162 s_animationDurationFactor
= std::max(0.0,
163 group
.readEntry("AnimationDurationFactor", 1.0));
167 double GlobalConfig::s_animationDurationFactor
= -1.0;