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"
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"
16 // The KDE session version environment variable used in KDE 4.
17 const char kKDE4SessionEnvVar
[] = "KDE_SESSION_VERSION";
24 const char kDotConfigDir
[] = ".config";
25 const char kXdgConfigHomeEnvVar
[] = "XDG_CONFIG_HOME";
27 FilePath
GetXDGDirectory(Environment
* env
, const char* env_name
,
28 const char* fallback_dir
) {
30 std::string env_value
;
31 if (env
->GetVar(env_name
, &env_value
) && !env_value
.empty())
32 path
= FilePath(env_value
);
34 path
= file_util::GetHomeDir().Append(fallback_dir
);
35 return path
.StripTrailingSeparators();
38 FilePath
GetXDGUserDirectory(const char* dir_name
, const char* fallback_dir
) {
40 char* xdg_dir
= xdg_user_dir_lookup(dir_name
);
42 path
= FilePath(xdg_dir
);
45 path
= file_util::GetHomeDir().Append(fallback_dir
);
47 return path
.StripTrailingSeparators();
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
;
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
;
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
;
89 return DESKTOP_ENVIRONMENT_OTHER
;
92 const char* GetDesktopEnvironmentName(DesktopEnvironment env
) {
94 case DESKTOP_ENVIRONMENT_OTHER
:
96 case DESKTOP_ENVIRONMENT_GNOME
:
98 case DESKTOP_ENVIRONMENT_KDE3
:
100 case DESKTOP_ENVIRONMENT_KDE4
:
102 case DESKTOP_ENVIRONMENT_UNITY
:
104 case DESKTOP_ENVIRONMENT_XFCE
:
110 const char* GetDesktopEnvironmentName(Environment
* env
) {
111 return GetDesktopEnvironmentName(GetDesktopEnvironment(env
));