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/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/strings/string_util.h"
15 #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h"
16 
17 namespace {
18 
19 // The KDE session version environment variable introduced in KDE 4.
20 const char kKDESessionEnvVar[] = "KDE_SESSION_VERSION";
21 
22 }  // namespace
23 
24 namespace base {
25 namespace nix {
26 
27 const char kDotConfigDir[] = ".config";
28 const char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";
29 
GetXDGDirectory(Environment * env,const char * env_name,const char * fallback_dir)30 FilePath GetXDGDirectory(Environment* env, const char* env_name,
31                          const char* fallback_dir) {
32   FilePath path;
33   std::string env_value;
34   if (env->GetVar(env_name, &env_value) && !env_value.empty()) {
35     path = FilePath(env_value);
36   } else {
37     PathService::Get(DIR_HOME, &path);
38     path = path.Append(fallback_dir);
39   }
40   return path.StripTrailingSeparators();
41 }
42 
GetXDGUserDirectory(const char * dir_name,const char * fallback_dir)43 FilePath GetXDGUserDirectory(const char* dir_name, const char* fallback_dir) {
44   FilePath path;
45   char* xdg_dir = xdg_user_dir_lookup(dir_name);
46   if (xdg_dir) {
47     path = FilePath(xdg_dir);
48     free(xdg_dir);
49   } else {
50     PathService::Get(DIR_HOME, &path);
51     path = path.Append(fallback_dir);
52   }
53   return path.StripTrailingSeparators();
54 }
55 
GetDesktopEnvironment(Environment * env)56 DesktopEnvironment GetDesktopEnvironment(Environment* env) {
57   // XDG_CURRENT_DESKTOP is the newest standard circa 2012.
58   std::string xdg_current_desktop;
59   if (env->GetVar("XDG_CURRENT_DESKTOP", &xdg_current_desktop)) {
60     // Not all desktop environments set this env var as of this writing.
61     if (base::StartsWith(xdg_current_desktop, "Unity",
62                          base::CompareCase::SENSITIVE)) {
63       // gnome-fallback sessions set XDG_CURRENT_DESKTOP to Unity
64       // DESKTOP_SESSION can be gnome-fallback or gnome-fallback-compiz
65       std::string desktop_session;
66       if (env->GetVar("DESKTOP_SESSION", &desktop_session) &&
67           desktop_session.find("gnome-fallback") != std::string::npos) {
68         return DESKTOP_ENVIRONMENT_GNOME;
69       }
70       return DESKTOP_ENVIRONMENT_UNITY;
71     }
72     if (xdg_current_desktop == "GNOME")
73       return DESKTOP_ENVIRONMENT_GNOME;
74     if (xdg_current_desktop == "X-Cinnamon")
75       return DESKTOP_ENVIRONMENT_CINNAMON;
76     if (xdg_current_desktop == "KDE") {
77       std::string kde_session;
78       if (env->GetVar(kKDESessionEnvVar, &kde_session)) {
79         if (kde_session == "5") {
80           return DESKTOP_ENVIRONMENT_KDE5;
81         }
82       }
83       return DESKTOP_ENVIRONMENT_KDE4;
84     }
85     if (xdg_current_desktop == "Pantheon")
86       return DESKTOP_ENVIRONMENT_PANTHEON;
87     if (xdg_current_desktop == "XFCE")
88       return DESKTOP_ENVIRONMENT_XFCE;
89   }
90 
91   // DESKTOP_SESSION was what everyone used in 2010.
92   std::string desktop_session;
93   if (env->GetVar("DESKTOP_SESSION", &desktop_session)) {
94     if (desktop_session == "gnome" || desktop_session == "mate")
95       return DESKTOP_ENVIRONMENT_GNOME;
96     if (desktop_session == "kde4" || desktop_session == "kde-plasma")
97       return DESKTOP_ENVIRONMENT_KDE4;
98     if (desktop_session == "kde") {
99       // This may mean KDE4 on newer systems, so we have to check.
100       if (env->HasVar(kKDESessionEnvVar))
101         return DESKTOP_ENVIRONMENT_KDE4;
102       return DESKTOP_ENVIRONMENT_KDE3;
103     }
104     if (desktop_session.find("xfce") != std::string::npos ||
105         desktop_session == "xubuntu") {
106       return DESKTOP_ENVIRONMENT_XFCE;
107     }
108   }
109 
110   // Fall back on some older environment variables.
111   // Useful particularly in the DESKTOP_SESSION=default case.
112   if (env->HasVar("GNOME_DESKTOP_SESSION_ID"))
113     return DESKTOP_ENVIRONMENT_GNOME;
114   if (env->HasVar("KDE_FULL_SESSION")) {
115     if (env->HasVar(kKDESessionEnvVar))
116       return DESKTOP_ENVIRONMENT_KDE4;
117     return DESKTOP_ENVIRONMENT_KDE3;
118   }
119 
120   return DESKTOP_ENVIRONMENT_OTHER;
121 }
122 
GetDesktopEnvironmentName(DesktopEnvironment env)123 const char* GetDesktopEnvironmentName(DesktopEnvironment env) {
124   switch (env) {
125     case DESKTOP_ENVIRONMENT_OTHER:
126       return nullptr;
127     case DESKTOP_ENVIRONMENT_CINNAMON:
128       return "CINNAMON";
129     case DESKTOP_ENVIRONMENT_GNOME:
130       return "GNOME";
131     case DESKTOP_ENVIRONMENT_KDE3:
132       return "KDE3";
133     case DESKTOP_ENVIRONMENT_KDE4:
134       return "KDE4";
135     case DESKTOP_ENVIRONMENT_KDE5:
136       return "KDE5";
137     case DESKTOP_ENVIRONMENT_PANTHEON:
138       return "PANTHEON";
139     case DESKTOP_ENVIRONMENT_UNITY:
140       return "UNITY";
141     case DESKTOP_ENVIRONMENT_XFCE:
142       return "XFCE";
143   }
144   return nullptr;
145 }
146 
GetDesktopEnvironmentName(Environment * env)147 const char* GetDesktopEnvironmentName(Environment* env) {
148   return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
149 }
150 
151 }  // namespace nix
152 }  // namespace base
153