]> cloud.milkyroute.net Git - dolphin.git/blob - src/global.cpp
Fix selection rect after porting from QFontMetrics::width()
[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 <KDialogJobUiDelegate>
27 #include <KIO/ApplicationLauncherJob>
28 #include <KService>
29 #include <KWindowSystem>
30
31 #include <QApplication>
32 #include <QIcon>
33
34 QList<QUrl> Dolphin::validateUris(const QStringList& uriList)
35 {
36 const QString currentDir = QDir::currentPath();
37 QList<QUrl> urls;
38 foreach (const QString& str, uriList) {
39 const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile);
40 if (url.isValid()) {
41 urls.append(url);
42 } else {
43 qCWarning(DolphinDebug) << "Invalid URL: " << str;
44 }
45 }
46 return urls;
47 }
48
49 QUrl Dolphin::homeUrl()
50 {
51 return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile);
52 }
53
54 void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags)
55 {
56 QString command = QStringLiteral("dolphin --new-window");
57
58 if (flags.testFlag(OpenNewWindowFlag::Select)) {
59 command.append(QLatin1String(" --select"));
60 }
61
62 if (!urls.isEmpty()) {
63 command.append(QLatin1String(" %U"));
64 }
65 KService::Ptr service(new KService(QApplication::applicationDisplayName(), command, QApplication::windowIcon().name()));
66 auto *job = new KIO::ApplicationLauncherJob(service, window);
67 job->setUrls(urls);
68 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, window));
69 job->start();
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 auto dolphinInterfaces = dolphinGuiInstances(preferredService);
82 if (dolphinInterfaces.isEmpty()) {
83 return false;
84 }
85
86 QStringList newUrls;
87
88 // check to see if any instances already have any of the given URLs open
89 const auto urls = QUrl::toStringList(inputUrls);
90 for (const QString& url : urls) {
91 bool urlFound = false;
92 for (auto& interface: dolphinInterfaces) {
93 auto isUrlOpenReply = 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 if (!urlFound) {
102 newUrls.append(url);
103 }
104 }
105 dolphinInterfaces.front().second << newUrls;
106
107 for (const auto& interface: qAsConst(dolphinInterfaces)) {
108 if (!interface.second.isEmpty()) {
109 auto reply = openFiles ? interface.first->openFiles(interface.second, splitView) : interface.first->openDirectories(interface.second, splitView);
110 reply.waitForFinished();
111 if (!reply.isError()) {
112 interface.first->activateWindow();
113 attached = true;
114 }
115 }
116 }
117 return attached;
118 }
119
120 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QString& preferredService)
121 {
122 QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> dolphinInterfaces;
123 if (!preferredService.isEmpty()) {
124 QSharedPointer<OrgKdeDolphinMainWindowInterface> preferredInterface(
125 new OrgKdeDolphinMainWindowInterface(preferredService,
126 QStringLiteral("/dolphin/Dolphin_1"),
127 QDBusConnection::sessionBus()));
128 if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) {
129 dolphinInterfaces.append(qMakePair(preferredInterface, QStringList()));
130 }
131 }
132
133 // Look for dolphin instances among all available dbus services.
134 const QStringList dbusServices = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
135 // Don't match the service without trailing "-" (unique instance)
136 const QString pattern = QStringLiteral("org.kde.dolphin-");
137 // Don't match the pid without leading "-"
138 const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
139 for (const QString& service : dbusServices) {
140 if (service.startsWith(pattern) && !service.endsWith(myPid)) {
141 // Check if instance can handle our URLs
142 QSharedPointer<OrgKdeDolphinMainWindowInterface> interface(
143 new OrgKdeDolphinMainWindowInterface(service,
144 QStringLiteral("/dolphin/Dolphin_1"),
145 QDBusConnection::sessionBus()));
146 if (interface->isValid() && !interface->lastError().isValid()) {
147 dolphinInterfaces.append(qMakePair(interface, QStringList()));
148 }
149 }
150 }
151
152 return dolphinInterfaces;
153 }