summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/gconf_titlebar_listener.cc
blob: 90bae20608c90a4df3d8f604c461b8fd74e0856b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/gtk/gconf_titlebar_listener.h"

#include <gtk/gtk.h>

#include "base/environment.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/nix/xdg_util.h"
#include "chrome/browser/ui/gtk/browser_titlebar.h"
#include "ui/base/x/x11_util.h"

namespace {

// The GConf key we read for the button placement string. Even through the key
// has "metacity" in it, it's shared between metacity and compiz.
const char* kButtonLayoutKey = "/apps/metacity/general/button_layout";

// GConf requires us to subscribe to a parent directory before we can subscribe
// to changes in an individual key in that directory.
const char* kMetacityGeneral = "/apps/metacity/general";

}  // namespace

// Public interface:

// static
GConfTitlebarListener* GConfTitlebarListener::GetInstance() {
  return Singleton<GConfTitlebarListener>::get();
}

void GConfTitlebarListener::SetTitlebarButtons(BrowserTitlebar* titlebar) {
  if (client_) {
    titlebar->BuildButtons(current_value_);
    titlebars_.insert(titlebar);
  } else {
    titlebar->BuildButtons(BrowserTitlebar::kDefaultButtonString);
  }
}

void GConfTitlebarListener::RemoveObserver(BrowserTitlebar* titlebar) {
  titlebars_.erase(titlebar);
}

// Protected:

GConfTitlebarListener::~GConfTitlebarListener() {}

// Private:

GConfTitlebarListener::GConfTitlebarListener() : client_(NULL) {
  scoped_ptr<base::Environment> env(base::Environment::Create());
  base::nix::DesktopEnvironment de =
      base::nix::GetDesktopEnvironment(env.get());
  if (de == base::nix::DESKTOP_ENVIRONMENT_GNOME ||
      de == base::nix::DESKTOP_ENVIRONMENT_UNITY ||
      ui::GuessWindowManager() == ui::WM_METACITY) {
    client_ = gconf_client_get_default();
    // If we fail to get a context, that's OK, since we'll just fallback on
    // not receiving gconf keys.
    if (client_) {
      // Get the initial value of the key.
      GError* error = NULL;
      GConfValue* gconf_value = gconf_client_get(client_, kButtonLayoutKey,
                                                 &error);
      if (HandleGError(error, kButtonLayoutKey))
        return;
      ParseAndStoreValue(gconf_value);
      if (gconf_value)
        gconf_value_free(gconf_value);

      // Register that we're interested in the values of this directory.
      gconf_client_add_dir(client_, kMetacityGeneral,
                           GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
      if (HandleGError(error, kMetacityGeneral))
        return;

      // Register to get notifies about changes to this key.
      gconf_client_notify_add(
          client_, kButtonLayoutKey,
          reinterpret_cast<void (*)(GConfClient*, guint, GConfEntry*, void*)>(
              OnChangeNotificationThunk),
          this, NULL, &error);
      if (HandleGError(error, kButtonLayoutKey))
        return;
    }
  }
}

void GConfTitlebarListener::OnChangeNotification(GConfClient* client,
                                                 guint cnxn_id,
                                                 GConfEntry* entry) {
  if (strcmp(gconf_entry_get_key(entry), kButtonLayoutKey) == 0) {
    GConfValue* gconf_value = gconf_entry_get_value(entry);
    ParseAndStoreValue(gconf_value);

    // Broadcast the new configuration to all windows:
    for (std::set<BrowserTitlebar*>::const_iterator it = titlebars_.begin();
         it != titlebars_.end(); ++it) {
      (*it)->BuildButtons(current_value_);
    }
  }
}

bool GConfTitlebarListener::HandleGError(GError* error, const char* key) {
  if (error != NULL) {
    LOG(ERROR) << "Error with gconf key '" << key << "': " << error->message;
    g_error_free(error);
    g_object_unref(client_);
    client_ = NULL;
    return true;
  }
  return false;
}

void GConfTitlebarListener::ParseAndStoreValue(GConfValue* gconf_value) {
  if (gconf_value) {
    const char* value = gconf_value_get_string(gconf_value);
    current_value_ = value ? value : BrowserTitlebar::kDefaultButtonString;
  } else {
    current_value_ = BrowserTitlebar::kDefaultButtonString;
  }
}