]> 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
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() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
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 const auto urls = QUrl::toStringList(inputUrls);
85 for (const QString& url : urls) {
86 bool urlFound = false;
87
88 // looping through the windows starting from the active one
89 int i = activeWindowIndex;
90 do {
91 auto &interface = dolphinInterfaces[i];
92
93 auto isUrlOpenReply = openFiles ? interface.first->isUrlOrParentOpen(url) : interface.first->isUrlOpen(url);
94 isUrlOpenReply.waitForFinished();
95 if (!isUrlOpenReply.isError() && isUrlOpenReply.value()) {
96 interface.second.append(url);
97 urlFound = true;
98 break;
99 }
100
101 i = (i + 1) % dolphinInterfaces.size();
102 }
103 while (i != activeWindowIndex);
104
105 if (!urlFound) {
106 dolphinInterfaces[activeWindowIndex].second.append(url);
107 }
108 }
109
110 for (const auto& interface: qAsConst(dolphinInterfaces)) {
111 if (interface.second.isEmpty()) {
112 continue;
113 }
114 auto reply = openFiles ?
115 interface.first->openFiles(interface.second, splitView) :
116 interface.first->openDirectories(interface.second, splitView);
117 reply.waitForFinished();
118 if (!reply.isError()) {
119 interface.first->activateWindow();
120 attached = true;
121 }
122 }
123 return attached;
124 }
125
126 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QString& preferredService)
127 {
128 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> dolphinInterfaces;
129 if (!preferredService.isEmpty()) {
130 QSharedPointer<OrgKdeDolphinMainWindowInterface> preferredInterface(
131 new OrgKdeDolphinMainWindowInterface(preferredService,
132 QStringLiteral("/dolphin/Dolphin_1"),
133 QDBusConnection::sessionBus()));
134 if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) {
135 dolphinInterfaces.append(qMakePair(preferredInterface, QStringList()));
136 }
137 }
138
139 // Look for dolphin instances among all available dbus services.
140 QDBusConnectionInterface *sessionInterface = QDBusConnection::sessionBus().interface();
141 const QStringList dbusServices = sessionInterface ? sessionInterface->registeredServiceNames().value() : QStringList();
142 // Don't match the service without trailing "-" (unique instance)
143 const QString pattern = QStringLiteral("org.kde.dolphin-");
144 // Don't match the pid without leading "-"
145 const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
146 for (const QString& service : dbusServices) {
147 if (service.startsWith(pattern) && !service.endsWith(myPid)) {
148 // Check if instance can handle our URLs
149 QSharedPointer<OrgKdeDolphinMainWindowInterface> interface(
150 new OrgKdeDolphinMainWindowInterface(service,
151 QStringLiteral("/dolphin/Dolphin_1"),
152 QDBusConnection::sessionBus()));
153 if (interface->isValid() && !interface->lastError().isValid()) {
154 dolphinInterfaces.append(qMakePair(interface, QStringList()));
155 }
156 }
157 }
158
159 return dolphinInterfaces;
160 }
161
162 QPair<QString, Qt::SortOrder> Dolphin::sortOrderForUrl(QUrl &url)
163 {
164 ViewProperties globalProps(url);
165 return QPair<QString, Qt::SortOrder>(globalProps.sortRole(), globalProps.sortOrder());
166 }
167
168 double GlobalConfig::animationDurationFactor()
169 {
170 if (s_animationDurationFactor >= 0.0) {
171 return s_animationDurationFactor;
172 }
173 // This is the first time this method is called.
174 auto kdeGlobalsConfig = KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
175 updateAnimationDurationFactor(kdeGlobalsConfig, {"AnimationDurationFactor"});
176
177 KConfigWatcher::Ptr configWatcher = KConfigWatcher::create(KSharedConfig::openConfig());
178 connect(configWatcher.data(), &KConfigWatcher::configChanged,
179 &GlobalConfig::updateAnimationDurationFactor);
180 return s_animationDurationFactor;
181 }
182
183 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup &group, const QByteArrayList &names)
184 {
185 if (group.name() == QLatin1String("KDE") &&
186 names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
187 s_animationDurationFactor = std::max(0.0,
188 group.readEntry("AnimationDurationFactor", 1.0));
189 }
190 }
191
192 double GlobalConfig::s_animationDurationFactor = -1.0;