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
,
63 const QString
&preferredService
,
64 const QString
&activationToken
)
66 bool attached
= false;
68 if (inputUrls
.isEmpty()) {
72 auto dolphinInterfaces
= dolphinGuiInstances(preferredService
);
73 if (dolphinInterfaces
.isEmpty()) {
77 int activeWindowIndex
= -1;
78 for (const auto &interface
: std::as_const(dolphinInterfaces
)) {
81 auto isActiveWindowReply
= interface
.first
->isActiveWindow();
82 isActiveWindowReply
.waitForFinished();
83 if (!isActiveWindowReply
.isError() && isActiveWindowReply
.value()) {
88 // check to see if any instances already have any of the given URLs or their parents open
89 QList
<QUrl
> newWindowURLs
;
90 for (const QUrl
&url
: inputUrls
) {
91 bool urlFound
= false;
92 const QString urlString
= url
.toString();
94 // looping through the windows starting from the active one
95 int i
= activeWindowIndex
;
97 auto &interface
= dolphinInterfaces
[i
];
99 auto isUrlOpenReply
= openFiles
? interface
.first
->isItemVisibleInAnyView(urlString
) : interface
.first
->isUrlOpen(urlString
);
100 isUrlOpenReply
.waitForFinished();
101 if (!isUrlOpenReply
.isError() && isUrlOpenReply
.value()) {
102 interface
.second
.append(urlString
);
107 i
= (i
+ 1) % dolphinInterfaces
.size();
108 } while (i
!= activeWindowIndex
);
111 if (GeneralSettings::openExternallyCalledFolderInNewTab()) {
112 dolphinInterfaces
[activeWindowIndex
].second
.append(urlString
);
114 newWindowURLs
.append(url
);
119 for (const auto &interface
: std::as_const(dolphinInterfaces
)) {
120 if (interface
.second
.isEmpty()) {
123 auto reply
= openFiles
? interface
.first
->openFiles(interface
.second
, splitView
) : interface
.first
->openDirectories(interface
.second
, splitView
);
124 reply
.waitForFinished();
125 if (!reply
.isError()) {
126 interface
.first
->activateWindow(activationToken
);
130 if (attached
&& !newWindowURLs
.isEmpty()) {
132 openNewWindow(newWindowURLs
, nullptr, Dolphin::OpenNewWindowFlag::Select
);
134 openNewWindow(newWindowURLs
);
141 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> Dolphin::dolphinGuiInstances(const QString
&preferredService
)
143 QVector
<QPair
<QSharedPointer
<OrgKdeDolphinMainWindowInterface
>, QStringList
>> dolphinInterfaces
;
144 if (!preferredService
.isEmpty()) {
145 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> preferredInterface(
146 new OrgKdeDolphinMainWindowInterface(preferredService
, QStringLiteral("/dolphin/Dolphin_1"), QDBusConnection::sessionBus()));
147 if (preferredInterface
->isValid() && !preferredInterface
->lastError().isValid()) {
148 dolphinInterfaces
.append(qMakePair(preferredInterface
, QStringList()));
152 // Look for dolphin instances among all available dbus services.
153 QDBusConnectionInterface
*sessionInterface
= QDBusConnection::sessionBus().interface();
154 const QStringList dbusServices
= sessionInterface
? sessionInterface
->registeredServiceNames().value() : QStringList();
155 // Don't match the service without trailing "-" (unique instance)
156 const QString pattern
= QStringLiteral("org.kde.dolphin-");
157 // Don't match the pid without leading "-"
158 const QString myPid
= QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
159 for (const QString
&service
: dbusServices
) {
160 if (service
.startsWith(pattern
) && !service
.endsWith(myPid
)) {
161 // Check if instance can handle our URLs
162 QSharedPointer
<OrgKdeDolphinMainWindowInterface
> interface(
163 new OrgKdeDolphinMainWindowInterface(service
, QStringLiteral("/dolphin/Dolphin_1"), QDBusConnection::sessionBus()));
164 if (interface
->isValid() && !interface
->lastError().isValid()) {
165 dolphinInterfaces
.append(qMakePair(interface
, QStringList()));
170 return dolphinInterfaces
;
173 QPair
<QString
, Qt::SortOrder
> Dolphin::sortOrderForUrl(QUrl
&url
)
175 ViewProperties
globalProps(url
);
176 return QPair
<QString
, Qt::SortOrder
>(globalProps
.sortRole(), globalProps
.sortOrder());
179 double GlobalConfig::animationDurationFactor()
181 if (s_animationDurationFactor
>= 0.0) {
182 return s_animationDurationFactor
;
184 // This is the first time this method is called.
185 auto kdeGlobalsConfig
= KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
186 updateAnimationDurationFactor(kdeGlobalsConfig
, {"AnimationDurationFactor"});
188 KConfigWatcher::Ptr configWatcher
= KConfigWatcher::create(KSharedConfig::openConfig());
189 connect(configWatcher
.data(), &KConfigWatcher::configChanged
, &GlobalConfig::updateAnimationDurationFactor
);
190 return s_animationDurationFactor
;
193 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup
&group
, const QByteArrayList
&names
)
195 if (group
.name() == QLatin1String("KDE") && names
.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
196 s_animationDurationFactor
= std::max(0.0, group
.readEntry("AnimationDurationFactor", 1.0));
200 double GlobalConfig::s_animationDurationFactor
= -1.0;
202 #include "moc_global.cpp"