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>
19 #include <QApplication>
21 QList
<QUrl
> Dolphin::validateUris(const QStringList
& uriList
)
23 const QString currentDir
= QDir::currentPath();
25 for (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 if (inputUrls
.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
67 auto dolphinInterfaces
= dolphinGuiInstances(preferredService
);
68 if (dolphinInterfaces
.isEmpty()) {
72 int activeWindowIndex
= -1;
73 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
76 auto isActiveWindowReply
= interface
.first
->isActiveWindow();
77 isActiveWindowReply
.waitForFinished();
78 if (!isActiveWindowReply
.isError() && isActiveWindowReply
.value()) {
83 // check to see if any instances already have any of the given URLs or their parents open
84 const auto urls
= QUrl::toStringList(inputUrls
);
85 for (const QString
& url
: urls
) {
86 bool urlFound
= false;
88 // looping through the windows starting from the active one
89 int i
= activeWindowIndex
;
91 auto &interface
= dolphinInterfaces
[i
];
93 auto isUrlOpenReply
= openFiles
? interface
.first
->isUrlOrParentOpen(url
) : interface
.first
->isUrlOpen(url
);
94 isUrlOpenReply
.waitForFinished();
95 if (!isUrlOpenReply
.isError() && isUrlOpenReply
.value()) {
96 interface
.second
.append(url
);
101 i
= (i
+ 1) % dolphinInterfaces
.size();
103 while (i
!= activeWindowIndex
);
106 dolphinInterfaces
[activeWindowIndex
].second
.append(url
);
110 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
111 if (interface
.second
.isEmpty()) {
114 auto reply
= openFiles
?
115 interface
.first
->openFiles(interface
.second
, splitView
) :
116 interface
.first
->openDirectories(interface
.second
, splitView
);
117 reply
.waitForFinished();
118 if (!reply
.isError()) {
119 interface
.first
->activateWindow();
126 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> Dolphin::dolphinGuiInstances(const QString
& preferredService
)
128 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> dolphinInterfaces
;
129 if (!preferredService
.isEmpty()) {
130 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> preferredInterface(
131 new OrgKdeDolphinMainWindowInterface(preferredService
,
132 QStringLiteral("/dolphin/Dolphin_1"),
133 QDBusConnection::sessionBus()));
134 if (preferredInterface
->isValid() && !preferredInterface
->lastError().isValid()) {
135 dolphinInterfaces
.append(qMakePair(preferredInterface
, QStringList()));
139 // Look for dolphin instances among all available dbus services.
140 QDBusConnectionInterface
*sessionInterface
= QDBusConnection::sessionBus().interface();
141 const QStringList dbusServices
= sessionInterface
? sessionInterface
->registeredServiceNames().value() : QStringList();
142 // Don't match the service without trailing "-" (unique instance)
143 const QString pattern
= QStringLiteral("org.kde.dolphin-");
144 // Don't match the pid without leading "-"
145 const QString myPid
= QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
146 for (const QString
& service
: dbusServices
) {
147 if (service
.startsWith(pattern
) && !service
.endsWith(myPid
)) {
148 // Check if instance can handle our URLs
149 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> interface(
150 new OrgKdeDolphinMainWindowInterface(service
,
151 QStringLiteral("/dolphin/Dolphin_1"),
152 QDBusConnection::sessionBus()));
153 if (interface
->isValid() && !interface
->lastError().isValid()) {
154 dolphinInterfaces
.append(qMakePair(interface
, QStringList()));
159 return dolphinInterfaces
;
162 QPair
<QString
, Qt::SortOrder
> Dolphin::sortOrderForUrl(QUrl
&url
)
164 ViewProperties
globalProps(url
);
165 return QPair
<QString
, Qt::SortOrder
>(globalProps
.sortRole(), globalProps
.sortOrder());
168 double GlobalConfig::animationDurationFactor()
170 if (s_animationDurationFactor
>= 0.0) {
171 return s_animationDurationFactor
;
173 // This is the first time this method is called.
174 auto kdeGlobalsConfig
= KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
175 updateAnimationDurationFactor(kdeGlobalsConfig
, {"AnimationDurationFactor"});
177 KConfigWatcher::Ptr configWatcher
= KConfigWatcher::create(KSharedConfig::openConfig());
178 connect(configWatcher
.data(), &KConfigWatcher::configChanged
,
179 &GlobalConfig::updateAnimationDurationFactor
);
180 return s_animationDurationFactor
;
183 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup
&group
, const QByteArrayList
&names
)
185 if (group
.name() == QLatin1String("KDE") &&
186 names
.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
187 s_animationDurationFactor
= std::max(0.0,
188 group
.readEntry("AnimationDurationFactor", 1.0));
192 double GlobalConfig::s_animationDurationFactor
= -1.0;