+void QtUi::refreshIconTheme()
+{
+ // List of available fallback themes
+ QStringList availableThemes;
+ for (auto&& themePair : availableIconThemes()) {
+ availableThemes << themePair.first;
+ }
+
+ if (availableThemes.isEmpty()) {
+ // We could probably introduce a more sophisticated fallback handling, such as putting the "most important" icons into hicolor,
+ // but this just gets complex for no good reason. We really rely on a supported theme to be installed, if not system-wide, then
+ // as part of the Quassel installation (which is enabled by default anyway).
+ qWarning() << tr(
+ "No supported icon theme installed, you'll lack icons! Supported are the KDE/Plasma themes Breeze, Breeze Dark and Oxygen.");
+ return;
+ }
+
+ UiStyleSettings s;
+ QString fallbackTheme{s.value("Icons/FallbackTheme").toString()};
+
+ if (fallbackTheme.isEmpty() || !availableThemes.contains(fallbackTheme)) {
+ if (availableThemes.contains(_systemIconTheme)) {
+ fallbackTheme = _systemIconTheme;
+ }
+ else {
+ fallbackTheme = availableThemes.first();
+ }
+ }
+
+ if (_systemIconTheme.isEmpty() || _systemIconTheme == fallbackTheme || s.value("Icons/OverrideSystemTheme", true).toBool()) {
+ // We have a valid fallback theme and want to override the system theme (if it's even defined), so we're basically done
+ QIcon::setThemeName(fallbackTheme);
+ emit iconThemeRefreshed();
+ return;
+ }
+
+ // At this point, we have a system theme that we don't want to override, but that may not contain all
+ // required icons.
+ // We create a dummy theme that inherits first from the system theme, then from the supported fallback.
+ // This rather ugly hack allows us to inject the fallback into the inheritance chain, so non-standard
+ // icons missing in the system theme will be filled in by the fallback.
+ // Since we can't get notified when the system theme changes, this means that a restart may be required
+ // to apply a theme change... but you can't have everything, I guess.
+ if (!_dummyThemeDir) {
+ _dummyThemeDir = std::make_unique<QTemporaryDir>();
+ if (!_dummyThemeDir->isValid() || !QDir{_dummyThemeDir->path()}.mkpath("icons/quassel-icon-proxy/apps/32")) {
+ qWarning() << "Could not create temporary directory for proxying the system icon theme, using fallback";
+ QIcon::setThemeName(fallbackTheme);
+ emit iconThemeRefreshed();
+ return;
+ }
+ // Add this to XDG_DATA_DIRS, otherwise KIconLoader complains
+ auto xdgDataDirs = qgetenv("XDG_DATA_DIRS");
+ if (!xdgDataDirs.isEmpty())
+ xdgDataDirs += ":";
+ xdgDataDirs += _dummyThemeDir->path();
+ qputenv("XDG_DATA_DIRS", xdgDataDirs);
+
+ QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << _dummyThemeDir->path() + "/icons");
+ }
+
+ QFile indexFile{_dummyThemeDir->path() + "/icons/quassel-icon-proxy/index.theme"};
+ if (!indexFile.open(QFile::WriteOnly | QFile::Truncate)) {
+ qWarning() << "Could not create index file for proxying the system icon theme, using fallback";
+ QIcon::setThemeName(fallbackTheme);
+ emit iconThemeRefreshed();
+ return;
+ }
+
+ // Write a dummy index file that is sufficient to make QIconLoader happy
+ auto indexContents = QString{"[Icon Theme]\n"
+ "Name=quassel-icon-proxy\n"
+ "Inherits=%1,%2\n"
+ "Directories=apps/32\n"
+ "[apps/32]\nSize=32\nType=Fixed\n"}
+ .arg(_systemIconTheme, fallbackTheme);
+ if (indexFile.write(indexContents.toLatin1()) < 0) {
+ qWarning() << "Could not write index file for proxying the system icon theme, using fallback";
+ QIcon::setThemeName(fallbackTheme);
+ emit iconThemeRefreshed();
+ return;
+ }
+ indexFile.close();
+ QIcon::setThemeName("quassel-icon-proxy");