]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
Dolphin Service Installer: Run uninstall scripts, rename methods
[dolphin.git] / src / global.cpp
1 /*
2 * Copyright 2015 Ashish Bansal<bansal.ashish096@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "global.h"
21
22 #include "dolphin_generalsettings.h"
23 #include "dolphindebug.h"
24 #include "dolphinmainwindowinterface.h"
25
26 #include <KRun>
27 #include <KWindowSystem>
28
29 #include <QApplication>
30 #include <QIcon>
31
32 QList<QUrl> Dolphin::validateUris(const QStringList& uriList)
33 {
34 const QString currentDir = QDir::currentPath();
35 QList<QUrl> urls;
36 foreach (const QString& str, uriList) {
37 const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile);
38 if (url.isValid()) {
39 urls.append(url);
40 } else {
41 qCWarning(DolphinDebug) << "Invalid URL: " << str;
42 }
43 }
44 return urls;
45 }
46
47 QUrl Dolphin::homeUrl()
48 {
49 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile);
50 }
51
52 void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags)
53 {
54 QString command = QStringLiteral("dolphin --new-window");
55
56 if (flags.testFlag(OpenNewWindowFlag::Select)) {
57 command.append(QLatin1String(" --select"));
58 }
59
60 if (!urls.isEmpty()) {
61 command.append(QLatin1String(" %U"));
62 }
63 KRun::run(
64 command,
65 urls,
66 window,
67 QApplication::applicationDisplayName(),
68 QApplication::windowIcon().name()
69 );
70 }
71
72 bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFiles, bool splitView, const QString& preferredService)
73 {
74 bool attached = false;
75
76 // TODO: once Wayland clients can raise or activate themselves remove check from conditional
77 if (KWindowSystem::isPlatformWayland() || inputUrls.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
78 return false;
79 }
80
81 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> dolphinInterfaces;
82 if (!preferredService.isEmpty()) {
83 QSharedPointer<OrgKdeDolphinMainWindowInterface> preferredInterface(
84 new OrgKdeDolphinMainWindowInterface(preferredService,
85 QStringLiteral("/dolphin/Dolphin_1"),
86 QDBusConnection::sessionBus()));
87 if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) {
88 dolphinInterfaces.append(qMakePair(preferredInterface, QStringList()));
89 }
90 }
91
92 // Look for dolphin instances among all available dbus services.
93 const QStringList dbusServices = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
94 // Don't match the service without trailing "-" (unique instance)
95 const QString pattern = QStringLiteral("org.kde.dolphin-");
96 // Don't match the pid without leading "-"
97 const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
98 for (const QString& service : dbusServices) {
99 if (service.startsWith(pattern) && !service.endsWith(myPid)) {
100 // Check if instance can handle our URLs
101 QSharedPointer<OrgKdeDolphinMainWindowInterface> interface(
102 new OrgKdeDolphinMainWindowInterface(service,
103 QStringLiteral("/dolphin/Dolphin_1"),
104 QDBusConnection::sessionBus()));
105 if (interface->isValid() && !interface->lastError().isValid()) {
106 dolphinInterfaces.append(qMakePair(interface, QStringList()));
107 }
108 }
109 }
110
111 if (dolphinInterfaces.isEmpty()) {
112 return false;
113 }
114
115 QStringList newUrls;
116
117 // check to see if any instances already have any of the given URLs open
118 const auto urls = QUrl::toStringList(inputUrls);
119 for (const QString& url : urls) {
120 bool urlFound = false;
121 for (auto& interface: dolphinInterfaces) {
122 auto isUrlOpenReply = interface.first->isUrlOpen(url);
123 isUrlOpenReply.waitForFinished();
124 if (!isUrlOpenReply.isError() && isUrlOpenReply.value()) {
125 interface.second.append(url);
126 urlFound = true;
127 break;
128 }
129 }
130 if (!urlFound) {
131 newUrls.append(url);
132 }
133 }
134 dolphinInterfaces.front().second << newUrls;
135
136 for (const auto& interface: dolphinInterfaces) {
137 if (!interface.second.isEmpty()) {
138 auto reply = openFiles ? interface.first->openFiles(interface.second, splitView) : interface.first->openDirectories(interface.second, splitView);
139 reply.waitForFinished();
140 if (!reply.isError()) {
141 interface.first->activateWindow();
142 attached = true;
143 }
144 }
145 }
146 return attached;
147 }