1 // Copyright (c) 2011 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 "chrome/browser/ui/gtk/gconf_titlebar_listener.h"
9 #include "base/environment.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/singleton.h"
12 #include "base/nix/xdg_util.h"
13 #include "chrome/browser/ui/gtk/browser_titlebar.h"
14 #include "ui/base/x/x11_util.h"
18 // The GConf key we read for the button placement string. Even through the key
19 // has "metacity" in it, it's shared between metacity and compiz.
20 const char* kButtonLayoutKey = "/apps/metacity/general/button_layout";
22 // GConf requires us to subscribe to a parent directory before we can subscribe
23 // to changes in an individual key in that directory.
24 const char* kMetacityGeneral = "/apps/metacity/general";
31 GConfTitlebarListener* GConfTitlebarListener::GetInstance() {
32 return Singleton<GConfTitlebarListener>::get();
35 void GConfTitlebarListener::SetTitlebarButtons(BrowserTitlebar* titlebar) {
37 titlebar->BuildButtons(current_value_);
38 titlebars_.insert(titlebar);
40 titlebar->BuildButtons(BrowserTitlebar::kDefaultButtonString);
44 void GConfTitlebarListener::RemoveObserver(BrowserTitlebar* titlebar) {
45 titlebars_.erase(titlebar);
50 GConfTitlebarListener::~GConfTitlebarListener() {}
54 GConfTitlebarListener::GConfTitlebarListener() : client_(NULL) {
55 scoped_ptr<base::Environment> env(base::Environment::Create());
56 base::nix::DesktopEnvironment de =
57 base::nix::GetDesktopEnvironment(env.get());
58 if (de == base::nix::DESKTOP_ENVIRONMENT_GNOME ||
59 de == base::nix::DESKTOP_ENVIRONMENT_UNITY ||
60 ui::GuessWindowManager() == ui::WM_METACITY) {
61 client_ = gconf_client_get_default();
62 // If we fail to get a context, that's OK, since we'll just fallback on
63 // not receiving gconf keys.
65 // Get the initial value of the key.
67 GConfValue* gconf_value = gconf_client_get(client_, kButtonLayoutKey,
69 if (HandleGError(error, kButtonLayoutKey))
71 ParseAndStoreValue(gconf_value);
73 gconf_value_free(gconf_value);
75 // Register that we're interested in the values of this directory.
76 gconf_client_add_dir(client_, kMetacityGeneral,
77 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
78 if (HandleGError(error, kMetacityGeneral))
81 // Register to get notifies about changes to this key.
82 gconf_client_notify_add(
83 client_, kButtonLayoutKey,
84 reinterpret_cast<void (*)(GConfClient*, guint, GConfEntry*, void*)>(
85 OnChangeNotificationThunk),
87 if (HandleGError(error, kButtonLayoutKey))
93 void GConfTitlebarListener::OnChangeNotification(GConfClient* client,
96 if (strcmp(gconf_entry_get_key(entry), kButtonLayoutKey) == 0) {
97 GConfValue* gconf_value = gconf_entry_get_value(entry);
98 ParseAndStoreValue(gconf_value);
100 // Broadcast the new configuration to all windows:
101 for (std::set<BrowserTitlebar*>::const_iterator it = titlebars_.begin();
102 it != titlebars_.end(); ++it) {
103 (*it)->BuildButtons(current_value_);
108 bool GConfTitlebarListener::HandleGError(GError* error, const char* key) {
110 LOG(ERROR) << "Error with gconf key '" << key << "': " << error->message;
112 g_object_unref(client_);
119 void GConfTitlebarListener::ParseAndStoreValue(GConfValue* gconf_value) {
121 const char* value = gconf_value_get_string(gconf_value);
122 current_value_ = value ? value : BrowserTitlebar::kDefaultButtonString;
124 current_value_ = BrowserTitlebar::kDefaultButtonString;