• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 "base/environment.h"
8 #include "base/file_path.h"
9 #include "base/file_util.h"
10 #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h"
11 
12 namespace base {
13 namespace nix {
14 
GetXDGDirectory(Environment * env,const char * env_name,const char * fallback_dir)15 FilePath GetXDGDirectory(Environment* env, const char* env_name,
16                          const char* fallback_dir) {
17   std::string env_value;
18   if (env->GetVar(env_name, &env_value) && !env_value.empty())
19     return FilePath(env_value);
20   return file_util::GetHomeDir().Append(fallback_dir);
21 }
22 
GetXDGUserDirectory(Environment * env,const char * dir_name,const char * fallback_dir)23 FilePath GetXDGUserDirectory(Environment* env, const char* dir_name,
24                              const char* fallback_dir) {
25   char* xdg_dir = xdg_user_dir_lookup(dir_name);
26   if (xdg_dir) {
27     FilePath rv(xdg_dir);
28     free(xdg_dir);
29     return rv;
30   }
31   return file_util::GetHomeDir().Append(fallback_dir);
32 }
33 
GetDesktopEnvironment(Environment * env)34 DesktopEnvironment GetDesktopEnvironment(Environment* env) {
35   std::string desktop_session;
36   if (env->GetVar("DESKTOP_SESSION", &desktop_session)) {
37     if (desktop_session == "gnome") {
38       return DESKTOP_ENVIRONMENT_GNOME;
39     } else if (desktop_session == "kde4") {
40       return DESKTOP_ENVIRONMENT_KDE4;
41     } else if (desktop_session == "kde") {
42       // This may mean KDE4 on newer systems, so we have to check.
43       if (env->HasVar("KDE_SESSION_VERSION"))
44         return DESKTOP_ENVIRONMENT_KDE4;
45       return DESKTOP_ENVIRONMENT_KDE3;
46     } else if (desktop_session.find("xfce") != std::string::npos ||
47                desktop_session == "xubuntu") {
48       return DESKTOP_ENVIRONMENT_XFCE;
49     }
50   }
51 
52   // Fall back on some older environment variables.
53   // Useful particularly in the DESKTOP_SESSION=default case.
54   if (env->HasVar("GNOME_DESKTOP_SESSION_ID")) {
55     return DESKTOP_ENVIRONMENT_GNOME;
56   } else if (env->HasVar("KDE_FULL_SESSION")) {
57     if (env->HasVar("KDE_SESSION_VERSION"))
58       return DESKTOP_ENVIRONMENT_KDE4;
59     return DESKTOP_ENVIRONMENT_KDE3;
60   }
61 
62   return DESKTOP_ENVIRONMENT_OTHER;
63 }
64 
GetDesktopEnvironmentName(DesktopEnvironment env)65 const char* GetDesktopEnvironmentName(DesktopEnvironment env) {
66   switch (env) {
67     case DESKTOP_ENVIRONMENT_OTHER:
68       return NULL;
69     case DESKTOP_ENVIRONMENT_GNOME:
70       return "GNOME";
71     case DESKTOP_ENVIRONMENT_KDE3:
72       return "KDE3";
73     case DESKTOP_ENVIRONMENT_KDE4:
74       return "KDE4";
75     case DESKTOP_ENVIRONMENT_XFCE:
76       return "XFCE";
77   }
78   return NULL;
79 }
80 
GetDesktopEnvironmentName(Environment * env)81 const char* GetDesktopEnvironmentName(Environment* env) {
82   return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
83 }
84 
85 }  // namespace nix
86 }  // namespace base
87