Replies: 6 comments
-
其实 Linux 下的桌面环境,主流也就两三种。再根据它们的特性,特别写一下获取方式,应该是可以的。 |
Beta Was this translation helpful? Give feedback.
-
以我当前使用的 KDE 桌面环境为例,它「时时记录」当前壁纸路径完整信息的文件是:
这里, 即使用户设置了「每一秒换一次壁纸」,KDE桌面也是先将实际的图片地址写到这里,再下发到桌面其它组件来渲染: 比如,我们看到这一时刻,生效的壁纸是: 那么,写一个 KDE 下读取该字段的函数,就是非常简单的了:
|
Beta Was this translation helpful? Give feedback.
-
同样地,Gnome 桌面环境中,可以直接使用 https://unix.stackexchange.com/questions/606635/how-to-find-current-wallpaper-image-file-path https://askubuntu.com/questions/1220766/how-to-get-the-file-location-of-the-current-wallpaper-file
|
Beta Was this translation helpful? Give feedback.
-
KDE 与 Gnome 兼容进来,就解决了 99% 的linux桌面用户,其它小众桌面环境,几乎可以忽略不计。 |
Beta Was this translation helpful? Give feedback.
-
趁楚哥正在制作新版,给一点建议,希望能有助益。 |
Beta Was this translation helpful? Give feedback.
-
适配 Gnome 与 KDE 的办法,已测可行: QString FluTools::getWallpaperFilePath() {
#if defined(Q_OS_WIN)
wchar_t path[MAX_PATH] = {};
if (::SystemParametersInfoW(SPI_GETDESKWALLPAPER, MAX_PATH, path, FALSE) == FALSE) {
return {};
}
return QString::fromWCharArray(path);
#elif defined(Q_OS_LINUX)
QString currentDesktopName = getCurrentDesktopName();
if(currentDesktopName.contains("KDE")){
QString str = getKDEWallpaperFilePath();
qDebug() << "KDE_WallpaperFilePath :" << str << '\n';
return str;
}
if(currentDesktopName.contains("GNOME")){
QString str = getGnomeWallpaperFilePath();
qDebug() << "Gnome_WallpaperFilePath :" << str << '\n';
return str;
}
auto type = QSysInfo::productType();
if (type == "uos") {
QProcess process;
QStringList args;
args << "--session";
args << "--type=method_call";
args << "--print-reply";
args << "--dest=com.deepin.wm";
args << "/com/deepin/wm";
args << "com.deepin.wm.GetCurrentWorkspaceBackgroundForMonitor";
args << QString("string:'%1'").arg(currentTimestamp());
process.start("dbus-send", args);
process.waitForFinished();
QByteArray result = process.readAllStandardOutput().trimmed();
int startIndex = result.indexOf("file:///");
if (startIndex != -1) {
auto path = result.mid(startIndex + 7, result.length() - startIndex - 8);
return path;
}
}
#elif defined(Q_OS_MACOS)
QProcess process;
QStringList args;
args << "-e";
args << R"(tell application "Finder" to get POSIX path of (desktop picture as alias))";
process.start("osascript", args);
process.waitForFinished();
QByteArray result = process.readAllStandardOutput().trimmed();
if(result.isEmpty()){
return "/System/Library/CoreServices/DefaultDesktop.heic";
}
return result;
#else
return {};
#endif
return {};
}
QString FluTools::getCurrentUserName(){
QProcess process;
process.start("whoami");
process.waitForFinished();
QString output = process.readAllStandardOutput();
output.remove(QChar('\n')); // 移除换行符(如果有的话)
return output;
}
QString FluTools::getCurrentDesktopName(){
QProcess process;
QString program = "bash";
QStringList arguments;
arguments << "-c" << "echo $XDG_CURRENT_DESKTOP";
process.start(program, arguments);
process.waitForFinished();
QByteArray output = process.readAllStandardOutput();
QString desktopEnvironment = QString::fromLocal8Bit(output);
desktopEnvironment = desktopEnvironment.trimmed();
qDebug() << "Desktop Environment:" << desktopEnvironment << '\n';
return desktopEnvironment;
}
QString FluTools::getKDEWallpaperFilePath(){
QString currentUserName = getCurrentUserName();
QString currentLogPath = QString("/home/%1/.config/plasma-org.kde.plasma.desktop-appletsrc").arg(currentUserName);
QFile aFile(currentLogPath);
if(!aFile.exists()){
qDebug() << "This KDE file is not exits !" << currentLogPath << '\n';
return NULL;
}
if(!aFile.open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug() << "This KDE file open failed !" << currentLogPath << '\n';
return NULL;
}
QTextStream aStream(&aFile);
aStream.setAutoDetectUnicode(true);
bool arrivedTarget = false;
QString str;
while(!aStream.atEnd()){
str = aStream.readLine();
if(str.contains("[Wallpaper]")){
arrivedTarget = true;
}
if(str.contains("Image=file://")){
if(arrivedTarget){
QString strTemp = str;
strTemp.replace("Image=file://","");
qDebug() << "current KDEWallpaper is: " << strTemp << '\n';
return strTemp;
}
}
}
return NULL;
}
QString FluTools::getGnomeWallpaperFilePath(){
QProcess process;
QString program = "bash";
QStringList arguments;
arguments << "-c" << "gsettings get org.gnome.desktop.background picture-uri";
process.start(program, arguments);
process.waitForFinished();
QByteArray output = process.readAllStandardOutput();
QString filePath = QString::fromLocal8Bit(output);
filePath = filePath.trimmed();
filePath.replace("file://","");
qDebug() << "current GnomeWallpaper is: " << filePath << '\n';
return filePath;
} |
Beta Was this translation helpful? Give feedback.
-
FluTools.cpp 中有个函数,定义了找查当前壁纸的方法:
群主使用
auto type = QSysInfo::productType();
来实现大概的系统判断,但这个确实有很大的局限性。目前 FluApp 的这一特性因此无法覆盖到其它比较流行的桌面环境,比如Gnome
或KDE
在 Qt 下,可以调用
bash
执行一个本地变量读取操作,获取到当前用户正在使用的桌面环境名称,比如拟用QString FluTools::getCurrentDesktopName()
,会得到Gnome
或KDE
等等。Beta Was this translation helpful? Give feedback.
All reactions