银河麒麟使用的桌面为MATE桌面。
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QMessageBox>
#include <QStandardPaths>
#include <QFileInfo>
#include <QProcess>
bool shortcutExists(const QString& appName) {
// 获取桌面路径
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
if (desktopPath.isEmpty()) {
qDebug() << "无法获取桌面路径";
return false;
}
// 构建.desktop文件路径
QString desktopFileName = appName + ".desktop";
QString desktopFilePath = desktopPath + QDir::separator() + desktopFileName;
// 检查文件是否存在
return QFile::exists(desktopFilePath);
}
bool isAutostartEnabled(const QString& appName) {
// 获取自动启动目录
QString autostartDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart/";
QString autostartFilePath = autostartDir + appName + ".desktop";
// 检查自动启动文件是否存在
return QFile::exists(autostartFilePath);
}
QString findApplicationIcon(const QString& appName, const QString& execPath) {
// 获取可执行文件所在目录
QFileInfo execFileInfo(execPath);
QString execDir = execFileInfo.absolutePath();
// 检查目录下是否存在logo.png
QString logoPath = execDir + QDir::separator() + "logo.png";
if (QFile::exists(logoPath)) {
qDebug() << "找到应用图标:" << logoPath;
return logoPath;
}
// 尝试查找其他可能的图标文件
QStringList possibleIcons = {
"icon.png",
appName.toLower() + ".png",
appName.toLower() + ".svg",
"application-x-executable" // 默认应用图标
};
for (const QString& icon : possibleIcons) {
QString iconPath = execDir + QDir::separator() + icon;
if (QFile::exists(iconPath)) {
qDebug() << "找到替代图标:" << iconPath;
return iconPath;
}
}
// 如果没有找到自定义图标,使用系统默认图标
qDebug() << "未找到自定义图标,使用默认图标";
return "application-x-executable"; // 系统默认应用图标
}
bool createDesktopShortcut(const QString& appName,
const QString& execPath,
const QString& iconPath = "",
const QString& comment = "",
bool terminal = false,
bool mateSpecific = true) {
// 获取桌面路径
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
if (desktopPath.isEmpty()) {
qDebug() << "无法获取桌面路径";
return false;
}
// 构建.desktop文件路径
QString desktopFileName = appName + ".desktop";
QString desktopFilePath = desktopPath + QDir::separator() + desktopFileName;
// 创建文件
QFile file(desktopFilePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "无法创建文件:" << desktopFilePath;
return false;
}
// 写入文件内容
QTextStream out(&file);
out << "[Desktop Entry]\n";
out << "Version=1.0\n";
out << "Type=Application\n";
out << "Name=" << appName << "\n";
if (!comment.isEmpty())
out << "Comment=" << comment << "\n";
out << "Exec=" << execPath << "\n";
out << "Terminal=" << (terminal ? "true" : "false") << "\n";
// 使用提供的图标路径或默认图标
QString effectiveIcon = !iconPath.isEmpty() ? iconPath : "application-x-executable";
out << "Icon=" << effectiveIcon << "\n";
// MATE桌面特定配置
if (mateSpecific) {
out << "X-MATE-Autostart-enabled=true\n";
out << "X-MATE-Autostart-Delay=0\n";
out << "X-MATE-Provides=panelapplet\n";
out << "Categories=GNOME;GTK;Utility;\n";
} else {
out << "Categories=Application;\n";
}
file.close();
// 设置文件权限为可执行
QFileInfo fileInfo(desktopFilePath);
QFile::setPermissions(
desktopFilePath,
fileInfo.permissions() |
QFileDevice::ExeUser |
QFileDevice::ExeGroup |
QFileDevice::ExeOther
);
qDebug() << "桌面快捷方式已创建:" << desktopFilePath;
// 刷新MATE桌面(可选)
QProcess::startDetached("mate-panel --replace &");
return true;
}
bool enableAutostart(const QString& appName,
const QString& execPath,
const QString& iconPath = "",
const QString& comment = "",
bool terminal = false,
bool mateSpecific = true) {
// 获取自动启动目录
QString autostartDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart/";
QString autostartFilePath = autostartDir + appName + ".desktop";
// 创建自动启动目录(如果不存在)
QDir().mkpath(autostartDir);
// 创建文件
QFile file(autostartFilePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "无法创建自动启动文件:" << autostartFilePath;
return false;
}
// 写入文件内容
QTextStream out(&file);
out << "[Desktop Entry]\n";
out << "Version=1.0\n";
out << "Type=Application\n";
out << "Name=" << appName << "\n";
if (!comment.isEmpty())
out << "Comment=" << comment << "\n";
out << "Exec=" << execPath << "\n";
out << "Terminal=" << (terminal ? "true" : "false") << "\n";
// 使用提供的图标路径或默认图标
QString effectiveIcon = !iconPath.isEmpty() ? iconPath : "application-x-executable";
out << "Icon=" << effectiveIcon << "\n";
// 自动启动特定配置
out << "NoDisplay=true\n";
out << "Hidden=false\n";
out << "X-GNOME-Autostart-enabled=true\n";
out << "X-MATE-Autostart-enabled=true\n";
// MATE桌面特定配置
if (mateSpecific) {
out << "X-MATE-Autostart-Delay=0\n";
out << "Categories=GNOME;GTK;Utility;\n";
} else {
out << "Categories=Application;\n";
}
file.close();
// 设置文件权限
QFileInfo fileInfo(autostartFilePath);
QFile::setPermissions(
autostartFilePath,
fileInfo.permissions() |
QFileDevice::ExeUser |
QFileDevice::ExeGroup |
QFileDevice::ExeOther
);
qDebug() << "已启用开机自启动:" << autostartFilePath;
return true;
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
// 获取应用信息
QString appName = QCoreApplication::applicationName();
QString execPath = QCoreApplication::applicationFilePath();
QString comment = "My Qt Application for MATE Desktop";
// 查找应用图标
QString iconPath = findApplicationIcon(appName, execPath);
// 检查并创建桌面快捷方式
if (!shortcutExists(appName)) {
if (createDesktopShortcut(appName, execPath, iconPath, comment, false, true)) {
qDebug() << "MATE桌面快捷方式创建成功!";
} else {
qDebug() << "MATE桌面快捷方式创建失败!";
return 1;
}
} else {
qDebug() << "桌面快捷方式已存在,跳过创建";
}
// 检查并启用开机自启动
if (!isAutostartEnabled(appName)) {
if (enableAutostart(appName, execPath, iconPath, comment, false, true)) {
qDebug() << "已成功设置开机自启动!";
} else {
qDebug() << "开机自启动设置失败!";
return 1;
}
} else {
qDebug() << "开机自启动已启用,跳过设置";
}
return 0;
}
评论 (0)