|
3 | 3 | #include <QDesktopServices> |
4 | 4 | #include <QProcess> |
5 | 5 | #include <QDebug> |
| 6 | +#include "FileSystem.h" |
6 | 7 |
|
7 | | -/** |
8 | | - * This shouldn't exist, but until QTBUG-9328 and other unreported bugs are fixed, it needs to be a thing. |
9 | | - */ |
10 | | -#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) |
11 | | - |
12 | | -#include <unistd.h> |
13 | | -#include <errno.h> |
14 | | -#include <sys/types.h> |
15 | | -#include <sys/wait.h> |
16 | | - |
17 | | -template <typename T> |
18 | | -bool IndirectOpen(T callable, qint64 *pid_forked = nullptr) |
| 8 | +namespace DesktopServices { |
| 9 | +bool openDirectory(const QString &path) |
19 | 10 | { |
20 | | - auto pid = fork(); |
21 | | - if(pid_forked) |
22 | | - { |
23 | | - if(pid > 0) |
24 | | - *pid_forked = pid; |
25 | | - else |
26 | | - *pid_forked = 0; |
27 | | - } |
28 | | - if(pid == -1) |
29 | | - { |
30 | | - qWarning() << "IndirectOpen failed to fork: " << errno; |
31 | | - return false; |
32 | | - } |
33 | | - // child - do the stuff |
34 | | - if(pid == 0) |
35 | | - { |
36 | | - // unset all this garbage so it doesn't get passed to the child process |
37 | | - qunsetenv("LD_PRELOAD"); |
38 | | - qunsetenv("LD_LIBRARY_PATH"); |
39 | | - qunsetenv("LD_DEBUG"); |
40 | | - qunsetenv("QT_PLUGIN_PATH"); |
41 | | - qunsetenv("QT_FONTPATH"); |
| 11 | + QUrl url = QUrl::fromLocalFile(path); |
| 12 | + QString urlString = url.toString(QUrl::FullyEncoded); |
42 | 13 |
|
43 | | - // open the URL |
44 | | - auto status = callable(); |
| 14 | + qDebug() << "Opening directory" << path << "url" << urlString; |
45 | 15 |
|
46 | | - // detach from the parent process group. |
47 | | - setsid(); |
48 | | - |
49 | | - // die. now. do not clean up anything, it would just hang forever. |
50 | | - _exit(status ? 0 : 1); |
51 | | - } |
52 | | - else |
| 16 | + if(!FS::ensureFolderPathExists(path)) |
53 | 17 | { |
54 | | - //parent - assume it worked. |
55 | | - int status; |
56 | | - while (waitpid(pid, &status, 0)) |
57 | | - { |
58 | | - if(WIFEXITED(status)) |
59 | | - { |
60 | | - return WEXITSTATUS(status) == 0; |
61 | | - } |
62 | | - if(WIFSIGNALED(status)) |
63 | | - { |
64 | | - return false; |
65 | | - } |
66 | | - } |
67 | | - return true; |
| 18 | + qDebug() << "Failed to create directory for opening:" << path << "url" << urlString; |
| 19 | + return false; |
68 | 20 | } |
69 | | -} |
70 | | -#endif |
71 | 21 |
|
72 | | -namespace DesktopServices { |
73 | | -bool openDirectory(const QString &path, bool ensureExists) |
74 | | -{ |
75 | | - qDebug() << "Opening directory" << path; |
76 | | - QDir parentPath; |
77 | | - QDir dir(path); |
78 | | - if (!dir.exists()) |
79 | | - { |
80 | | - parentPath.mkpath(dir.absolutePath()); |
81 | | - } |
82 | | - auto f = [&]() |
83 | | - { |
84 | | - return QDesktopServices::openUrl(QUrl::fromLocalFile(dir.absolutePath())); |
85 | | - }; |
86 | 22 | #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) |
87 | | - return IndirectOpen(f); |
| 23 | + return QProcess::startDetached("xdg-open", QStringList() << urlString); |
88 | 24 | #else |
89 | | - return f(); |
| 25 | + return QDesktopServices::openUrl(url); |
90 | 26 | #endif |
91 | 27 | } |
92 | 28 |
|
93 | 29 | bool openFile(const QString &path) |
94 | 30 | { |
95 | | - qDebug() << "Opening file" << path; |
96 | | - auto f = [&]() |
97 | | - { |
98 | | - return QDesktopServices::openUrl(QUrl::fromLocalFile(path)); |
99 | | - }; |
| 31 | + QUrl url = QUrl::fromLocalFile(path); |
| 32 | + QString urlString = url.toString(QUrl::FullyEncoded); |
| 33 | + qDebug() << "Opening file" << path << " url " << urlString; |
| 34 | + |
100 | 35 | #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) |
101 | | - return IndirectOpen(f); |
| 36 | + return QProcess::startDetached("xdg-open", QStringList() << urlString); |
102 | 37 | #else |
103 | | - return f(); |
| 38 | + return QDesktopServices::openUrl(url); |
104 | 39 | #endif |
105 | 40 | } |
106 | 41 |
|
107 | 42 | bool openFile(const QString &application, const QString &path, const QString &workingDirectory, qint64 *pid) |
108 | 43 | { |
109 | 44 | qDebug() << "Opening file" << path << "using" << application; |
110 | | -#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) |
111 | | - // FIXME: the pid here is fake. So if something depends on it, it will likely misbehave |
112 | | - return IndirectOpen([&]() |
113 | | - { |
114 | | - return QProcess::startDetached(application, QStringList() << path, workingDirectory); |
115 | | - }, pid); |
116 | | -#else |
117 | 45 | return QProcess::startDetached(application, QStringList() << path, workingDirectory, pid); |
118 | | -#endif |
119 | 46 | } |
120 | 47 |
|
121 | 48 | bool run(const QString &application, const QStringList &args, const QString &workingDirectory, qint64 *pid) |
122 | 49 | { |
123 | 50 | qDebug() << "Running" << application << "with args" << args.join(' '); |
124 | | -#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) |
125 | | - // FIXME: the pid here is fake. So if something depends on it, it will likely misbehave |
126 | | - return IndirectOpen([&]() |
127 | | - { |
128 | | - return QProcess::startDetached(application, args, workingDirectory); |
129 | | - }, pid); |
130 | | -#else |
131 | 51 | return QProcess::startDetached(application, args, workingDirectory, pid); |
132 | | -#endif |
133 | 52 | } |
134 | 53 |
|
135 | 54 | bool openUrl(const QUrl &url) |
136 | 55 | { |
137 | | - qDebug() << "Opening URL" << url.toString(); |
138 | | - auto f = [&]() |
139 | | - { |
140 | | - return QDesktopServices::openUrl(url); |
141 | | - }; |
| 56 | + QString urlString = url.toString(QUrl::FullyEncoded); |
| 57 | + qDebug() << "Opening URL" << urlString; |
142 | 58 | #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) |
143 | | - return IndirectOpen(f); |
| 59 | + return QProcess::startDetached("xdg-open", QStringList() << urlString); |
144 | 60 | #else |
145 | | - return f(); |
| 61 | + return QDesktopServices::openUrl(url); |
146 | 62 | #endif |
147 | 63 | } |
148 | 64 |
|
|
0 commit comments