Changeset View
Standalone View
src/global.cpp
Show All 17 Lines | |||||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "global.h" | 20 | #include "global.h" | ||
21 | 21 | | |||
22 | #include "dolphin_generalsettings.h" | 22 | #include "dolphin_generalsettings.h" | ||
23 | #include "dolphindebug.h" | 23 | #include "dolphindebug.h" | ||
24 | 24 | | |||
25 | #include <KRun> | 25 | #include <KRun> | ||
26 | #include <KWindowSystem> | ||||
26 | 27 | | |||
27 | #include <QApplication> | 28 | #include <QApplication> | ||
28 | #include <QIcon> | 29 | #include <QIcon> | ||
30 | #include <QDBusInterface> | ||||
31 | #include <QDBusConnectionInterface> | ||||
29 | 32 | | |||
30 | QList<QUrl> Dolphin::validateUris(const QStringList& uriList) | 33 | QList<QUrl> Dolphin::validateUris(const QStringList& uriList) | ||
31 | { | 34 | { | ||
32 | const QString currentDir = QDir::currentPath(); | 35 | const QString currentDir = QDir::currentPath(); | ||
33 | QList<QUrl> urls; | 36 | QList<QUrl> urls; | ||
34 | foreach (const QString& str, uriList) { | 37 | foreach (const QString& str, uriList) { | ||
35 | const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile); | 38 | const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile); | ||
36 | if (url.isValid()) { | 39 | if (url.isValid()) { | ||
37 | urls.append(url); | 40 | urls.append(url); | ||
38 | } else { | 41 | } else { | ||
39 | qCWarning(DolphinDebug) << "Invalid URL: " << str; | 42 | qCWarning(DolphinDebug) << "Invalid URL: " << str; | ||
40 | } | 43 | } | ||
41 | } | 44 | } | ||
42 | return urls; | 45 | return urls; | ||
43 | } | 46 | } | ||
44 | 47 | | |||
45 | QUrl Dolphin::homeUrl() | 48 | QUrl Dolphin::homeUrl() | ||
46 | { | 49 | { | ||
47 | return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile); | 50 | return QUrl::fromUserInput(GeneralSettings::homeUrl(), QString(), QUrl::AssumeLocalFile); | ||
48 | } | 51 | } | ||
49 | 52 | | |||
50 | void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags) | 53 | void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const OpenNewWindowFlags &flags) | ||
51 | { | 54 | { | ||
52 | QString command = QStringLiteral("dolphin"); | 55 | QString command = QStringLiteral("dolphin --new-window"); | ||
53 | 56 | | |||
elvisangelaccio: Please use `QStringLiteral("dolphin --new-window")` to save a string concatenation. | |||||
54 | if (flags.testFlag(OpenNewWindowFlag::Select)) { | 57 | if (flags.testFlag(OpenNewWindowFlag::Select)) { | ||
55 | command.append(QLatin1String(" --select")); | 58 | command.append(QLatin1String(" --select")); | ||
56 | } | 59 | } | ||
57 | 60 | | |||
58 | if (!urls.isEmpty()) { | 61 | if (!urls.isEmpty()) { | ||
59 | command.append(QLatin1String(" %U")); | 62 | command.append(QLatin1String(" %U")); | ||
60 | } | 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>& urls, bool openFiles, bool splitView, const QString& preferredService) | ||||
74 | { | ||||
75 | if (KWindowSystem::isPlatformWayland()) { | ||||
76 | // TODO: once Wayland clients can raise or activate themselves remove this conditional | ||||
77 | return false; | ||||
As Wayland and X11 clients can be part of the same session and thus the same DBus, it would be more accurate to ask the running dolphin whether it's using Wayland or XCB. It would require a new DBus call though, so probably too complex and not worth it though. fvogt: As Wayland and X11 clients can be part of the same session and thus the same DBus, it would be… | |||||
78 | } | ||||
79 | | ||||
80 | const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value(); | ||||
81 | | ||||
82 | // Don't match the service without trailing "-" (unique instance) | ||||
83 | const QString pattern = QStringLiteral("org.kde.dolphin-"); | ||||
84 | const QString myPid = QString::number(QCoreApplication::applicationPid()); | ||||
fvogt: `!preferredService.isEmpty()` | |||||
Prepend '-' to myPid so it does not accidentally skip pid {1123, 2123, ...} when the applicationPid is 123. (service.endsWith(myPid) on line 99) thomasp: Prepend '-' to myPid so it does not accidentally skip pid {1123, 2123, ...} when the… | |||||
85 | QVector<QPair<QSharedPointer<QDBusInterface>, QStringList>> dolphinServices; | ||||
86 | if (!preferredService.isEmpty()) { | ||||
87 | QSharedPointer<QDBusInterface> preferred( | ||||
88 | new QDBusInterface(preferredService, | ||||
89 | QStringLiteral("/dolphin/Dolphin_1"), | ||||
90 | QStringLiteral("org.kde.dolphin.MainWindow")) | ||||
91 | ); | ||||
92 | if (preferred->isValid()) { | ||||
93 | dolphinServices.append(qMakePair(preferred, QStringList() )); | ||||
94 | } | ||||
95 | } | ||||
96 | | ||||
97 | // find all dolphin instances | ||||
elvisangelaccio: Coding style: missing space before the colon. | |||||
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()) { | ||||
107 | continue; | ||||
108 | } | ||||
109 | dolphinServices.append(qMakePair(instance, QStringList())); | ||||
110 | } | ||||
111 | } | ||||
112 | | ||||
113 | if (dolphinServices.isEmpty()) { | ||||
114 | return false; | ||||
115 | } | ||||
61 | 116 | | |||
62 | KRun::run(command, urls, window, qApp->applicationDisplayName(), qApp->windowIcon().name()); | 117 | QStringList newUrls; | ||
118 | | ||||
119 | // check to see if any instances already have any of the given URLs open | ||||
120 | for (const QString& url : QUrl::toStringList(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()) { | ||||
fvogt: `if (isUrlOpen.isValid() && isUrlOpen.value()) {` | |||||
This might be dangerous? The wording of the docs doesn't make it too clear: https://doc.qt.io/qt-5/qdbusreply.html#value feverfew: This might be dangerous? The wording of the docs doesn't make it too clear: https://doc.qt. | |||||
Even though isUrlOpen.value() may give an "undefined" value, it doesn't matter as isUrlOpen.isValid() will be false. feverfew: Even though `isUrlOpen.value()` may give an "undefined" value, it doesn't matter as `isUrlOpen. | |||||
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 | } | ||||
IMO this function should take a const QList<QUrl> &urls parameter and only convert it to a QStringList here for the call. fvogt: IMO this function should take a `const QList<QUrl> &urls` parameter and only convert it to a… | |||||
141 | } | ||||
142 | return true; | ||||
63 | } | 143 | } | ||
elvisangelaccio: Brace should start at the end of the previous line | |||||
elvisangelaccio: Brace should start at the end of the previous line | |||||
elvisangelaccio: `QCoreApplication::applicationPid()` | |||||
elvisangelaccio: Missing braces | |||||
elvisangelaccio: Why not use `QPair` instead of defining this struct ? |
Please use QStringLiteral("dolphin --new-window") to save a string concatenation.