1 /***************************************************************************
2 * Copyright © 2019 Alexander Potashev <aspotashev@gmail.com> *
4 * This program is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU General Public License as *
6 * published by the Free Software Foundation; either version 2 of *
7 * the License or (at your option) version 3 or any later version *
8 * accepted by the membership of KDE e.V. (or its successor approved *
9 * by the membership of KDE e.V.), which shall act as a proxy *
10 * defined in Section 14 of version 3 of the license. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 ***************************************************************************/
23 #include <QStandardPaths>
25 #include <QDirIterator>
26 #include <QCommandLineParser>
27 #include <QMimeDatabase>
29 #include <QDesktopServices>
30 #include <QGuiApplication>
32 #include <KLocalizedString>
34 // @param msg Error that gets logged to CLI
35 Q_NORETURN
void fail(const QString
&str
)
40 auto args
= QStringList
{"--passivepopup", i18n("Dolphin service menu installation failed"), "15"};
41 process
.start("kdialog", args
, QIODevice::ReadOnly
);
42 if (!process
.waitForStarted()) {
43 qFatal("Failed to run kdialog");
49 QString
getServiceMenusDir()
51 const QString dataLocation
= QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation
);
52 return QDir(dataLocation
).absoluteFilePath("kservices5/ServiceMenus");
55 struct UncompressCommand
62 void runUncompress(const QString
&inputPath
, const QString
&outputPath
) {
63 QVector
<QPair
<QStringList
, UncompressCommand
>> mimeTypeToCommand
;
64 mimeTypeToCommand
.append({QStringList
{"application/x-tar", "application/tar", "application/x-gtar",
66 UncompressCommand
{"tar", QStringList() << "-xf", QStringList() << "-C"}});
67 mimeTypeToCommand
.append({QStringList
{"application/x-gzip", "application/gzip",
68 "application/x-gzip-compressed-tar", "application/gzip-compressed-tar",
69 "application/x-gzip-compressed", "application/gzip-compressed",
70 "application/tgz", "application/x-compressed-tar",
71 "application/x-compressed-gtar", "file/tgz",
72 "multipart/x-tar-gz", "application/x-gunzip", "application/gzipped",
74 UncompressCommand
{"tar", QStringList
{"-zxf"}, QStringList
{"-C"}}});
75 mimeTypeToCommand
.append({QStringList
{"application/bzip", "application/bzip2", "application/x-bzip",
76 "application/x-bzip2", "application/bzip-compressed",
77 "application/bzip2-compressed", "application/x-bzip-compressed",
78 "application/x-bzip2-compressed", "application/bzip-compressed-tar",
79 "application/bzip2-compressed-tar", "application/x-bzip-compressed-tar",
80 "application/x-bzip2-compressed-tar", "application/x-bz2"},
81 UncompressCommand
{"tar", QStringList
{"-jxf"}, QStringList
{"-C"}}});
82 mimeTypeToCommand
.append({QStringList
{"application/zip", "application/x-zip", "application/x-zip-compressed",
84 UncompressCommand
{"unzip", QStringList
{}, QStringList
{"-d"}}});
86 const auto mime
= QMimeDatabase().mimeTypeForFile(inputPath
).name();
88 UncompressCommand command
{};
89 for (const auto &pair
: mimeTypeToCommand
) {
90 if (pair
.first
.contains(mime
)) {
91 command
= pair
.second
;
96 if (command
.command
.isEmpty()) {
97 fail(i18n("Unsupported archive type %1: %2", mime
, inputPath
));
103 QStringList() << command
.args1
<< inputPath
<< command
.args2
<< outputPath
,
105 if (!process
.waitForStarted()) {
106 fail(i18n("Failed to run uncompressor command for %1", inputPath
));
109 if (!process
.waitForFinished()) {
111 i18n("Process did not finish in reasonable time: %1 %2", process
.program(), process
.arguments().join(" ")));
114 if (process
.exitStatus() != QProcess::NormalExit
|| process
.exitCode() != 0) {
115 fail(i18n("Failed to uncompress %1", inputPath
));
119 QString
findRecursive(const QString
&dir
, const QString
&basename
)
121 QDirIterator
it(dir
, QStringList
{basename
}, QDir::Files
, QDirIterator::Subdirectories
);
122 while (it
.hasNext()) {
123 return QFileInfo(it
.next()).canonicalFilePath();
129 bool runInstallerScriptOnce(const QString
&path
, const QStringList
&args
)
132 process
.setWorkingDirectory(QFileInfo(path
).absolutePath());
134 process
.start(path
, args
, QIODevice::NotOpen
);
135 if (!process
.waitForStarted()) {
136 fail(i18n("Failed to run installer script %1", path
));
139 // Wait until installer exits, without timeout
140 if (!process
.waitForFinished(-1)) {
141 qWarning() << "Failed to wait on installer:" << process
.program() << process
.arguments().join(" ");
145 if (process
.exitStatus() != QProcess::NormalExit
|| process
.exitCode() != 0) {
146 qWarning() << "Installer script exited with error:" << process
.program() << process
.arguments().join(" ");
153 // If hasArgVariants is true, run "path".
154 // If hasArgVariants is false, run "path argVariants[i]" until successful.
155 bool runInstallerScript(const QString
&path
, bool hasArgVariants
, const QStringList
&argVariants
, QString
&errorText
)
158 if (!file
.setPermissions(QFile::ReadOwner
| QFile::WriteOwner
| QFile::ExeOwner
)) {
159 errorText
= i18n("Failed to set permissions on %1: %2", path
, file
.errorString());
163 qInfo() << "[servicemenuinstaller]: Trying to run installer/uninstaller" << path
;
164 if (hasArgVariants
) {
165 for (const auto &arg
: argVariants
) {
166 if (runInstallerScriptOnce(path
, QStringList
{arg
})) {
171 if (runInstallerScriptOnce(path
, QStringList
{})) {
177 "%2 = comma separated list of arguments",
178 "Installer script %1 failed, tried arguments \"%2\".", path
, argVariants
.join(i18nc("Separator between arguments", "\", \"")));
182 QString
generateDirPath(const QString
&archive
)
184 return QStringLiteral("%1-dir").arg(archive
);
187 bool cmdInstall(const QString
&archive
, QString
&errorText
)
189 const auto serviceDir
= getServiceMenusDir();
190 if (!QDir().mkpath(serviceDir
)) {
191 // TODO Cannot get error string because of this bug: https://bugreports.qt.io/browse/QTBUG-1483
192 errorText
= i18n("Failed to create path %1", serviceDir
);
196 if (archive
.endsWith(QLatin1String(".desktop"))) {
197 // Append basename to destination directory
198 const auto dest
= QDir(serviceDir
).absoluteFilePath(QFileInfo(archive
).fileName());
199 qInfo() << "Single-File Service-Menu" << archive
<< dest
;
201 QFile
source(archive
);
202 if (!source
.copy(dest
)) {
203 errorText
= i18n("Failed to copy .desktop file %1 to %2: %3", archive
, dest
, source
.errorString());
207 const QStringList binaryPackages
= {"application/vnd.debian.binary-package", "application/x-rpm"};
208 if (binaryPackages
.contains(QMimeDatabase().mimeTypeForFile(archive
).name())) {
209 return QDesktopServices::openUrl(QUrl(archive
));
211 const QString dir
= generateDirPath(archive
);
212 if (QFile::exists(dir
)) {
213 if (!QDir(dir
).removeRecursively()) {
214 errorText
= i18n("Failed to remove directory %1", dir
);
219 if (QDir().mkdir(dir
)) {
220 errorText
= i18n("Failed to create directory %1", dir
);
223 runUncompress(archive
, dir
);
225 // Try "install-it" first
226 QString installItPath
;
227 const auto basenames1
= QStringList
{"install-it.sh", "install-it"};
228 for (const auto &basename
: qAsConst(basenames1
)) {
229 const auto path
= findRecursive(dir
, basename
);
230 if (!path
.isEmpty()) {
231 installItPath
= path
;
236 if (!installItPath
.isEmpty()) {
237 return runInstallerScript(installItPath
, false, QStringList
{}, errorText
);
240 // If "install-it" is missing, try "install"
241 QString installerPath
;
242 const auto basenames2
= QStringList
{"installKDE4.sh", "installKDE4", "install.sh", "install"};
243 for (const auto &basename
: qAsConst(basenames2
)) {
244 const auto path
= findRecursive(dir
, basename
);
245 if (!path
.isEmpty()) {
246 installerPath
= path
;
251 if (!installerPath
.isEmpty()) {
252 return runInstallerScript(installerPath
, true, QStringList
{"--local", "--local-install", "--install"}, errorText
);
255 fail(i18n("Failed to find an installation script in %1", dir
));
261 bool cmdUninstall(const QString
&archive
, QString
&errorText
)
263 const auto serviceDir
= getServiceMenusDir();
264 if (archive
.endsWith(QLatin1String(".desktop"))) {
265 // Append basename to destination directory
266 const auto dest
= QDir(serviceDir
).absoluteFilePath(QFileInfo(archive
).fileName());
268 if (!file
.remove()) {
269 errorText
= i18n("Failed to remove .desktop file %1: %2", dest
, file
.errorString());
273 const QString dir
= generateDirPath(archive
);
275 // Try "deinstall" first
276 QString deinstallPath
;
277 const auto basenames1
= QStringList
{"deinstall.sh", "deinstall"};
278 for (const auto &basename
: qAsConst(basenames1
)) {
279 const auto path
= findRecursive(dir
, basename
);
280 if (!path
.isEmpty()) {
281 deinstallPath
= path
;
286 if (!deinstallPath
.isEmpty()) {
287 bool ok
= runInstallerScript(deinstallPath
, false, QStringList
{}, errorText
);
292 // If "deinstall" is missing, try "install --uninstall"
294 QString installerPath
;
295 const auto basenames2
= QStringList
{"install-it.sh", "install-it", "installKDE4.sh",
296 "installKDE4", "install.sh", "install"};
297 for (const auto &basename
: qAsConst(basenames2
)) {
298 const auto path
= findRecursive(dir
, basename
);
299 if (!path
.isEmpty()) {
300 installerPath
= path
;
305 if (!installerPath
.isEmpty()) {
306 bool ok
= runInstallerScript(
307 installerPath
, true, QStringList
{"--remove", "--delete", "--uninstall", "--deinstall"}, errorText
);
312 fail(i18n("Failed to find an uninstallation script in %1", dir
));
317 if (!dirObject
.removeRecursively()) {
318 errorText
= i18n("Failed to remove directory %1", dir
);
326 int main(int argc
, char *argv
[])
328 QGuiApplication
app(argc
, argv
);
330 QCommandLineParser parser
;
331 parser
.addPositionalArgument(QStringLiteral("command"), i18nc("@info:shell", "Command to execute: install or uninstall."));
332 parser
.addPositionalArgument(QStringLiteral("path"), i18nc("@info:shell", "Path to archive."));
335 const QStringList args
= parser
.positionalArguments();
336 if (args
.isEmpty()) {
337 fail(i18n("Command is required."));
339 if (args
.size() == 1) {
340 fail(i18n("Path to archive is required."));
343 const QString cmd
= args
[0];
344 const QString archive
= args
[1];
347 if (cmd
== "install") {
348 if (!cmdInstall(archive
, errorText
)) {
351 } else if (cmd
== "uninstall") {
352 if (!cmdUninstall(archive
, errorText
)) {
356 fail(i18n("Unsupported command %1", cmd
));