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>
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
, const QString
&activationToken
)
62 bool attached
= false;
64 if (inputUrls
.isEmpty()) {
68 auto dolphinInterfaces
= dolphinGuiInstances(preferredService
);
69 if (dolphinInterfaces
.isEmpty()) {
73 int activeWindowIndex
= -1;
74 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
77 auto isActiveWindowReply
= interface
.first
->isActiveWindow();
78 isActiveWindowReply
.waitForFinished();
79 if (!isActiveWindowReply
.isError() && isActiveWindowReply
.value()) {
84 // check to see if any instances already have any of the given URLs or their parents open
85 QList
<QUrl
> newWindowURLs
;
86 for (const QUrl
& url
: inputUrls
) {
87 bool urlFound
= false;
88 const QString urlString
= url
.toString();
90 // looping through the windows starting from the active one
91 int i
= activeWindowIndex
;
93 auto &interface
= dolphinInterfaces
[i
];
95 auto isUrlOpenReply
= openFiles
? interface
.first
->isItemVisibleInAnyView(urlString
) : interface
.first
->isUrlOpen(urlString
);
96 isUrlOpenReply
.waitForFinished();
97 if (!isUrlOpenReply
.isError() && isUrlOpenReply
.value()) {
98 interface
.second
.append(urlString
);
103 i
= (i
+ 1) % dolphinInterfaces
.size();
105 while (i
!= activeWindowIndex
);
108 if (GeneralSettings::openExternallyCalledFolderInNewTab()) {
109 dolphinInterfaces
[activeWindowIndex
].second
.append(urlString
);
111 newWindowURLs
.append(url
);
116 for (const auto& interface
: qAsConst(dolphinInterfaces
)) {
117 if (interface
.second
.isEmpty()) {
120 auto reply
= openFiles
?
121 interface
.first
->openFiles(interface
.second
, splitView
) :
122 interface
.first
->openDirectories(interface
.second
, splitView
);
123 reply
.waitForFinished();
124 if (!reply
.isError()) {
125 interface
.first
->activateWindow(activationToken
);
129 if (attached
&& !newWindowURLs
.isEmpty()) {
131 openNewWindow(newWindowURLs
, nullptr, Dolphin::OpenNewWindowFlag::Select
);
133 openNewWindow(newWindowURLs
);
140 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> Dolphin::dolphinGuiInstances(const QString
& preferredService
)
142 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> dolphinInterfaces
;
143 if (!preferredService
.isEmpty()) {
144 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> preferredInterface(
145 new OrgKdeDolphinMainWindowInterface(preferredService
,
146 QStringLiteral("/dolphin/Dolphin_1"),
147 QDBusConnection::sessionBus()));
148 if (preferredInterface
->isValid() && !preferredInterface
->lastError().isValid()) {
149 dolphinInterfaces
.append(qMakePair(preferredInterface
, QStringList()));
153 // Look for dolphin instances among all available dbus services.
154 QDBusConnectionInterface
*sessionInterface
= QDBusConnection::sessionBus().interface();
155 const QStringList dbusServices
= sessionInterface
? sessionInterface
->registeredServiceNames().value() : QStringList();
156 // Don't match the service without trailing "-" (unique instance)
157 const QString pattern
= QStringLiteral("org.kde.dolphin-");
158 // Don't match the pid without leading "-"
159 const QString myPid
= QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
160 for (const QString
& service
: dbusServices
) {
161 if (service
.startsWith(pattern
) && !service
.endsWith(myPid
)) {
162 // Check if instance can handle our URLs
163 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> interface(
164 new OrgKdeDolphinMainWindowInterface(service
,
165 QStringLiteral("/dolphin/Dolphin_1"),
166 QDBusConnection::sessionBus()));
167 if (interface
->isValid() && !interface
->lastError().isValid()) {
168 dolphinInterfaces
.append(qMakePair(interface
, QStringList()));
173 return dolphinInterfaces
;
176 QPair
<QString
, Qt::SortOrder
> Dolphin::sortOrderForUrl(QUrl
&url
)
178 ViewProperties
globalProps(url
);
179 return QPair
<QString
, Qt::SortOrder
>(globalProps
.sortRole(), globalProps
.sortOrder());
182 double GlobalConfig::animationDurationFactor()
184 if (s_animationDurationFactor
>= 0.0) {
185 return s_animationDurationFactor
;
187 // This is the first time this method is called.
188 auto kdeGlobalsConfig
= KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
189 updateAnimationDurationFactor(kdeGlobalsConfig
, {"AnimationDurationFactor"});
191 KConfigWatcher::Ptr configWatcher
= KConfigWatcher::create(KSharedConfig::openConfig());
192 connect(configWatcher
.data(), &KConfigWatcher::configChanged
,
193 &GlobalConfig::updateAnimationDurationFactor
);
194 return s_animationDurationFactor
;
197 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup
&group
, const QByteArrayList
&names
)
199 if (group
.name() == QLatin1String("KDE") &&
200 names
.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
201 s_animationDurationFactor
= std::max(0.0,
202 group
.readEntry("AnimationDurationFactor", 1.0));
206 double GlobalConfig::s_animationDurationFactor
= -1.0;