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. 4 5 #include "base/nix/xdg_util.h" 6 7 #include <string> 8 9 #include "base/environment.h" 10 #include "base/file_util.h" 11 #include "base/files/file_path.h" 12 #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" 13 14 namespace { 15 16 // The KDE session version environment variable used in KDE 4. 17 const char kKDE4SessionEnvVar[] = "KDE_SESSION_VERSION"; 18 19 } // namespace 20 21 namespace base { 22 namespace nix { 23 24 const char kDotConfigDir[] = ".config"; 25 const char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME"; 26 27 FilePath GetXDGDirectory(Environment* env, const char* env_name, 28 const char* fallback_dir) { 29 FilePath path; 30 std::string env_value; 31 if (env->GetVar(env_name, &env_value) && !env_value.empty()) 32 path = FilePath(env_value); 33 else 34 path = GetHomeDir().Append(fallback_dir); 35 return path.StripTrailingSeparators(); 36 } 37 38 FilePath GetXDGUserDirectory(const char* dir_name, const char* fallback_dir) { 39 FilePath path; 40 char* xdg_dir = xdg_user_dir_lookup(dir_name); 41 if (xdg_dir) { 42 path = FilePath(xdg_dir); 43 free(xdg_dir); 44 } else { 45 path = GetHomeDir().Append(fallback_dir); 46 } 47 return path.StripTrailingSeparators(); 48 } 49 50 DesktopEnvironment GetDesktopEnvironment(Environment* env) { 51 // XDG_CURRENT_DESKTOP is the newest standard circa 2012. 52 std::string xdg_current_desktop; 53 if (env->GetVar("XDG_CURRENT_DESKTOP", &xdg_current_desktop)) { 54 // Not all desktop environments set this env var as of this writing. 55 if (xdg_current_desktop == "Unity") 56 return DESKTOP_ENVIRONMENT_UNITY; 57 else if (xdg_current_desktop == "GNOME") 58 return DESKTOP_ENVIRONMENT_GNOME; 59 } 60 61 // DESKTOP_SESSION was what everyone used in 2010. 62 std::string desktop_session; 63 if (env->GetVar("DESKTOP_SESSION", &desktop_session)) { 64 if (desktop_session == "gnome") { 65 return DESKTOP_ENVIRONMENT_GNOME; 66 } else if (desktop_session == "kde4") { 67 return DESKTOP_ENVIRONMENT_KDE4; 68 } else if (desktop_session == "kde") { 69 // This may mean KDE4 on newer systems, so we have to check. 70 if (env->HasVar(kKDE4SessionEnvVar)) 71 return DESKTOP_ENVIRONMENT_KDE4; 72 return DESKTOP_ENVIRONMENT_KDE3; 73 } else if (desktop_session.find("xfce") != std::string::npos || 74 desktop_session == "xubuntu") { 75 return DESKTOP_ENVIRONMENT_XFCE; 76 } 77 } 78 79 // Fall back on some older environment variables. 80 // Useful particularly in the DESKTOP_SESSION=default case. 81 if (env->HasVar("GNOME_DESKTOP_SESSION_ID")) { 82 return DESKTOP_ENVIRONMENT_GNOME; 83 } else if (env->HasVar("KDE_FULL_SESSION")) { 84 if (env->HasVar(kKDE4SessionEnvVar)) 85 return DESKTOP_ENVIRONMENT_KDE4; 86 return DESKTOP_ENVIRONMENT_KDE3; 87 } 88 89 return DESKTOP_ENVIRONMENT_OTHER; 90 } 91 92 const char* GetDesktopEnvironmentName(DesktopEnvironment env) { 93 switch (env) { 94 case DESKTOP_ENVIRONMENT_OTHER: 95 return NULL; 96 case DESKTOP_ENVIRONMENT_GNOME: 97 return "GNOME"; 98 case DESKTOP_ENVIRONMENT_KDE3: 99 return "KDE3"; 100 case DESKTOP_ENVIRONMENT_KDE4: 101 return "KDE4"; 102 case DESKTOP_ENVIRONMENT_UNITY: 103 return "UNITY"; 104 case DESKTOP_ENVIRONMENT_XFCE: 105 return "XFCE"; 106 } 107 return NULL; 108 } 109 110 const char* GetDesktopEnvironmentName(Environment* env) { 111 return GetDesktopEnvironmentName(GetDesktopEnvironment(env)); 112 } 113 114 } // namespace nix 115 } // namespace base 116