Changeset View
Changeset View
Standalone View
Standalone View
src/settings/services/servicemenuinstaller/servicemenuinstaller.cpp
Show All 18 Lines | |||||
19 | ***************************************************************************/ | 19 | ***************************************************************************/ | ||
20 | 20 | | |||
21 | #include <QDebug> | 21 | #include <QDebug> | ||
22 | #include <QProcess> | 22 | #include <QProcess> | ||
23 | #include <QStandardPaths> | 23 | #include <QStandardPaths> | ||
24 | #include <QDir> | 24 | #include <QDir> | ||
25 | #include <QDirIterator> | 25 | #include <QDirIterator> | ||
26 | #include <QCommandLineParser> | 26 | #include <QCommandLineParser> | ||
27 | #include <QMimeDatabase> | ||||
27 | 28 | | |||
28 | #include <KLocalizedString> | 29 | #include <KLocalizedString> | ||
29 | 30 | | |||
30 | // @param msg Error that gets logged to CLI | 31 | // @param msg Error that gets logged to CLI | ||
31 | Q_NORETURN void fail(const QString &str) | 32 | Q_NORETURN void fail(const QString &str) | ||
32 | { | 33 | { | ||
33 | qCritical() << str; | 34 | qCritical() << str; | ||
34 | 35 | | |||
35 | QProcess process; | 36 | QProcess process; | ||
36 | auto args = QStringList{"--passivepopup", i18n("Dolphin service menu installation failed"), "15"}; | 37 | auto args = QStringList{"--passivepopup", i18n("Dolphin service menu installation failed"), "15"}; | ||
37 | process.start("kdialog", args, QIODevice::ReadOnly); | 38 | process.start("kdialog", args, QIODevice::ReadOnly); | ||
38 | if (!process.waitForStarted()) { | 39 | if (!process.waitForStarted()) { | ||
39 | qFatal("Failed to run kdialog"); | 40 | qFatal("Failed to run kdialog"); | ||
40 | } | 41 | } | ||
41 | 42 | | |||
42 | exit(1); | 43 | exit(1); | ||
43 | } | 44 | } | ||
44 | 45 | | |||
45 | bool evaluateShell(const QString &program, const QStringList &arguments, QString &output, QString &errorText) | | |||
46 | { | | |||
47 | QProcess process; | | |||
48 | process.start(program, arguments, QIODevice::ReadOnly); | | |||
49 | if (!process.waitForStarted()) { | | |||
50 | fail(i18n("Failed to run process: %1 %2", program, arguments.join(" "))); | | |||
51 | } | | |||
52 | | ||||
53 | if (!process.waitForFinished()) { | | |||
54 | fail(i18n("Process did not finish in reasonable time: %1 %2", program, arguments.join(" "))); | | |||
55 | } | | |||
56 | | ||||
57 | const auto stdoutResult = QString::fromUtf8(process.readAllStandardOutput()).trimmed(); | | |||
58 | const auto stderrResult = QString::fromUtf8(process.readAllStandardError()).trimmed(); | | |||
59 | | ||||
60 | if (process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0) { | | |||
61 | output = stdoutResult; | | |||
62 | return true; | | |||
63 | } else { | | |||
64 | errorText = stderrResult + stdoutResult; | | |||
65 | return false; | | |||
66 | } | | |||
67 | } | | |||
68 | | ||||
69 | QString mimeType(const QString &path) | | |||
70 | { | | |||
71 | QString result; | | |||
72 | QString errorText; | | |||
73 | if (evaluateShell("xdg-mime", QStringList{"query", "filetype", path}, result, errorText)) { | | |||
74 | return result; | | |||
75 | } else { | | |||
76 | fail(i18n("Failed to run xdg-mime %1: %2", path, errorText)); | | |||
77 | } | | |||
78 | } | | |||
79 | | ||||
80 | QString getServiceMenusDir() | 46 | QString getServiceMenusDir() | ||
81 | { | 47 | { | ||
82 | const QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); | 48 | const QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); | ||
83 | return QDir(dataLocation).absoluteFilePath("kservices5/ServiceMenus"); | 49 | return QDir(dataLocation).absoluteFilePath("kservices5/ServiceMenus"); | ||
84 | } | 50 | } | ||
85 | 51 | | |||
86 | struct UncompressCommand | 52 | struct UncompressCommand | ||
87 | { | 53 | { | ||
Show All 21 Lines | 72 | mimeTypeToCommand.append({QStringList{"application/bzip", "application/bzip2", "application/x-bzip", | |||
109 | "application/x-bzip2-compressed", "application/bzip-compressed-tar", | 75 | "application/x-bzip2-compressed", "application/bzip-compressed-tar", | ||
110 | "application/bzip2-compressed-tar", "application/x-bzip-compressed-tar", | 76 | "application/bzip2-compressed-tar", "application/x-bzip-compressed-tar", | ||
111 | "application/x-bzip2-compressed-tar", "application/x-bz2"}, | 77 | "application/x-bzip2-compressed-tar", "application/x-bz2"}, | ||
112 | UncompressCommand{"tar", QStringList{"-jxf"}, QStringList{"-C"}}}); | 78 | UncompressCommand{"tar", QStringList{"-jxf"}, QStringList{"-C"}}}); | ||
113 | mimeTypeToCommand.append({QStringList{"application/zip", "application/x-zip", "application/x-zip-compressed", | 79 | mimeTypeToCommand.append({QStringList{"application/zip", "application/x-zip", "application/x-zip-compressed", | ||
114 | "multipart/x-zip"}, | 80 | "multipart/x-zip"}, | ||
115 | UncompressCommand{"unzip", QStringList{}, QStringList{"-d"}}}); | 81 | UncompressCommand{"unzip", QStringList{}, QStringList{"-d"}}}); | ||
116 | 82 | | |||
117 | const auto mime = mimeType(inputPath); | 83 | const auto mime = QMimeDatabase().mimeTypeForFile(inputPath).name(); | ||
118 | 84 | | |||
119 | UncompressCommand command{}; | 85 | UncompressCommand command{}; | ||
120 | for (const auto &pair : mimeTypeToCommand) { | 86 | for (const auto &pair : mimeTypeToCommand) { | ||
121 | if (pair.first.contains(mime)) { | 87 | if (pair.first.contains(mime)) { | ||
122 | command = pair.second; | 88 | command = pair.second; | ||
123 | break; | 89 | break; | ||
124 | } | 90 | } | ||
125 | } | 91 | } | ||
▲ Show 20 Lines • Show All 262 Lines • Show Last 20 Lines |