]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
Keep working towards a reviewable state
[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 #include <QIcon>
22
23 QList<QUrl> Dolphin::validateUris(const QStringList& uriList)
24 {
25 const QString currentDir = QDir::currentPath();
26 QList<QUrl> urls;
27 for (const QString& str : uriList) {
28 const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile);
29 if (url.isValid()) {
30 urls.append(url);
31 } else {
32 qCWarning(DolphinDebug) << "Invalid URL: " << str;
33 }
34 }
35 return urls;
36 }
37
38 QUrl Dolphin::homeUrl()
39 {
40 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile);
41 }
42
43 void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags)
44 {
45 QString command = QStringLiteral("dolphin --new-window");
46
47 if (flags.testFlag(OpenNewWindowFlag::Select)) {
48 command.append(QLatin1String(" --select"));
49 }
50
51 if (!urls.isEmpty()) {
52 command.append(QLatin1String(" %U"));
53 }
54 KService::Ptr service(new KService(QApplication::applicationDisplayName(), command, QApplication::windowIcon().name()));
55 auto *job = new KIO::ApplicationLauncherJob(service, window);
56 job->setUrls(urls);
57 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, window));
58 job->start();
59 }
60
61 bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFiles, bool splitView, const QString& preferredService)
62 {
63 bool attached = false;
64
65 if (inputUrls.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
66 return false;
67 }
68
69 auto dolphinInterfaces = dolphinGuiInstances(preferredService);
70 if (dolphinInterfaces.isEmpty()) {
71 return false;
72 }
73
74 int activeWindowIndex = -1;
75 for (const auto& interface: qAsConst(dolphinInterfaces)) {
76 ++activeWindowIndex;
77
78 auto isActiveWindowReply = interface.first->isActiveWindow();
79 isActiveWindowReply.waitForFinished();
80 if (!isActiveWindowReply.isError() && isActiveWindowReply.value()) {
81 break;
82 }
83 }
84
85 // check to see if any instances already have any of the given URLs or their parents open
86 const auto urls = QUrl::toStringList(inputUrls);
87 for (const QString& url : urls) {
88 bool urlFound = false;
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->isUrlOrParentOpen(url) : interface.first->isUrlOpen(url);
96 isUrlOpenReply.waitForFinished();
97 if (!isUrlOpenReply.isError() && isUrlOpenReply.value()) {
98 interface.second.append(url);
99 urlFound = true;
100 break;
101 }
102
103 i = (i + 1) % dolphinInterfaces.size();
104 }
105 while (i != activeWindowIndex);
106
107 if (!urlFound) {
108 dolphinInterfaces[activeWindowIndex].second.append(url);
109 }
110 }
111
112 for (const auto& interface: qAsConst(dolphinInterfaces)) {
113 if (interface.second.isEmpty()) {
114 continue;
115 }
116 auto reply = openFiles ?
117 interface.first->openFiles(interface.second, splitView) :
118 interface.first->openDirectories(interface.second, splitView);
119 reply.waitForFinished();
120 if (!reply.isError()) {
121 interface.first->activateWindow();
122 attached = true;
123 }
124 }
125 return attached;
126 }
127
128 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QString& preferredService)
129 {
130 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> dolphinInterfaces;
131 if (!preferredService.isEmpty()) {
132 QSharedPointer<OrgKdeDolphinMainWindowInterface> preferredInterface(
133 new OrgKdeDolphinMainWindowInterface(preferredService,
134 QStringLiteral("/dolphin/Dolphin_1"),
135 QDBusConnection::sessionBus()));
136 if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) {
137 dolphinInterfaces.append(qMakePair(preferredInterface, QStringList()));
138 }
139 }
140
141 // Look for dolphin instances among all available dbus services.
142 QDBusConnectionInterface *sessionInterface = QDBusConnection::sessionBus().interface();
143 const QStringList dbusServices = sessionInterface ? sessionInterface->registeredServiceNames().value() : QStringList();
144 // Don't match the service without trailing "-" (unique instance)
145 const QString pattern = QStringLiteral("org.kde.dolphin-");
146 // Don't match the pid without leading "-"
147 const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
148 for (const QString& service : dbusServices) {
149 if (service.startsWith(pattern) && !service.endsWith(myPid)) {
150 // Check if instance can handle our URLs
151 QSharedPointer<OrgKdeDolphinMainWindowInterface> interface(
152 new OrgKdeDolphinMainWindowInterface(service,
153 QStringLiteral("/dolphin/Dolphin_1"),
154 QDBusConnection::sessionBus()));
155 if (interface->isValid() && !interface->lastError().isValid()) {
156 dolphinInterfaces.append(qMakePair(interface, QStringList()));
157 }
158 }
159 }
160
161 return dolphinInterfaces;
162 }
163
164 QPair<QString, Qt::SortOrder> Dolphin::sortOrderForUrl(QUrl &url)
165 {
166 ViewProperties globalProps(url);
167 return QPair<QString, Qt::SortOrder>(globalProps.sortRole(), globalProps.sortOrder());
168 }
169
170 double GlobalConfig::animationDurationFactor()
171 {
172 if (s_animationDurationFactor >= 0.0) {
173 return s_animationDurationFactor;
174 }
175 // This is the first time this method is called.
176 auto kdeGlobalsConfig = KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
177 updateAnimationDurationFactor(kdeGlobalsConfig, {"AnimationDurationFactor"});
178
179 KConfigWatcher::Ptr configWatcher = KConfigWatcher::create(KSharedConfig::openConfig());
180 connect(configWatcher.data(), &KConfigWatcher::configChanged,
181 &GlobalConfig::updateAnimationDurationFactor);
182 return s_animationDurationFactor;
183 }
184
185 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup &group, const QByteArrayList &names)
186 {
187 if (group.name() == QLatin1String("KDE") &&
188 names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
189 s_animationDurationFactor = std::max(0.0,
190 group.readEntry("AnimationDurationFactor", 1.0));
191 }
192 }
193
194 double GlobalConfig::s_animationDurationFactor = -1.0;