This source file includes following definitions.
- GetXDGDirectory
- GetXDGUserDirectory
- GetDesktopEnvironment
- GetDesktopEnvironmentName
- GetDesktopEnvironmentName
#include "base/nix/xdg_util.h"
#include <string>
#include "base/environment.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h"
namespace {
const char kKDE4SessionEnvVar[] = "KDE_SESSION_VERSION";
}
namespace base {
namespace nix {
const char kDotConfigDir[] = ".config";
const char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";
FilePath GetXDGDirectory(Environment* env, const char* env_name,
const char* fallback_dir) {
FilePath path;
std::string env_value;
if (env->GetVar(env_name, &env_value) && !env_value.empty())
path = FilePath(env_value);
else
path = GetHomeDir().Append(fallback_dir);
return path.StripTrailingSeparators();
}
FilePath GetXDGUserDirectory(const char* dir_name, const char* fallback_dir) {
FilePath path;
char* xdg_dir = xdg_user_dir_lookup(dir_name);
if (xdg_dir) {
path = FilePath(xdg_dir);
free(xdg_dir);
} else {
path = GetHomeDir().Append(fallback_dir);
}
return path.StripTrailingSeparators();
}
DesktopEnvironment GetDesktopEnvironment(Environment* env) {
std::string xdg_current_desktop;
if (env->GetVar("XDG_CURRENT_DESKTOP", &xdg_current_desktop)) {
if (xdg_current_desktop == "Unity")
return DESKTOP_ENVIRONMENT_UNITY;
else if (xdg_current_desktop == "GNOME")
return DESKTOP_ENVIRONMENT_GNOME;
}
std::string desktop_session;
if (env->GetVar("DESKTOP_SESSION", &desktop_session)) {
if (desktop_session == "gnome") {
return DESKTOP_ENVIRONMENT_GNOME;
} else if (desktop_session == "kde4") {
return DESKTOP_ENVIRONMENT_KDE4;
} else if (desktop_session == "kde") {
if (env->HasVar(kKDE4SessionEnvVar))
return DESKTOP_ENVIRONMENT_KDE4;
return DESKTOP_ENVIRONMENT_KDE3;
} else if (desktop_session.find("xfce") != std::string::npos ||
desktop_session == "xubuntu") {
return DESKTOP_ENVIRONMENT_XFCE;
}
}
if (env->HasVar("GNOME_DESKTOP_SESSION_ID")) {
return DESKTOP_ENVIRONMENT_GNOME;
} else if (env->HasVar("KDE_FULL_SESSION")) {
if (env->HasVar(kKDE4SessionEnvVar))
return DESKTOP_ENVIRONMENT_KDE4;
return DESKTOP_ENVIRONMENT_KDE3;
}
return DESKTOP_ENVIRONMENT_OTHER;
}
const char* GetDesktopEnvironmentName(DesktopEnvironment env) {
switch (env) {
case DESKTOP_ENVIRONMENT_OTHER:
return NULL;
case DESKTOP_ENVIRONMENT_GNOME:
return "GNOME";
case DESKTOP_ENVIRONMENT_KDE3:
return "KDE3";
case DESKTOP_ENVIRONMENT_KDE4:
return "KDE4";
case DESKTOP_ENVIRONMENT_UNITY:
return "UNITY";
case DESKTOP_ENVIRONMENT_XFCE:
return "XFCE";
}
return NULL;
}
const char* GetDesktopEnvironmentName(Environment* env) {
return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
}
}
}