- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / gconf_titlebar_listener.cc
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.
4
5 #include "chrome/browser/ui/gtk/gconf_titlebar_listener.h"
6
7 #include <gtk/gtk.h>
8
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"
15
16 namespace {
17
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";
21
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";
25
26 }  // namespace
27
28 // Public interface:
29
30 // static
31 GConfTitlebarListener* GConfTitlebarListener::GetInstance() {
32   return Singleton<GConfTitlebarListener>::get();
33 }
34
35 void GConfTitlebarListener::SetTitlebarButtons(BrowserTitlebar* titlebar) {
36   if (client_) {
37     titlebar->BuildButtons(current_value_);
38     titlebars_.insert(titlebar);
39   } else {
40     titlebar->BuildButtons(BrowserTitlebar::kDefaultButtonString);
41   }
42 }
43
44 void GConfTitlebarListener::RemoveObserver(BrowserTitlebar* titlebar) {
45   titlebars_.erase(titlebar);
46 }
47
48 // Protected:
49
50 GConfTitlebarListener::~GConfTitlebarListener() {}
51
52 // Private:
53
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.
64     if (client_) {
65       // Get the initial value of the key.
66       GError* error = NULL;
67       GConfValue* gconf_value = gconf_client_get(client_, kButtonLayoutKey,
68                                                  &error);
69       if (HandleGError(error, kButtonLayoutKey))
70         return;
71       ParseAndStoreValue(gconf_value);
72       if (gconf_value)
73         gconf_value_free(gconf_value);
74
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))
79         return;
80
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),
86           this, NULL, &error);
87       if (HandleGError(error, kButtonLayoutKey))
88         return;
89     }
90   }
91 }
92
93 void GConfTitlebarListener::OnChangeNotification(GConfClient* client,
94                                                  guint cnxn_id,
95                                                  GConfEntry* entry) {
96   if (strcmp(gconf_entry_get_key(entry), kButtonLayoutKey) == 0) {
97     GConfValue* gconf_value = gconf_entry_get_value(entry);
98     ParseAndStoreValue(gconf_value);
99
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_);
104     }
105   }
106 }
107
108 bool GConfTitlebarListener::HandleGError(GError* error, const char* key) {
109   if (error != NULL) {
110     LOG(ERROR) << "Error with gconf key '" << key << "': " << error->message;
111     g_error_free(error);
112     g_object_unref(client_);
113     client_ = NULL;
114     return true;
115   }
116   return false;
117 }
118
119 void GConfTitlebarListener::ParseAndStoreValue(GConfValue* gconf_value) {
120   if (gconf_value) {
121     const char* value = gconf_value_get_string(gconf_value);
122     current_value_ = value ? value : BrowserTitlebar::kDefaultButtonString;
123   } else {
124     current_value_ = BrowserTitlebar::kDefaultButtonString;
125   }
126 }