]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
GIT_SILENT Update Appstream for new release
[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,
61 bool openFiles,
62 bool splitView,
63 const QString &preferredService,
64 const QString &activationToken)
65 {
66 bool attached = false;
67
68 if (inputUrls.isEmpty()) {
69 return false;
70 }
71
72 auto dolphinInterfaces = dolphinGuiInstances(preferredService);
73 if (dolphinInterfaces.isEmpty()) {
74 return false;
75 }
76
77 int activeWindowIndex = -1;
78 for (const auto &interface : std::as_const(dolphinInterfaces)) {
79 ++activeWindowIndex;
80
81 auto isActiveWindowReply = interface.first->isActiveWindow();
82 isActiveWindowReply.waitForFinished();
83 if (!isActiveWindowReply.isError() && isActiveWindowReply.value()) {
84 break;
85 }
86 }
87
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();
93
94 // looping through the windows starting from the active one
95 int i = activeWindowIndex;
96 do {
97 auto &interface = dolphinInterfaces[i];
98
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);
103 urlFound = true;
104 break;
105 }
106
107 i = (i + 1) % dolphinInterfaces.size();
108 } while (i != activeWindowIndex);
109
110 if (!urlFound) {
111 if (GeneralSettings::openExternallyCalledFolderInNewTab()) {
112 dolphinInterfaces[activeWindowIndex].second.append(urlString);
113 } else {
114 newWindowURLs.append(url);
115 }
116 }
117 }
118
119 for (const auto &interface : std::as_const(dolphinInterfaces)) {
120 if (interface.second.isEmpty()) {
121 continue;
122 }
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);
127 attached = true;
128 }
129 }
130 if (attached && !newWindowURLs.isEmpty()) {
131 if (openFiles) {
132 openNewWindow(newWindowURLs, nullptr, Dolphin::OpenNewWindowFlag::Select);
133 } else {
134 openNewWindow(newWindowURLs);
135 }
136 }
137
138 return attached;
139 }
140
141 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QString &preferredService)
142 {
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()));
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, QStringLiteral("/dolphin/Dolphin_1"), QDBusConnection::sessionBus()));
164 if (interface->isValid() && !interface->lastError().isValid()) {
165 dolphinInterfaces.append(qMakePair(interface, QStringList()));
166 }
167 }
168 }
169
170 return dolphinInterfaces;
171 }
172
173 QPair<QString, Qt::SortOrder> Dolphin::sortOrderForUrl(QUrl &url)
174 {
175 ViewProperties globalProps(url);
176 return QPair<QString, Qt::SortOrder>(globalProps.sortRole(), globalProps.sortOrder());
177 }
178
179 double GlobalConfig::animationDurationFactor()
180 {
181 if (s_animationDurationFactor >= 0.0) {
182 return s_animationDurationFactor;
183 }
184 // This is the first time this method is called.
185 auto kdeGlobalsConfig = KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
186 updateAnimationDurationFactor(kdeGlobalsConfig, {"AnimationDurationFactor"});
187
188 KConfigWatcher::Ptr configWatcher = KConfigWatcher::create(KSharedConfig::openConfig());
189 connect(configWatcher.data(), &KConfigWatcher::configChanged, &GlobalConfig::updateAnimationDurationFactor);
190 return s_animationDurationFactor;
191 }
192
193 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup &group, const QByteArrayList &names)
194 {
195 if (group.name() == QLatin1String("KDE") && names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
196 s_animationDurationFactor = std::max(0.0, group.readEntry("AnimationDurationFactor", 1.0));
197 }
198 }
199
200 double GlobalConfig::s_animationDurationFactor = -1.0;
201
202 #include "moc_global.cpp"