summaryrefslogtreecommitdiff
path: root/libgnome-control-center-extension/cc-panel.c
blob: 29783abc1d7fafd72e86f01551baa43ab02a7708 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#include <gtk/gtk.h>
#include <gio/gio.h>

#include "cc-panel.h"
#include "cc-page.h"

#define CC_PANEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_PANEL, CcPanelPrivate))

struct CcPanelPrivate
{
        char            *id;
        char            *display_name;
        char            *category;
        char            *current_location;

        gboolean         is_active;
        CcPage          *current_page;
};

enum {
        PROP_0,
        PROP_ID,
        PROP_DISPLAY_NAME,
        PROP_CATEGORY,
        PROP_CURRENT_LOCATION,
        PROP_CURRENT_PAGE,
};

enum {
        ACTIVE_CHANGED,
        LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0, };

static void     cc_panel_class_init    (CcPanelClass *klass);
static void     cc_panel_init          (CcPanel      *panel);
static void     cc_panel_finalize      (GObject       *object);

G_DEFINE_ABSTRACT_TYPE (CcPanel, cc_panel, GTK_TYPE_ALIGNMENT)

static void
_cc_panel_set_id (CcPanel    *panel,
                  const char *id)
{
        g_free (panel->priv->id);
        panel->priv->id = g_strdup (id);
}

static void
_cc_panel_set_display_name (CcPanel    *panel,
                            const char *name)
{
        g_free (panel->priv->display_name);
        panel->priv->display_name = g_strdup (name);
}

static void
_cc_panel_set_current_page (CcPanel    *panel,
                            CcPage     *page)
{
        CcPage *old;

        if (page == panel->priv->current_page)
                return;

        old = panel->priv->current_page;
        panel->priv->current_page = page;
        if (old != NULL) {
                cc_page_set_active (old, FALSE);
        }
        if (panel->priv->current_page != NULL) {
                g_object_ref (panel->priv->current_page);
                cc_page_set_active (panel->priv->current_page,
                                    panel->priv->is_active);
        }
        if (old != NULL) {
                g_object_unref (old);
        }

        g_object_notify (G_OBJECT (panel), "current-page");
}

static void
cc_panel_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
        CcPanel *self;

        self = CC_PANEL (object);

        switch (prop_id) {
        case PROP_ID:
                _cc_panel_set_id (self, g_value_get_string (value));
                break;
        case PROP_DISPLAY_NAME:
                _cc_panel_set_display_name (self, g_value_get_string (value));
                break;
        case PROP_CURRENT_PAGE:
                _cc_panel_set_current_page (self, g_value_get_object (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
cc_panel_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
        CcPanel *self;

        self = CC_PANEL (object);

        switch (prop_id) {
        case PROP_ID:
                g_value_set_string (value, self->priv->id);
                break;
        case PROP_DISPLAY_NAME:
                g_value_set_string (value, self->priv->display_name);
                break;
        case PROP_CURRENT_PAGE:
                g_value_set_object (value, self->priv->current_page);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
cc_panel_real_active_changed (CcPanel *panel,
                              gboolean is_active)
{
        if (panel->priv->is_active == is_active)
                return;

        panel->priv->is_active = is_active;
        if (panel->priv->current_page != NULL) {
                cc_page_set_active (panel->priv->current_page, is_active);
        }
        g_debug ("Panel %s is %s",
                 panel->priv->id,
                 panel->priv->is_active ? "active" : "inactive");
}

void
cc_panel_set_active (CcPanel *panel,
                     gboolean is_active)
{
        g_return_if_fail (CC_IS_PANEL (panel));

        g_object_ref (panel);
        gtk_widget_queue_resize (GTK_WIDGET (panel));
        if (panel->priv->is_active != is_active) {
                g_signal_emit (panel, signals [ACTIVE_CHANGED], 0, is_active);
        }
        g_object_unref (panel);
}

gboolean
cc_panel_is_active (CcPanel *panel)
{
        g_return_val_if_fail (CC_IS_PANEL (panel), FALSE);
        return panel->priv->is_active;
}

static GObject *
cc_panel_constructor (GType                  type,
                      guint                  n_construct_properties,
                      GObjectConstructParam *construct_properties)
{
        CcPanel      *panel;

        panel = CC_PANEL (G_OBJECT_CLASS (cc_panel_parent_class)->constructor (type,
                                                                               n_construct_properties,
                                                                               construct_properties));

        return G_OBJECT (panel);
}

static void
cc_panel_class_init (CcPanelClass *klass)
{
        GObjectClass    *object_class = G_OBJECT_CLASS (klass);

        object_class->get_property = cc_panel_get_property;
        object_class->set_property = cc_panel_set_property;
        object_class->constructor = cc_panel_constructor;
        object_class->finalize = cc_panel_finalize;

        klass->active_changed = cc_panel_real_active_changed;

        g_type_class_add_private (klass, sizeof (CcPanelPrivate));

        signals [ACTIVE_CHANGED]
                = g_signal_new ("active-changed",
                                G_TYPE_FROM_CLASS (object_class),
                                G_SIGNAL_RUN_FIRST,
                                G_STRUCT_OFFSET (CcPanelClass, active_changed),
                                NULL,
                                NULL,
                                g_cclosure_marshal_VOID__BOOLEAN,
                                G_TYPE_NONE,
                                1, G_TYPE_BOOLEAN);

        g_object_class_install_property (object_class,
                                         PROP_ID,
                                         g_param_spec_string ("id",
                                                              "id",
                                                              "id",
                                                              NULL,
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
        g_object_class_install_property (object_class,
                                         PROP_DISPLAY_NAME,
                                         g_param_spec_string ("display-name",
                                                              "display name",
                                                              "display name",
                                                              NULL,
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
        g_object_class_install_property (object_class,
                                         PROP_CURRENT_PAGE,
                                         g_param_spec_object ("current-page",
                                                              "",
                                                              "",
                                                              CC_TYPE_PAGE,
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

}

static void
cc_panel_init (CcPanel *panel)
{

        panel->priv = CC_PANEL_GET_PRIVATE (panel);
}

static void
cc_panel_finalize (GObject *object)
{
        CcPanel *panel;

        g_return_if_fail (object != NULL);
        g_return_if_fail (CC_IS_PANEL (object));

        panel = CC_PANEL (object);

        g_return_if_fail (panel->priv != NULL);

        g_free (panel->priv->id);
        g_free (panel->priv->display_name);

        G_OBJECT_CLASS (cc_panel_parent_class)->finalize (object);
}