Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / base / nix / xdg_util.cc
blobb3caf2abe0b020d32aa67917bc1a2fc3f6d57433
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"
7 #include <string>
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"
14 namespace {
16 // The KDE session version environment variable used in KDE 4.
17 const char kKDE4SessionEnvVar[] = "KDE_SESSION_VERSION";
19 } // namespace
21 namespace base {
22 namespace nix {
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) {
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 = file_util::GetHomeDir().Append(fallback_dir);
35 return path.StripTrailingSeparators();
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 = 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) {
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";
107 return NULL;
110 const char* GetDesktopEnvironmentName(Environment* env) {
111 return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
114 } // namespace nix
115 } // namespace base