Temporarily make lastchange use only the git hash.
[chromium-blink-merge.git] / base / nix / xdg_util.cc
blobe889b5681a035b987ecf9e9118a5dece378cc0f3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/nix/xdg_util.h"
7 #include <string>
9 #include "base/base_paths.h"
10 #include "base/environment.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/path_service.h"
14 #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h"
16 namespace {
18 // The KDE session version environment variable used in KDE 4.
19 const char kKDE4SessionEnvVar[] = "KDE_SESSION_VERSION";
21 } // namespace
23 namespace base {
24 namespace nix {
26 const char kDotConfigDir[] = ".config";
27 const char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";
29 FilePath GetXDGDirectory(Environment* env, const char* env_name,
30 const char* fallback_dir) {
31 FilePath path;
32 std::string env_value;
33 if (env->GetVar(env_name, &env_value) && !env_value.empty()) {
34 path = FilePath(env_value);
35 } else {
36 PathService::Get(base::DIR_HOME, &path);
37 path = path.Append(fallback_dir);
39 return path.StripTrailingSeparators();
42 FilePath GetXDGUserDirectory(const char* dir_name, const char* fallback_dir) {
43 FilePath path;
44 char* xdg_dir = xdg_user_dir_lookup(dir_name);
45 if (xdg_dir) {
46 path = FilePath(xdg_dir);
47 free(xdg_dir);
48 } else {
49 PathService::Get(base::DIR_HOME, &path);
50 path = path.Append(fallback_dir);
52 return path.StripTrailingSeparators();
55 DesktopEnvironment GetDesktopEnvironment(Environment* env) {
56 // XDG_CURRENT_DESKTOP is the newest standard circa 2012.
57 std::string xdg_current_desktop;
58 if (env->GetVar("XDG_CURRENT_DESKTOP", &xdg_current_desktop)) {
59 // Not all desktop environments set this env var as of this writing.
60 if (xdg_current_desktop == "Unity")
61 return DESKTOP_ENVIRONMENT_UNITY;
62 else if (xdg_current_desktop == "GNOME")
63 return DESKTOP_ENVIRONMENT_GNOME;
66 // DESKTOP_SESSION was what everyone used in 2010.
67 std::string desktop_session;
68 if (env->GetVar("DESKTOP_SESSION", &desktop_session)) {
69 if (desktop_session == "gnome") {
70 return DESKTOP_ENVIRONMENT_GNOME;
71 } else if (desktop_session == "kde4") {
72 return DESKTOP_ENVIRONMENT_KDE4;
73 } else if (desktop_session == "kde") {
74 // This may mean KDE4 on newer systems, so we have to check.
75 if (env->HasVar(kKDE4SessionEnvVar))
76 return DESKTOP_ENVIRONMENT_KDE4;
77 return DESKTOP_ENVIRONMENT_KDE3;
78 } else if (desktop_session.find("xfce") != std::string::npos ||
79 desktop_session == "xubuntu") {
80 return DESKTOP_ENVIRONMENT_XFCE;
84 // Fall back on some older environment variables.
85 // Useful particularly in the DESKTOP_SESSION=default case.
86 if (env->HasVar("GNOME_DESKTOP_SESSION_ID")) {
87 return DESKTOP_ENVIRONMENT_GNOME;
88 } else if (env->HasVar("KDE_FULL_SESSION")) {
89 if (env->HasVar(kKDE4SessionEnvVar))
90 return DESKTOP_ENVIRONMENT_KDE4;
91 return DESKTOP_ENVIRONMENT_KDE3;
94 return DESKTOP_ENVIRONMENT_OTHER;
97 const char* GetDesktopEnvironmentName(DesktopEnvironment env) {
98 switch (env) {
99 case DESKTOP_ENVIRONMENT_OTHER:
100 return NULL;
101 case DESKTOP_ENVIRONMENT_GNOME:
102 return "GNOME";
103 case DESKTOP_ENVIRONMENT_KDE3:
104 return "KDE3";
105 case DESKTOP_ENVIRONMENT_KDE4:
106 return "KDE4";
107 case DESKTOP_ENVIRONMENT_UNITY:
108 return "UNITY";
109 case DESKTOP_ENVIRONMENT_XFCE:
110 return "XFCE";
112 return NULL;
115 const char* GetDesktopEnvironmentName(Environment* env) {
116 return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
119 } // namespace nix
120 } // namespace base