2 * SPDX-FileCopyrightText: 2019 Alexander Potashev <aspotashev@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
7 #include <KLocalizedString>
9 #include <QCommandLineParser>
12 #include <QDirIterator>
13 #include <QGuiApplication>
14 #include <QMimeDatabase>
16 #include <QStandardPaths>
20 #include "../../../config-dolphin.h"
22 Q_GLOBAL_STATIC_WITH_ARGS(QStringList
,
24 ({QLatin1String("application/vnd.debian.binary-package"),
25 QLatin1String("application/x-rpm"),
26 QLatin1String("application/x-xz"),
27 QLatin1String("application/zstd")}))
29 enum PackageOperation
{ Install
, Uninstall
};
32 #include <PackageKit/Daemon>
33 #include <PackageKit/Details>
34 #include <PackageKit/Transaction>
36 #include <QDesktopServices>
39 // @param msg Error that gets logged to CLI
40 Q_NORETURN
void fail(const QString
&str
)
43 const QStringList args
= {"--detailederror", i18n("Dolphin service menu installation failed"), str
};
44 QProcess::startDetached("kdialog", args
);
49 QString
getServiceMenusDir()
51 const QString dataLocation
= QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation
);
52 return QDir(dataLocation
).absoluteFilePath("kio/servicemenus");
56 void packageKitInstall(const QString
&fileName
)
58 PackageKit::Transaction
*transaction
= PackageKit::Daemon::installFile(fileName
, PackageKit::Transaction::TransactionFlagNone
);
60 const auto exitWithError
= [=](PackageKit::Transaction::Error
, const QString
&details
) {
64 QObject::connect(transaction
, &PackageKit::Transaction::finished
, [=](PackageKit::Transaction::Exit status
, uint
) {
65 if (status
== PackageKit::Transaction::ExitSuccess
) {
68 // Fallback error handling
69 QTimer::singleShot(500, [=]() {
70 fail(i18n("Failed to install \"%1\", exited with status \"%2\"", fileName
, QVariant::fromValue(status
).toString()));
73 QObject::connect(transaction
, &PackageKit::Transaction::errorCode
, exitWithError
);
76 void packageKitUninstall(const QString
&fileName
)
78 const auto exitWithError
= [=](PackageKit::Transaction::Error
, const QString
&details
) {
81 const auto uninstallLambda
= [=](PackageKit::Transaction::Exit status
, uint
) {
82 if (status
== PackageKit::Transaction::ExitSuccess
) {
87 PackageKit::Transaction
*transaction
= PackageKit::Daemon::getDetailsLocal(fileName
);
88 QObject::connect(transaction
, &PackageKit::Transaction::details
, [=](const PackageKit::Details
&details
) {
89 PackageKit::Transaction
*transaction
= PackageKit::Daemon::removePackage(details
.packageId());
90 QObject::connect(transaction
, &PackageKit::Transaction::finished
, uninstallLambda
);
91 QObject::connect(transaction
, &PackageKit::Transaction::errorCode
, exitWithError
);
94 QObject::connect(transaction
, &PackageKit::Transaction::errorCode
, exitWithError
);
95 // Fallback error handling
96 QObject::connect(transaction
, &PackageKit::Transaction::finished
, [=](PackageKit::Transaction::Exit status
, uint
) {
97 if (status
!= PackageKit::Transaction::ExitSuccess
) {
98 QTimer::singleShot(500, [=]() {
99 fail(i18n("Failed to uninstall \"%1\", exited with status \"%2\"", fileName
, QVariant::fromValue(status
).toString()));
106 Q_NORETURN
void packageKit(PackageOperation operation
, const QString
&fileName
)
109 QFileInfo
fileInfo(fileName
);
110 if (!fileInfo
.exists()) {
111 fail(i18n("The file does not exist!"));
113 const QString absPath
= fileInfo
.absoluteFilePath();
114 if (operation
== PackageOperation::Install
) {
115 packageKitInstall(absPath
);
117 packageKitUninstall(absPath
);
119 QGuiApplication::exec(); // For event handling, no return after signals finish
120 fail(i18n("Unknown error when installing package"));
123 QDesktopServices::openUrl(QUrl(fileName
));
128 struct UncompressCommand
{
134 enum ScriptExecution
{ Process
, Konsole
};
136 void runUncompress(const QString
&inputPath
, const QString
&outputPath
)
138 QVector
<QPair
<QStringList
, UncompressCommand
>> mimeTypeToCommand
;
139 mimeTypeToCommand
.append({{"application/x-tar", "application/tar", "application/x-gtar", "multipart/x-tar"}, UncompressCommand({"tar", {"-xf"}, {"-C"}})});
140 mimeTypeToCommand
.append({{"application/x-gzip",
142 "application/x-gzip-compressed-tar",
143 "application/gzip-compressed-tar",
144 "application/x-gzip-compressed",
145 "application/gzip-compressed",
147 "application/x-compressed-tar",
148 "application/x-compressed-gtar",
150 "multipart/x-tar-gz",
151 "application/x-gunzip",
152 "application/gzipped",
154 UncompressCommand({"tar", {"-zxf"}, {"-C"}})});
155 mimeTypeToCommand
.append({{"application/bzip",
157 "application/x-bzip",
158 "application/x-bzip2",
159 "application/bzip-compressed",
160 "application/bzip2-compressed",
161 "application/x-bzip-compressed",
162 "application/x-bzip2-compressed",
163 "application/bzip-compressed-tar",
164 "application/bzip2-compressed-tar",
165 "application/x-bzip-compressed-tar",
166 "application/x-bzip2-compressed-tar",
167 "application/x-bz2"},
168 UncompressCommand({"tar", {"-jxf"}, {"-C"}})});
169 mimeTypeToCommand
.append(
170 {{"application/zip", "application/x-zip", "application/x-zip-compressed", "multipart/x-zip"}, UncompressCommand({"unzip", {}, {"-d"}})});
172 const auto mime
= QMimeDatabase().mimeTypeForFile(inputPath
).name();
174 UncompressCommand command
{};
175 for (const auto &pair
: std::as_const(mimeTypeToCommand
)) {
176 if (pair
.first
.contains(mime
)) {
177 command
= pair
.second
;
182 if (command
.command
.isEmpty()) {
183 fail(i18n("Unsupported archive type %1: %2", mime
, inputPath
));
187 process
.start(command
.command
, QStringList() << command
.args1
<< inputPath
<< command
.args2
<< outputPath
, QIODevice::NotOpen
);
188 if (!process
.waitForStarted()) {
189 fail(i18n("Failed to run uncompressor command for %1", inputPath
));
192 if (!process
.waitForFinished()) {
193 fail(i18n("Process did not finish in reasonable time: %1 %2", process
.program(), process
.arguments().join(" ")));
196 if (process
.exitStatus() != QProcess::NormalExit
|| process
.exitCode() != 0) {
197 fail(i18n("Failed to uncompress %1", inputPath
));
201 QString
findRecursive(const QString
&dir
, const QString
&basename
)
203 QDirIterator
it(dir
, QStringList
{basename
}, QDir::Files
, QDirIterator::Subdirectories
);
204 while (it
.hasNext()) {
205 return QFileInfo(it
.next()).absoluteFilePath();
211 bool runScriptOnce(const QString
&path
, const QStringList
&args
, ScriptExecution execution
)
214 process
.setWorkingDirectory(QFileInfo(path
).absolutePath());
216 const static bool konsoleAvailable
= !QStandardPaths::findExecutable("konsole").isEmpty();
217 if (konsoleAvailable
&& execution
== ScriptExecution::Konsole
) {
218 QString bashCommand
= KShell::quoteArg(path
) + ' ';
219 if (!args
.isEmpty()) {
220 bashCommand
.append(args
.join(' '));
222 bashCommand
.append("|| $SHELL");
223 // If the install script fails a shell opens and the user can fix the problem
224 // without an error konsole closes
225 process
.start("konsole",
226 QStringList() << "-e"
228 << "-c" << bashCommand
,
231 process
.start(path
, args
, QIODevice::NotOpen
);
233 if (!process
.waitForStarted()) {
234 fail(i18n("Failed to run installer script %1", path
));
237 // Wait until installer exits, without timeout
238 if (!process
.waitForFinished(-1)) {
239 qWarning() << "Failed to wait on installer:" << process
.program() << process
.arguments().join(" ");
243 if (process
.exitStatus() != QProcess::NormalExit
|| process
.exitCode() != 0) {
244 qWarning() << "Installer script exited with error:" << process
.program() << process
.arguments().join(" ");
251 // If hasArgVariants is true, run "path".
252 // If hasArgVariants is false, run "path argVariants[i]" until successful.
253 bool runScriptVariants(const QString
&path
, bool hasArgVariants
, const QStringList
&argVariants
, QString
&errorText
)
256 if (!file
.setPermissions(QFile::ReadOwner
| QFile::WriteOwner
| QFile::ExeOwner
)) {
257 errorText
= i18n("Failed to set permissions on %1: %2", path
, file
.errorString());
261 qInfo() << "[servicemenuinstaller]: Trying to run installer/uninstaller" << path
;
262 if (hasArgVariants
) {
263 for (const auto &arg
: argVariants
) {
264 if (runScriptOnce(path
, {arg
}, ScriptExecution::Process
)) {
268 } else if (runScriptOnce(path
, {}, ScriptExecution::Konsole
)) {
272 errorText
= i18nc("%2 = comma separated list of arguments",
273 "Installer script %1 failed, tried arguments \"%2\".",
275 argVariants
.join(i18nc("Separator between arguments", "\", \"")));
279 QString
generateDirPath(const QString
&archive
)
281 return QStringLiteral("%1-dir").arg(archive
);
284 bool cmdInstall(const QString
&archive
, QString
&errorText
)
286 const auto serviceDir
= getServiceMenusDir();
287 if (!QDir().mkpath(serviceDir
)) {
288 // TODO Cannot get error string because of this bug: https://bugreports.qt.io/browse/QTBUG-1483
289 errorText
= i18n("Failed to create path %1", serviceDir
);
293 if (archive
.endsWith(QLatin1String(".desktop"))) {
294 // Append basename to destination directory
295 const auto dest
= QDir(serviceDir
).absoluteFilePath(QFileInfo(archive
).fileName());
296 if (QFileInfo::exists(dest
)) {
299 qInfo() << "Single-File Service-Menu" << archive
<< dest
;
301 QFile
source(archive
);
302 if (!source
.copy(dest
)) {
303 errorText
= i18n("Failed to copy .desktop file %1 to %2: %3", archive
, dest
, source
.errorString());
306 QFile
destFile(dest
);
307 destFile
.setPermissions(destFile
.permissions() | QFile::ExeOwner
);
309 if (binaryPackages
->contains(QMimeDatabase().mimeTypeForFile(archive
).name())) {
310 packageKit(PackageOperation::Install
, archive
);
312 const QString dir
= generateDirPath(archive
);
313 if (QFile::exists(dir
)) {
314 if (!QDir(dir
).removeRecursively()) {
315 errorText
= i18n("Failed to remove directory %1", dir
);
320 if (QDir().mkdir(dir
)) {
321 errorText
= i18n("Failed to create directory %1", dir
);
324 runUncompress(archive
, dir
);
326 // Try "install-it" first
327 QString installItPath
;
328 const QStringList basenames1
= {"install-it.sh", "install-it"};
329 for (const auto &basename
: basenames1
) {
330 const auto path
= findRecursive(dir
, basename
);
331 if (!path
.isEmpty()) {
332 installItPath
= path
;
337 if (!installItPath
.isEmpty()) {
338 return runScriptVariants(installItPath
, false, QStringList
{}, errorText
);
341 // If "install-it" is missing, try "install"
342 QString installerPath
;
343 const QStringList basenames2
= {"installKDE4.sh", "installKDE4", "install.sh", "install*.sh"};
344 for (const auto &basename
: basenames2
) {
345 const auto path
= findRecursive(dir
, basename
);
346 if (!path
.isEmpty()) {
347 installerPath
= path
;
352 if (!installerPath
.isEmpty()) {
353 // Try to run script without variants first
354 if (!runScriptVariants(installerPath
, false, {}, errorText
)) {
355 return runScriptVariants(installerPath
, true, {"--local", "--local-install", "--install"}, errorText
);
360 fail(i18n("Failed to find an installation script in %1", dir
));
366 bool cmdUninstall(const QString
&archive
, QString
&errorText
)
368 const auto serviceDir
= getServiceMenusDir();
369 if (archive
.endsWith(QLatin1String(".desktop"))) {
370 // Append basename to destination directory
371 const auto dest
= QDir(serviceDir
).absoluteFilePath(QFileInfo(archive
).fileName());
373 if (!file
.remove()) {
374 errorText
= i18n("Failed to remove .desktop file %1: %2", dest
, file
.errorString());
378 if (binaryPackages
->contains(QMimeDatabase().mimeTypeForFile(archive
).name())) {
379 packageKit(PackageOperation::Uninstall
, archive
);
381 const QString dir
= generateDirPath(archive
);
383 // Try "deinstall" first
384 QString deinstallPath
;
385 const QStringList basenames1
= {"uninstall.sh", "uninstall", "deinstall.sh", "deinstall"};
386 for (const auto &basename
: basenames1
) {
387 const auto path
= findRecursive(dir
, basename
);
388 if (!path
.isEmpty()) {
389 deinstallPath
= path
;
394 if (!deinstallPath
.isEmpty()) {
395 const bool ok
= runScriptVariants(deinstallPath
, false, {}, errorText
);
400 // If "deinstall" is missing, try "install --uninstall"
401 QString installerPath
;
402 const QStringList basenames2
= {"install-it.sh", "install-it", "installKDE4.sh", "installKDE4", "install.sh", "install"};
403 for (const auto &basename
: basenames2
) {
404 const auto path
= findRecursive(dir
, basename
);
405 if (!path
.isEmpty()) {
406 installerPath
= path
;
411 if (!installerPath
.isEmpty()) {
412 const bool ok
= runScriptVariants(installerPath
, true, {"--remove", "--delete", "--uninstall", "--deinstall"}, errorText
);
417 fail(i18n("Failed to find an uninstallation script in %1", dir
));
422 if (!dirObject
.removeRecursively()) {
423 errorText
= i18n("Failed to remove directory %1", dir
);
431 int main(int argc
, char *argv
[])
433 QGuiApplication
app(argc
, argv
);
435 QCommandLineParser parser
;
436 parser
.addPositionalArgument(QStringLiteral("command"), i18nc("@info:shell", "Command to execute: install or uninstall."));
437 parser
.addPositionalArgument(QStringLiteral("path"), i18nc("@info:shell", "Path to archive."));
440 const QStringList args
= parser
.positionalArguments();
441 if (args
.isEmpty()) {
442 fail(i18n("Command is required."));
444 if (args
.size() == 1) {
445 fail(i18n("Path to archive is required."));
448 const QString cmd
= args
[0];
449 const QString archive
= args
[1];
452 if (cmd
== QLatin1String("install")) {
453 if (!cmdInstall(archive
, errorText
)) {
456 } else if (cmd
== QLatin1String("uninstall")) {
457 if (!cmdUninstall(archive
, errorText
)) {
461 fail(i18n("Unsupported command %1", cmd
));