]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
Fix Wayland window activation when attaching to an existing instance
[dolphin.git] / src / global.cpp
1 /*
2 * SPDX-FileCopyrightText: 2015 Ashish Bansal <bansal.ashish096@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "global.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "dolphindebug.h"
11 #include "dolphinmainwindowinterface.h"
12 #include "views/viewproperties.h"
13
14 #include <KConfigWatcher>
15 #include <KDialogJobUiDelegate>
16 #include <KIO/ApplicationLauncherJob>
17 #include <KService>
18 #include <KWindowSystem>
19
20 #include <QApplication>
21
22 QList<QUrl> Dolphin::validateUris(const QStringList& uriList)
23 {
24 const QString currentDir = QDir::currentPath();
25 QList<QUrl> urls;
26 for (const QString& str : uriList) {
27 const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile);
28 if (url.isValid()) {
29 urls.append(url);
30 } else {
31 qCWarning(DolphinDebug) << "Invalid URL: " << str;
32 }
33 }
34 return urls;
35 }
36
37 QUrl Dolphin::homeUrl()
38 {
39 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile);
40 }
41
42 void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags)
43 {
44 QString command = QStringLiteral("dolphin --new-window");
45
46 if (flags.testFlag(OpenNewWindowFlag::Select)) {
47 command.append(QLatin1String(" --select"));
48 }
49
50 if (!urls.isEmpty()) {
51 command.append(QLatin1String(" %U"));
52 }
53 KService::Ptr service(new KService(QApplication::applicationDisplayName(), command, QApplication::windowIcon().name()));
54 auto *job = new KIO::ApplicationLauncherJob(service, window);
55 job->setUrls(urls);
56 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, window));
57 job->start();
58 }
59
60 bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFiles, bool splitView, const QString& preferredService, const QString &activationToken)
61 {
62 bool attached = false;
63
64 if (inputUrls.isEmpty()) {
65 return false;
66 }
67
68 auto dolphinInterfaces = dolphinGuiInstances(preferredService);
69 if (dolphinInterfaces.isEmpty()) {
70 return false;
71 }
72
73 int activeWindowIndex = -1;
74 for (const auto& interface: qAsConst(dolphinInterfaces)) {
75 ++activeWindowIndex;
76
77 auto isActiveWindowReply = interface.first->isActiveWindow();
78 isActiveWindowReply.waitForFinished();
79 if (!isActiveWindowReply.isError() && isActiveWindowReply.value()) {
80 break;
81 }
82 }
83
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();
89
90 // looping through the windows starting from the active one
91 int i = activeWindowIndex;
92 do {
93 auto &interface = dolphinInterfaces[i];
94
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);
99 urlFound = true;
100 break;
101 }
102
103 i = (i + 1) % dolphinInterfaces.size();
104 }
105 while (i != activeWindowIndex);
106
107 if (!urlFound) {
108 if (GeneralSettings::openExternallyCalledFolderInNewTab()) {
109 dolphinInterfaces[activeWindowIndex].second.append(urlString);
110 } else {
111 newWindowURLs.append(url);
112 }
113 }
114 }
115
116 for (const auto& interface: qAsConst(dolphinInterfaces)) {
117 if (interface.second.isEmpty()) {
118 continue;
119 }
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);
126 attached = true;
127 }
128 }
129 if (attached && !newWindowURLs.isEmpty()) {
130 if (openFiles) {
131 openNewWindow(newWindowURLs, nullptr, Dolphin::OpenNewWindowFlag::Select);
132 } else {
133 openNewWindow(newWindowURLs);
134 }
135 }
136
137 return attached;
138 }
139
140 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QString& preferredService)
141 {
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()));
150 }
151 }
152
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()));
169 }
170 }
171 }
172
173 return dolphinInterfaces;
174 }
175
176 QPair<QString, Qt::SortOrder> Dolphin::sortOrderForUrl(QUrl &url)
177 {
178 ViewProperties globalProps(url);
179 return QPair<QString, Qt::SortOrder>(globalProps.sortRole(), globalProps.sortOrder());
180 }
181
182 double GlobalConfig::animationDurationFactor()
183 {
184 if (s_animationDurationFactor >= 0.0) {
185 return s_animationDurationFactor;
186 }
187 // This is the first time this method is called.
188 auto kdeGlobalsConfig = KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
189 updateAnimationDurationFactor(kdeGlobalsConfig, {"AnimationDurationFactor"});
190
191 KConfigWatcher::Ptr configWatcher = KConfigWatcher::create(KSharedConfig::openConfig());
192 connect(configWatcher.data(), &KConfigWatcher::configChanged,
193 &GlobalConfig::updateAnimationDurationFactor);
194 return s_animationDurationFactor;
195 }
196
197 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup &group, const QByteArrayList &names)
198 {
199 if (group.name() == QLatin1String("KDE") &&
200 names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
201 s_animationDurationFactor = std::max(0.0,
202 group.readEntry("AnimationDurationFactor", 1.0));
203 }
204 }
205
206 double GlobalConfig::s_animationDurationFactor = -1.0;