MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / writeLinuxDesktop

Method writeLinuxDesktop

app/src/Misc/ShortcutGenerator_Linux.cpp:72–124  ·  view source on GitHub ↗

* @brief Writes a freedesktop.org Desktop Entry that launches Serial Studio with args. */

Source from the content-addressed store, hash-verified

70 * @brief Writes a freedesktop.org Desktop Entry that launches Serial Studio with args.
71 */
72bool Misc::ShortcutGenerator::writeLinuxDesktop(const QString& outputPath,
73 const QString& target,
74 const QStringList& args,
75 const QString& title,
76 const QString& iconPath,
77 QString& errorOut)
78{
79 QStringList quoted;
80 quoted.reserve(args.size());
81 for (const auto& a : args)
82 quoted << quoteArg(a);
83
84 QFile file(outputPath);
85 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
86 errorOut = tr("Could not open the shortcut path for writing: %1").arg(file.errorString());
87 return false;
88 }
89
90 const QString safeTitle = escapeDesktopValue(title);
91 const QString safeComment = escapeDesktopValue(tr("Serial Studio shortcut"));
92 const QString safeIcon = escapeDesktopValue(iconPath);
93 const QString safeExec = escapeDesktopValue(escapeExecPercent(
94 QStringLiteral("%1 %2").arg(quoteArg(target), quoted.join(QLatin1Char(' ')))));
95
96 QTextStream out(&file);
97 out << QStringLiteral("[Desktop Entry]\n");
98 out << QStringLiteral("Type=Application\n");
99 out << QStringLiteral("Version=1.0\n");
100 out << QStringLiteral("Name=%1\n").arg(safeTitle);
101 out << QStringLiteral("Comment=%1\n").arg(safeComment);
102 out << QStringLiteral("Exec=%1\n").arg(safeExec);
103 out << QStringLiteral("Icon=%1\n").arg(safeIcon);
104 out << QStringLiteral("Terminal=false\n");
105 out << QStringLiteral("Categories=Development;Engineering;\n");
106 file.close();
107
108 const auto perms = QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner
109 | QFileDevice::ReadGroup | QFileDevice::ExeGroup | QFileDevice::ReadOther
110 | QFileDevice::ExeOther;
111 if (!QFile::setPermissions(outputPath, perms)) {
112 errorOut = tr("Could not mark the shortcut as executable.");
113 return false;
114 }
115
116 const QString applicationsDir = QDir::homePath() + QStringLiteral("/.local/share/applications");
117 if (QFileInfo(outputPath).absolutePath() == applicationsDir) {
118 const QString tool = QStandardPaths::findExecutable(QStringLiteral("update-desktop-database"));
119 if (!tool.isEmpty())
120 QProcess::startDetached(tool, {applicationsDir});
121 }
122
123 return true;
124}
125
126//--------------------------------------------------------------------------------------------------
127// Stubs (only the platform writer is compiled in)

Callers

nothing calls this directly

Calls 7

escapeDesktopValueFunction · 0.85
escapeExecPercentFunction · 0.85
errorStringMethod · 0.80
isEmptyMethod · 0.80
sizeMethod · 0.45
openMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected