Home | History | Annotate | Download | only in nix
      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/third_party/xdg_user_dirs/xdg_user_dir_lookup.h"
     15 
     16 namespace {
     17 
     18 // The KDE session version environment variable used in KDE 4.
     19 const char kKDE4SessionEnvVar[] = "KDE_SESSION_VERSION";
     20 
     21 }  // namespace
     22 
     23 namespace base {
     24 namespace nix {
     25 
     26 const char kDotConfigDir[] = ".config";
     27 const char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";
     28 
     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);
     38   }
     39   return path.StripTrailingSeparators();
     40 }
     41 
     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);
     51   }
     52   return path.StripTrailingSeparators();
     53 }
     54 
     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       // gnome-fallback sessions set XDG_CURRENT_DESKTOP to Unity
     62       // DESKTOP_SESSION can be gnome-fallback or gnome-fallback-compiz
     63       std::string desktop_session;
     64       if (env->GetVar("DESKTOP_SESSION", &desktop_session) &&
     65           desktop_session.find("gnome-fallback") != std::string::npos) {
     66         return DESKTOP_ENVIRONMENT_GNOME;
     67       }
     68       return DESKTOP_ENVIRONMENT_UNITY;
     69     } else if (xdg_current_desktop == "GNOME") {
     70       return DESKTOP_ENVIRONMENT_GNOME;
     71     }
     72   }
     73 
     74   // DESKTOP_SESSION was what everyone used in 2010.
     75   std::string desktop_session;
     76   if (env->GetVar("DESKTOP_SESSION", &desktop_session)) {
     77     if (desktop_session == "gnome") {
     78       return DESKTOP_ENVIRONMENT_GNOME;
     79     } else if (desktop_session == "kde4") {
     80       return DESKTOP_ENVIRONMENT_KDE4;
     81     } else if (desktop_session == "kde") {
     82       // This may mean KDE4 on newer systems, so we have to check.
     83       if (env->HasVar(kKDE4SessionEnvVar))
     84         return DESKTOP_ENVIRONMENT_KDE4;
     85       return DESKTOP_ENVIRONMENT_KDE3;
     86     } else if (desktop_session.find("xfce") != std::string::npos ||
     87                desktop_session == "xubuntu") {
     88       return DESKTOP_ENVIRONMENT_XFCE;
     89     }
     90   }
     91 
     92   // Fall back on some older environment variables.
     93   // Useful particularly in the DESKTOP_SESSION=default case.
     94   if (env->HasVar("GNOME_DESKTOP_SESSION_ID")) {
     95     return DESKTOP_ENVIRONMENT_GNOME;
     96   } else if (env->HasVar("KDE_FULL_SESSION")) {
     97     if (env->HasVar(kKDE4SessionEnvVar))
     98       return DESKTOP_ENVIRONMENT_KDE4;
     99     return DESKTOP_ENVIRONMENT_KDE3;
    100   }
    101 
    102   return DESKTOP_ENVIRONMENT_OTHER;
    103 }
    104 
    105 const char* GetDesktopEnvironmentName(DesktopEnvironment env) {
    106   switch (env) {
    107     case DESKTOP_ENVIRONMENT_OTHER:
    108       return NULL;
    109     case DESKTOP_ENVIRONMENT_GNOME:
    110       return "GNOME";
    111     case DESKTOP_ENVIRONMENT_KDE3:
    112       return "KDE3";
    113     case DESKTOP_ENVIRONMENT_KDE4:
    114       return "KDE4";
    115     case DESKTOP_ENVIRONMENT_UNITY:
    116       return "UNITY";
    117     case DESKTOP_ENVIRONMENT_XFCE:
    118       return "XFCE";
    119   }
    120   return NULL;
    121 }
    122 
    123 const char* GetDesktopEnvironmentName(Environment* env) {
    124   return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
    125 }
    126 
    127 }  // namespace nix
    128 }  // namespace base
    129