]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
Match style of if() condition used above
[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
25 #include <KRun>
26 #include <KWindowSystem>
27
28 #include <QApplication>
29 #include <QIcon>
30 #include <QDBusInterface>
31 #include <QDBusConnectionInterface>
32
33 QList<QUrl> Dolphin::validateUris(const QStringList& uriList)
34 {
35 const QString currentDir = QDir::currentPath();
36 QList<QUrl> urls;
37 foreach (const QString& str, uriList) {
38 const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile);
39 if (url.isValid()) {
40 urls.append(url);
41 } else {
42 qCWarning(DolphinDebug) << "Invalid URL: " << str;
43 }
44 }
45 return urls;
46 }
47
48 QUrl Dolphin::homeUrl()
49 {
50 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile);
51 }
52
53 void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags)
54 {
55 QString command = QStringLiteral("dolphin --new-window");
56
57 if (flags.testFlag(OpenNewWindowFlag::Select)) {
58 command.append(QLatin1String(" --select"));
59 }
60
61 if (!urls.isEmpty()) {
62 command.append(QLatin1String(" %U"));
63 }
64 KRun::run(
65 command,
66 urls,
67 window,
68 QApplication::applicationDisplayName(),
69 QApplication::windowIcon().name()
70 );
71 }
72
73 bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFiles, bool splitView, const QString& preferredService)
74 {
75 // TODO: once Wayland clients can raise or activate themselves remove check from conditional
76 if (KWindowSystem::isPlatformWayland() || inputUrls.isEmpty() || !GeneralSettings::openExternallyCalledFolderInNewTab()) {
77 return false;
78 }
79
80 QVector<QPair<QSharedPointer<QDBusInterface>, QStringList>> dolphinServices;
81 if (!preferredService.isEmpty()) {
82 QSharedPointer<QDBusInterface> preferred(
83 new QDBusInterface(preferredService,
84 QStringLiteral("/dolphin/Dolphin_1"),
85 QStringLiteral("org.kde.dolphin.MainWindow"))
86 );
87 if (preferred->isValid() && !preferred->lastError().isValid()) {
88 dolphinServices.append(qMakePair(preferred, QStringList()));
89 }
90 }
91
92 // find all dolphin instances
93 const QStringList services = 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 = QStringLiteral("-") + QString::number(QCoreApplication::applicationPid());
98 for (const QString& service : services) {
99 if (service.startsWith(pattern) && !service.endsWith(myPid)) {
100 // Check if instance can handle our URLs
101 QSharedPointer<QDBusInterface> instance(
102 new QDBusInterface(service,
103 QStringLiteral("/dolphin/Dolphin_1"),
104 QStringLiteral("org.kde.dolphin.MainWindow"))
105 );
106 if (instance->isValid() && !instance->lastError().isValid()) {
107 dolphinServices.append(qMakePair(instance, QStringList()));
108 }
109 }
110 }
111
112 if (dolphinServices.isEmpty()) {
113 return false;
114 }
115
116 QStringList newUrls;
117
118 // check to see if any instances already have any of the given URLs open
119 const auto urls = QUrl::toStringList(inputUrls);
120 for (const QString& url : urls) {
121 bool urlFound = false;
122 for (auto& service: dolphinServices) {
123 QDBusReply<bool> isUrlOpen = service.first->call(QStringLiteral("isUrlOpen"), url);
124 if (isUrlOpen.isValid() && isUrlOpen.value()) {
125 service.second.append(url);
126 urlFound = true;
127 break;
128 }
129 }
130 if (!urlFound) {
131 newUrls.append(url);
132 }
133 }
134 dolphinServices.front().second << newUrls;
135
136 for (const auto& service: dolphinServices) {
137 if (!service.second.isEmpty()) {
138 service.first->call(openFiles ? QStringLiteral("openFiles") : QStringLiteral("openDirectories"), service.second, splitView);
139 service.first->call(QStringLiteral("activateWindow"));
140 }
141 }
142 return true;
143 }