1 // Copyright 2014 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/libgtk2ui/gconf_listener.h"
10 #include "base/callback.h"
11 #include "base/environment.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/nix/xdg_util.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_tokenizer.h"
16 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
17 #include "ui/base/x/x11_util.h"
18 #include "ui/views/window/frame_buttons.h"
22 // The GConf key we read for the button placement string. Even through the key
23 // has "metacity" in it, it's shared between metacity and compiz.
24 const char kButtonLayoutKey[] = "/apps/metacity/general/button_layout";
26 // The GConf key we read for what to do in case of middle clicks on non client
27 // area. Even through the key has "metacity" in it, it's shared between
28 // metacity and compiz.
29 const char kMiddleClickActionKey[] =
30 "/apps/metacity/general/action_middle_click_titlebar";
32 // GConf requires us to subscribe to a parent directory before we can subscribe
33 // to changes in an individual key in that directory.
34 const char kMetacityGeneral[] = "/apps/metacity/general";
36 const char kDefaultButtonString[] = ":minimize,maximize,close";
44 GConfListener::GConfListener(Gtk2UI* delegate)
45 : delegate_(delegate),
47 scoped_ptr<base::Environment> env(base::Environment::Create());
48 base::nix::DesktopEnvironment de =
49 base::nix::GetDesktopEnvironment(env.get());
50 if (de == base::nix::DESKTOP_ENVIRONMENT_GNOME ||
51 de == base::nix::DESKTOP_ENVIRONMENT_UNITY ||
52 ui::GuessWindowManager() == ui::WM_METACITY) {
53 client_ = gconf_client_get_default();
54 // If we fail to get a context, that's OK, since we'll just fallback on
55 // not receiving gconf keys.
57 // Register that we're interested in the values of this directory.
59 gconf_client_add_dir(client_, kMetacityGeneral,
60 GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
61 if (HandleGError(error, kMetacityGeneral))
64 // Get the initial value of the keys we're interested in.
65 GetAndRegister(kButtonLayoutKey,
66 base::Bind(&GConfListener::ParseAndStoreButtonValue,
67 base::Unretained(this)));
68 GetAndRegister(kMiddleClickActionKey,
69 base::Bind(&GConfListener::ParseAndStoreMiddleClickValue,
70 base::Unretained(this)));
75 GConfListener::~GConfListener() {
80 void GConfListener::GetAndRegister(
81 const char* key_to_subscribe,
82 const base::Callback<void(GConfValue*)>& initial_setter) {
84 GConfValue* gconf_value = gconf_client_get(client_, key_to_subscribe,
86 if (HandleGError(error, key_to_subscribe))
88 initial_setter.Run(gconf_value);
90 gconf_value_free(gconf_value);
92 // Register to get notifies about changes to this key.
93 gconf_client_notify_add(
94 client_, key_to_subscribe,
95 reinterpret_cast<void (*)(GConfClient*, guint, GConfEntry*, void*)>(
96 OnChangeNotificationThunk),
98 if (HandleGError(error, key_to_subscribe))
102 void GConfListener::OnChangeNotification(GConfClient* client,
105 if (strcmp(gconf_entry_get_key(entry), kButtonLayoutKey) == 0) {
106 GConfValue* gconf_value = gconf_entry_get_value(entry);
107 ParseAndStoreButtonValue(gconf_value);
108 } else if (strcmp(gconf_entry_get_key(entry), kMiddleClickActionKey) == 0) {
109 GConfValue* gconf_value = gconf_entry_get_value(entry);
110 ParseAndStoreMiddleClickValue(gconf_value);
114 bool GConfListener::HandleGError(GError* error, const char* key) {
116 LOG(ERROR) << "Error with gconf key '" << key << "': " << error->message;
118 g_object_unref(client_);
125 void GConfListener::ParseAndStoreButtonValue(GConfValue* gconf_value) {
126 std::string button_string;
128 const char* value = gconf_value_get_string(gconf_value);
129 button_string = value ? value : kDefaultButtonString;
131 button_string = kDefaultButtonString;
134 // Parse the button_layout string.
135 std::vector<views::FrameButton> leading_buttons;
136 std::vector<views::FrameButton> trailing_buttons;
137 bool left_side = true;
138 base::StringTokenizer tokenizer(button_string, ":,");
139 tokenizer.set_options(base::StringTokenizer::RETURN_DELIMS);
140 while (tokenizer.GetNext()) {
141 if (tokenizer.token_is_delim()) {
142 if (*tokenizer.token_begin() == ':')
145 base::StringPiece token = tokenizer.token_piece();
146 if (token == "minimize") {
147 (left_side ? leading_buttons : trailing_buttons).push_back(
148 views::FRAME_BUTTON_MINIMIZE);
149 } else if (token == "maximize") {
150 (left_side ? leading_buttons : trailing_buttons).push_back(
151 views::FRAME_BUTTON_MAXIMIZE);
152 } else if (token == "close") {
153 (left_side ? leading_buttons : trailing_buttons).push_back(
154 views::FRAME_BUTTON_CLOSE);
159 delegate_->SetWindowButtonOrdering(leading_buttons, trailing_buttons);
162 void GConfListener::ParseAndStoreMiddleClickValue(GConfValue* gconf_value) {
163 Gtk2UI::NonClientMiddleClickAction action =
164 views::LinuxUI::MIDDLE_CLICK_ACTION_LOWER;
166 const char* value = gconf_value_get_string(gconf_value);
168 if (strcmp(value, "none") == 0) {
169 action = views::LinuxUI::MIDDLE_CLICK_ACTION_NONE;
170 } else if (strcmp(value, "lower") == 0) {
171 action = views::LinuxUI::MIDDLE_CLICK_ACTION_LOWER;
172 } else if (strcmp(value, "minimize") == 0) {
173 action = views::LinuxUI::MIDDLE_CLICK_ACTION_MINIMIZE;
174 } else if (strcmp(value, "toggle-maximize") == 0) {
175 action = views::LinuxUI::MIDDLE_CLICK_ACTION_TOGGLE_MAXIMIZE;
177 // While we want to have the default state be lower if there isn't a
178 // value, we want to default to no action if the user has explicitly
179 // chose an action that we don't implement.
180 action = views::LinuxUI::MIDDLE_CLICK_ACTION_NONE;
184 delegate_->SetNonClientMiddleClickAction(action);
187 } // namespace libgtk2ui