Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | #include "qgenericunixservices_p.h" | - |
41 | | - |
42 | #include <QtCore/QStandardPaths> | - |
43 | #include <QtCore/QProcess> | - |
44 | #include <QtCore/QUrl> | - |
45 | #include <QtCore/QDebug> | - |
46 | | - |
47 | #include <stdlib.h> | - |
48 | | - |
49 | QT_BEGIN_NAMESPACE | - |
50 | | - |
51 | #ifndef QT_NO_MULTIPROCESS | - |
52 | | - |
53 | enum { debug = 0 }; | - |
54 | | - |
55 | static inline QByteArray detectDesktopEnvironment() | - |
56 | { | - |
57 | const QByteArray xdgCurrentDesktop = qgetenv("XDG_CURRENT_DESKTOP"); | - |
58 | if (!xdgCurrentDesktop.isEmpty()) | - |
59 | return xdgCurrentDesktop.toUpper(); | - |
60 | | - |
61 | | - |
62 | if (!qEnvironmentVariableIsEmpty("KDE_FULL_SESSION")) | - |
63 | return QByteArrayLiteral("KDE"); | - |
64 | if (!qEnvironmentVariableIsEmpty("GNOME_DESKTOP_SESSION_ID")) | - |
65 | return QByteArrayLiteral("GNOME"); | - |
66 | | - |
67 | | - |
68 | const QByteArray desktopSession = qgetenv("DESKTOP_SESSION"); | - |
69 | if (desktopSession == "gnome") | - |
70 | return QByteArrayLiteral("GNOME"); | - |
71 | if (desktopSession == "xfce") | - |
72 | return QByteArrayLiteral("XFCE"); | - |
73 | | - |
74 | return QByteArrayLiteral("UNKNOWN"); | - |
75 | } | - |
76 | | - |
77 | static inline bool checkExecutable(const QString &candidate, QString *result) | - |
78 | { | - |
79 | *result = QStandardPaths::findExecutable(candidate); | - |
80 | return !result->isEmpty(); | - |
81 | } | - |
82 | | - |
83 | static inline bool detectWebBrowser(const QByteArray &desktop, | - |
84 | bool checkBrowserVariable, | - |
85 | QString *browser) | - |
86 | { | - |
87 | const char *browsers[] = {"google-chrome", "firefox", "mozilla", "opera"}; | - |
88 | | - |
89 | browser->clear(); | - |
90 | if (checkExecutable(QStringLiteral("xdg-open"), browser))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
91 | return true; never executed: return true; | 0 |
92 | | - |
93 | if (checkBrowserVariable) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
94 | QByteArray browserVariable = qgetenv("DEFAULT_BROWSER"); | - |
95 | if (browserVariable.isEmpty())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
96 | browserVariable = qgetenv("BROWSER"); never executed: browserVariable = qgetenv("BROWSER"); | 0 |
97 | if (!browserVariable.isEmpty() && checkExecutable(QString::fromLocal8Bit(browserVariable), browser))TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
98 | return true; never executed: return true; | 0 |
99 | } never executed: end of block | 0 |
100 | | - |
101 | if (desktop == QByteArray("KDE")) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
102 | | - |
103 | if (checkExecutable(QStringLiteral("kfmclient"), browser)) { never executed: return qstring_literal_temp; TRUE | never evaluated | FALSE | never evaluated |
| 0 |
104 | browser->append(QStringLiteralQLatin1String(" exec")); | - |
105 | return true; never executed: return true; | 0 |
106 | } | - |
107 | } else if (desktop == QByteArray("GNOME")) { never executed: end of block TRUE | never evaluated | FALSE | never evaluated |
| 0 |
108 | if (checkExecutable(QStringLiteral("gnome-open"), browser)) never executed: return qstring_literal_temp; TRUE | never evaluated | FALSE | never evaluated |
| 0 |
109 | return true; never executed: return true; | 0 |
110 | } never executed: end of block | 0 |
111 | | - |
112 | for (size_t i = 0; i < sizeof(browsers)/sizeof(char *); ++i)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
113 | if (checkExecutable(QLatin1String(browsers[i]), browser))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
114 | return true; never executed: return true; | 0 |
115 | return false; never executed: return false; | 0 |
116 | } | - |
117 | | - |
118 | static inline bool launch(const QString &launcher, const QUrl &url) | - |
119 | { | - |
120 | const QString command = launcher + QLatin1Char(' ') + QLatin1String(url.toEncoded()); | - |
121 | if (debug)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
122 | qDebug("Launching %s", qPrintable(command)); never executed: QMessageLogger(__FILE__, 122, __PRETTY_FUNCTION__).debug("Launching %s", QString(command).toLocal8Bit().constData()); | 0 |
123 | #if defined(QT_NO_PROCESS) | - |
124 | const bool ok = ::system(qPrintable(command + QStringLiteralQLatin1String(" &"))); | - |
125 | #else | - |
126 | const bool ok = QProcess::startDetached(command); | - |
127 | #endif | - |
128 | if (!ok)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
129 | qWarning("Launch failed (%s)", qPrintable(command)); never executed: QMessageLogger(__FILE__, 129, __PRETTY_FUNCTION__).warning("Launch failed (%s)", QString(command).toLocal8Bit().constData()); | 0 |
130 | return ok; never executed: return ok; | 0 |
131 | } | - |
132 | | - |
133 | QByteArray QGenericUnixServices::desktopEnvironment() const | - |
134 | { | - |
135 | static const QByteArray result = detectDesktopEnvironment(); | - |
136 | return result; | - |
137 | } | - |
138 | | - |
139 | bool QGenericUnixServices::openUrl(const QUrl &url) | - |
140 | { | - |
141 | if (url.scheme() == QLatin1String("mailto")) | - |
142 | return openDocument(url); | - |
143 | | - |
144 | if (m_webBrowser.isEmpty() && !detectWebBrowser(desktopEnvironment(), true, &m_webBrowser)) { | - |
145 | qWarning("Unable to detect a web browser to launch '%s'", qPrintable(url.toString())); | - |
146 | return false; | - |
147 | } | - |
148 | return launch(m_webBrowser, url); | - |
149 | } | - |
150 | | - |
151 | bool QGenericUnixServices::openDocument(const QUrl &url) | - |
152 | { | - |
153 | if (m_documentLauncher.isEmpty() && !detectWebBrowser(desktopEnvironment(), false, &m_documentLauncher)) { | - |
154 | qWarning("Unable to detect a launcher for '%s'", qPrintable(url.toString())); | - |
155 | return false; | - |
156 | } | - |
157 | return launch(m_documentLauncher, url); | - |
158 | } | - |
159 | | - |
160 | #else | - |
161 | QByteArray QGenericUnixServices::desktopEnvironment() const | - |
162 | { | - |
163 | return QByteArrayLiteral("UNKNOWN"); | - |
164 | } | - |
165 | | - |
166 | bool QGenericUnixServices::openUrl(const QUrl &url) | - |
167 | { | - |
168 | qWarning("openUrl() not supported on this platform"); | - |
169 | return false; | - |
170 | } | - |
171 | | - |
172 | bool QGenericUnixServices::openDocument(const QUrl &url) | - |
173 | { | - |
174 | qWarning("openDocument() not supported on this platform"); | - |
175 | return false; | - |
176 | } | - |
177 | | - |
178 | #endif | - |
179 | | - |
180 | QT_END_NAMESPACE | - |
| | |