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