]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
c9ec6a1200036caaa1b7e6fb15bb6f516bc7e709
[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
19 #include <QApplication>
20
21 QList<QUrl> Dolphin::validateUris(const QStringList& uriList)
22 {
23 const QString currentDir = QDir::currentPath();
24 QList<QUrl> urls;
25 for (const QString& str : uriList) {
26 const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile);
27 if (url.isValid()) {
28 urls.append(url);
29 } else {
30 qCWarning(DolphinDebug) << "Invalid URL: " << str;
31 }
32 }
33 return urls;
34 }
35
36 QUrl Dolphin::homeUrl()
37 {
38 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile);
39 }
40
41 void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags)
42 {
43 QString command = QStringLiteral("dolphin --new-window");
44
45 if (flags.testFlag(OpenNewWindowFlag::Select)) {
46 command.append(QLatin1String(" --select"));
47 }
48
49 if (!urls.isEmpty()) {
50 command.append(QLatin1String(" %U"));
51 }
52 KService::Ptr service(new KService(QApplication::applicationDisplayName(), command, QApplication::windowIcon().name()));
53 auto *job = new KIO::ApplicationLauncherJob(service, window);
54 job->setUrls(urls);
55 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, window));
56 job->start();
57 }
58
59 bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFiles, bool splitView, const QString& preferredService)
60 {
61 bool attached = false;
62
63 if (inputUrls.isEmpty()) {
64 return false;
65 }
66
67 auto dolphinInterfaces = dolphinGuiInstances(preferredService);
68 if (dolphinInterfaces.isEmpty()) {
69 return false;
70 }
71
72 int activeWindowIndex = -1;
73 for (const auto& interface: qAsConst(dolphinInterfaces)) {
74 ++activeWindowIndex;
75
76 auto isActiveWindowReply = interface.first->isActiveWindow();
77 isActiveWindowReply.waitForFinished();
78 if (!isActiveWindowReply.isError() && isActiveWindowReply.value()) {
79 break;
80 }
81 }
82
83 // check to see if any instances already have any of the given URLs or their parents open
84 QList<QUrl> newWindowURLs;
85 for (const QUrl& url : inputUrls) {
86 bool urlFound = false;
87 const QString urlString = url.toString();
88
89 // looping through the windows starting from the active one
90 int i = activeWindowIndex;
91 do {
92 auto &interface = dolphinInterfaces[i];
93
94 auto isUrlOpenReply = openFiles ? interface.first->isItemVisibleInAnyView(urlString) : interface.first->isUrlOpen(urlString);
95 isUrlOpenReply.waitForFinished();
96 if (!isUrlOpenReply.isError() && isUrlOpenReply.value()) {
97 interface.second.append(urlString);
98 urlFound = true;
99 break;
100 }
101
102 i = (i + 1) % dolphinInterfaces.size();
103 }
104 while (i != activeWindowIndex);
105
106 if (!urlFound) {
107 if (GeneralSettings::openExternallyCalledFolderInNewTab()) {
108 dolphinInterfaces[activeWindowIndex].second.append(urlString);
109 } else {
110 newWindowURLs.append(url);
111 }
112 }
113 }
114
115 for (const auto& interface: qAsConst(dolphinInterfaces)) {
116 if (interface.second.isEmpty()) {
117 continue;
118 }
119 auto reply = openFiles ?
120 interface.first->openFiles(interface.second, splitView) :
121 interface.first->openDirectories(interface.second, splitView);
122 reply.waitForFinished();
123 if (!reply.isError()) {
124 interface.first->activateWindow();
125 attached = true;
126 }
127 }
128 if (attached && !newWindowURLs.isEmpty()) {
129 if (openFiles) {
130 openNewWindow(newWindowURLs, nullptr, Dolphin::OpenNewWindowFlag::Select);
131 } else {
132 openNewWindow(newWindowURLs);
133 }
134 }
135
136 return attached;
137 }
138
139 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QString& preferredService)
140 {
141 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> dolphinInterfaces;
142 if (!preferredService.isEmpty()) {
143 QSharedPointer<OrgKdeDolphinMainWindowInterface> preferredInterface(
144 new OrgKdeDolphinMainWindowInterface(preferredService,
145 QStringLiteral("/dolphin/Dolphin_1"),
146 QDBusConnection::sessionBus()));
147 if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) {
148 dolphinInterfaces.append(qMakePair(preferredInterface, QStringList()));
149 }
150 }
151
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,
164 QStringLiteral("/dolphin/Dolphin_1"),
165 QDBusConnection::sessionBus()));
166 if (interface->isValid() && !interface->lastError().isValid()) {
167 dolphinInterfaces.append(qMakePair(interface, QStringList()));
168 }
169 }
170 }
171
172 return dolphinInterfaces;
173 }
174
175 QPair<QString, Qt::SortOrder> Dolphin::sortOrderForUrl(QUrl &url)
176 {
177 ViewProperties globalProps(url);
178 return QPair<QString, Qt::SortOrder>(globalProps.sortRole(), globalProps.sortOrder());
179 }
180
181 double GlobalConfig::animationDurationFactor()
182 {
183 if (s_animationDurationFactor >= 0.0) {
184 return s_animationDurationFactor;
185 }
186 // This is the first time this method is called.
187 auto kdeGlobalsConfig = KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
188 updateAnimationDurationFactor(kdeGlobalsConfig, {"AnimationDurationFactor"});
189
190 KConfigWatcher::Ptr configWatcher = KConfigWatcher::create(KSharedConfig::openConfig());
191 connect(configWatcher.data(), &KConfigWatcher::configChanged,
192 &GlobalConfig::updateAnimationDurationFactor);
193 return s_animationDurationFactor;
194 }
195
196 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup &group, const QByteArrayList &names)
197 {
198 if (group.name() == QLatin1String("KDE") &&
199 names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
200 s_animationDurationFactor = std::max(0.0,
201 group.readEntry("AnimationDurationFactor", 1.0));
202 }
203 }
204
205 double GlobalConfig::s_animationDurationFactor = -1.0;