]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
Merge branch 'release/20.08'
[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
13 #include <KDialogJobUiDelegate>
14 #include <KIO/ApplicationLauncherJob>
15 #include <KService>
16 #include <KWindowSystem>
17
18 #include <QApplication>
19 #include <QIcon>
20
21 QList<QUrl> Dolphin::validateUris(const QStringList& uriList)
22 {
23 const QString currentDir = QDir::currentPath();
24 QList<QUrl> urls;
25 foreach (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 // TODO: once Wayland clients can raise or activate themselves remove check from conditional
64 if (KWindowSystem::isPlatformWayland() || inputUrls.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
65 return false;
66 }
67
68 auto dolphinInterfaces = dolphinGuiInstances(preferredService);
69 if (dolphinInterfaces.isEmpty()) {
70 return false;
71 }
72
73 QStringList newUrls;
74
75 // check to see if any instances already have any of the given URLs open
76 const auto urls = QUrl::toStringList(inputUrls);
77 for (const QString& url : urls) {
78 bool urlFound = false;
79 for (auto& interface: dolphinInterfaces) {
80 auto isUrlOpenReply = interface.first->isUrlOpen(url);
81 isUrlOpenReply.waitForFinished();
82 if (!isUrlOpenReply.isError() && isUrlOpenReply.value()) {
83 interface.second.append(url);
84 urlFound = true;
85 break;
86 }
87 }
88 if (!urlFound) {
89 newUrls.append(url);
90 }
91 }
92 dolphinInterfaces.front().second << newUrls;
93
94 for (const auto& interface: qAsConst(dolphinInterfaces)) {
95 if (!interface.second.isEmpty()) {
96 auto reply = openFiles ? interface.first->openFiles(interface.second, splitView) : interface.first->openDirectories(interface.second, splitView);
97 reply.waitForFinished();
98 if (!reply.isError()) {
99 interface.first->activateWindow();
100 attached = true;
101 }
102 }
103 }
104 return attached;
105 }
106
107 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QString& preferredService)
108 {
109 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> dolphinInterfaces;
110 if (!preferredService.isEmpty()) {
111 QSharedPointer<OrgKdeDolphinMainWindowInterface> preferredInterface(
112 new OrgKdeDolphinMainWindowInterface(preferredService,
113 QStringLiteral("/dolphin/Dolphin_1"),
114 QDBusConnection::sessionBus()));
115 if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) {
116 dolphinInterfaces.append(qMakePair(preferredInterface, QStringList()));
117 }
118 }
119
120 // Look for dolphin instances among all available dbus services.
121 const QStringList dbusServices = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
122 // Don't match the service without trailing "-" (unique instance)
123 const QString pattern = QStringLiteral("org.kde.dolphin-");
124 // Don't match the pid without leading "-"
125 const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
126 for (const QString& service : dbusServices) {
127 if (service.startsWith(pattern) && !service.endsWith(myPid)) {
128 // Check if instance can handle our URLs
129 QSharedPointer<OrgKdeDolphinMainWindowInterface> interface(
130 new OrgKdeDolphinMainWindowInterface(service,
131 QStringLiteral("/dolphin/Dolphin_1"),
132 QDBusConnection::sessionBus()));
133 if (interface->isValid() && !interface->lastError().isValid()) {
134 dolphinInterfaces.append(qMakePair(interface, QStringList()));
135 }
136 }
137 }
138
139 return dolphinInterfaces;
140 }