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"
12 #include "views/viewproperties.h"
14 #include <KConfigWatcher>
15 #include <KDialogJobUiDelegate>
16 #include <KIO/ApplicationLauncherJob>
18 #include <KWindowSystem>
20 #include <QApplication>
23 QList
<QUrl
> Dolphin::validateUris(const QStringList
& uriList
)
25 const QString currentDir
= QDir::currentPath();
27 for (const QString
& str
: uriList
) {
28 const QUrl url
= QUrl::fromUserInput(str
, currentDir
, QUrl::AssumeLocalFile
);
32 qCWarning(DolphinDebug
) << "Invalid URL: " << str
;
38 QUrl
Dolphin::homeUrl()
40 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile
);
43 void Dolphin::openNewWindow(const QList
<QUrl
> &urls
, QWidget
*window
, const OpenNewWindowFlags
&flags
)
45 QString command
= QStringLiteral("dolphin --new-window");
47 if (flags
.testFlag(OpenNewWindowFlag::Select
)) {
48 command
.append(QLatin1String(" --select"));
51 if (!urls
.isEmpty()) {
52 command
.append(QLatin1String(" %U"));
54 KService::Ptr
service(new KService(QApplication::applicationDisplayName(), command
, QApplication::windowIcon().name()));
55 auto *job
= new KIO::ApplicationLauncherJob(service
, window
);
57 job
->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, window
));
61 bool Dolphin::attachToExistingInstance(const QList
<QUrl
>& inputUrls
, bool openFiles
, bool splitView
, const QString
& preferredService
)
63 bool attached
= false;
65 // TODO: once Wayland clients can raise or activate themselves remove check from conditional
66 if (KWindowSystem::isPlatformWayland() || inputUrls
.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
70 auto dolphinInterfaces
= dolphinGuiInstances(preferredService
);
71 if (dolphinInterfaces
.isEmpty()) {
77 // check to see if any instances already have any of the given URLs open
78 const auto urls
= QUrl::toStringList(inputUrls
);
79 for (const QString
& url
: urls
) {
80 bool urlFound
= false;
81 for (auto& interface
: dolphinInterfaces
) {
82 auto isUrlOpenReply
= interface
.first
->isUrlOpen(url
);
83 isUrlOpenReply
.waitForFinished();
84 if (!isUrlOpenReply
.isError() && isUrlOpenReply
.value()) {
85 interface
.second
.append(url
);
94 dolphinInterfaces
.front().second
<< newUrls
;
96 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
97 if (!interface
.second
.isEmpty()) {
98 auto reply
= openFiles
? interface
.first
->openFiles(interface
.second
, splitView
) : interface
.first
->openDirectories(interface
.second
, splitView
);
99 reply
.waitForFinished();
100 if (!reply
.isError()) {
101 interface
.first
->activateWindow();
109 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> Dolphin::dolphinGuiInstances(const QString
& preferredService
)
111 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> dolphinInterfaces
;
112 if (!preferredService
.isEmpty()) {
113 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> preferredInterface(
114 new OrgKdeDolphinMainWindowInterface(preferredService
,
115 QStringLiteral("/dolphin/Dolphin_1"),
116 QDBusConnection::sessionBus()));
117 if (preferredInterface
->isValid() && !preferredInterface
->lastError().isValid()) {
118 dolphinInterfaces
.append(qMakePair(preferredInterface
, QStringList()));
122 // Look for dolphin instances among all available dbus services.
123 QDBusConnectionInterface
*sessionInterface
= QDBusConnection::sessionBus().interface();
124 const QStringList dbusServices
= sessionInterface
? sessionInterface
->registeredServiceNames().value() : QStringList();
125 // Don't match the service without trailing "-" (unique instance)
126 const QString pattern
= QStringLiteral("org.kde.dolphin-");
127 // Don't match the pid without leading "-"
128 const QString myPid
= QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
129 for (const QString
& service
: dbusServices
) {
130 if (service
.startsWith(pattern
) && !service
.endsWith(myPid
)) {
131 // Check if instance can handle our URLs
132 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> interface(
133 new OrgKdeDolphinMainWindowInterface(service
,
134 QStringLiteral("/dolphin/Dolphin_1"),
135 QDBusConnection::sessionBus()));
136 if (interface
->isValid() && !interface
->lastError().isValid()) {
137 dolphinInterfaces
.append(qMakePair(interface
, QStringList()));
142 return dolphinInterfaces
;
145 QPair
<QString
, Qt::SortOrder
> Dolphin::sortOrderForUrl(QUrl
&url
)
147 ViewProperties
globalProps(url
);
148 return QPair
<QString
, Qt::SortOrder
>(globalProps
.sortRole(), globalProps
.sortOrder());
151 double GlobalConfig::animationDurationFactor()
153 if (s_animationDurationFactor
>= 0.0) {
154 return s_animationDurationFactor
;
156 // This is the first time this method is called.
157 auto kdeGlobalsConfig
= KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
158 updateAnimationDurationFactor(kdeGlobalsConfig
, {"AnimationDurationFactor"});
160 KConfigWatcher::Ptr configWatcher
= KConfigWatcher::create(KSharedConfig::openConfig());
161 connect(configWatcher
.data(), &KConfigWatcher::configChanged
,
162 &GlobalConfig::updateAnimationDurationFactor
);
163 return s_animationDurationFactor
;
166 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup
&group
, const QByteArrayList
&names
)
168 if (group
.name() == QLatin1String("KDE") &&
169 names
.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
170 s_animationDurationFactor
= std::max(0.0,
171 group
.readEntry("AnimationDurationFactor", 1.0));
175 double GlobalConfig::s_animationDurationFactor
= -1.0;