evolution-3.6.4/widgets/misc/e-online-button.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 16  */
 17 
 18 #ifdef HAVE_CONFIG_H
 19 #include <config.h>
 20 #endif
 21 
 22 #include "e-online-button.h"
 23 
 24 #include <glib/gi18n.h>
 25 
 26 #define E_ONLINE_BUTTON_GET_PRIVATE(obj) \
 27 	(G_TYPE_INSTANCE_GET_PRIVATE \
 28 	((obj), E_TYPE_ONLINE_BUTTON, EOnlineButtonPrivate))
 29 
 30 #define ONLINE_TOOLTIP \
 31 	_("Evolution is currently online.  Click this button to work offline.")
 32 
 33 #define OFFLINE_TOOLTIP \
 34 	_("Evolution is currently offline.  Click this button to work online.")
 35 
 36 #define NETWORK_UNAVAILABLE_TOOLTIP \
 37 	_("Evolution is currently offline because the network is unavailable.")
 38 
 39 struct _EOnlineButtonPrivate {
 40 	GtkWidget *image;
 41 	gboolean online;
 42 };
 43 
 44 enum {
 45 	PROP_0,
 46 	PROP_ONLINE
 47 };
 48 
 49 G_DEFINE_TYPE (
 50 	EOnlineButton,
 51 	e_online_button,
 52 	GTK_TYPE_BUTTON)
 53 
 54 static void
 55 online_button_update_tooltip (EOnlineButton *button)
 56 {
 57 	const gchar *tooltip;
 58 
 59 	if (e_online_button_get_online (button))
 60 		tooltip = ONLINE_TOOLTIP;
 61 	else if (gtk_widget_get_sensitive (GTK_WIDGET (button)))
 62 		tooltip = OFFLINE_TOOLTIP;
 63 	else
 64 		tooltip = NETWORK_UNAVAILABLE_TOOLTIP;
 65 
 66 	gtk_widget_set_tooltip_text (GTK_WIDGET (button), tooltip);
 67 }
 68 
 69 static void
 70 online_button_set_property (GObject *object,
 71                              guint property_id,
 72                              const GValue *value,
 73                              GParamSpec *pspec)
 74 {
 75 	switch (property_id) {
 76 		case PROP_ONLINE:
 77 			e_online_button_set_online (
 78 				E_ONLINE_BUTTON (object),
 79 				g_value_get_boolean (value));
 80 			return;
 81 	}
 82 
 83 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 84 }
 85 
 86 static void
 87 online_button_get_property (GObject *object,
 88                              guint property_id,
 89                              GValue *value,
 90                              GParamSpec *pspec)
 91 {
 92 	switch (property_id) {
 93 		case PROP_ONLINE:
 94 			g_value_set_boolean (
 95 				value, e_online_button_get_online (
 96 				E_ONLINE_BUTTON (object)));
 97 			return;
 98 	}
 99 
100 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
101 }
102 
103 static void
104 online_button_dispose (GObject *object)
105 {
106 	EOnlineButtonPrivate *priv;
107 
108 	priv = E_ONLINE_BUTTON_GET_PRIVATE (object);
109 
110 	if (priv->image != NULL) {
111 		g_object_unref (priv->image);
112 		priv->image = NULL;
113 	}
114 
115 	/* Chain up to parent's dispose() method. */
116 	G_OBJECT_CLASS (e_online_button_parent_class)->dispose (object);
117 }
118 
119 static void
120 e_online_button_class_init (EOnlineButtonClass *class)
121 {
122 	GObjectClass *object_class;
123 
124 	g_type_class_add_private (class, sizeof (EOnlineButtonPrivate));
125 
126 	object_class = G_OBJECT_CLASS (class);
127 	object_class->set_property = online_button_set_property;
128 	object_class->get_property = online_button_get_property;
129 	object_class->dispose = online_button_dispose;
130 
131 	g_object_class_install_property (
132 		object_class,
133 		PROP_ONLINE,
134 		g_param_spec_boolean (
135 			"online",
136 			"Online",
137 			"The button state is online",
138 			TRUE,
139 			G_PARAM_READWRITE |
140 			G_PARAM_CONSTRUCT));
141 }
142 
143 static void
144 e_online_button_init (EOnlineButton *button)
145 {
146 	GtkWidget *widget;
147 
148 	button->priv = E_ONLINE_BUTTON_GET_PRIVATE (button);
149 
150 	gtk_widget_set_can_focus (GTK_WIDGET (button), FALSE);
151 	gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
152 
153 	widget = gtk_image_new ();
154 	gtk_container_add (GTK_CONTAINER (button), widget);
155 	button->priv->image = g_object_ref (widget);
156 	gtk_widget_show (widget);
157 
158 	g_signal_connect (
159 		button, "notify::online",
160 		G_CALLBACK (online_button_update_tooltip), NULL);
161 
162 	g_signal_connect (
163 		button, "notify::sensitive",
164 		G_CALLBACK (online_button_update_tooltip), NULL);
165 }
166 
167 GtkWidget *
168 e_online_button_new (void)
169 {
170 	return g_object_new (E_TYPE_ONLINE_BUTTON, NULL);
171 }
172 
173 gboolean
174 e_online_button_get_online (EOnlineButton *button)
175 {
176 	g_return_val_if_fail (E_IS_ONLINE_BUTTON (button), FALSE);
177 
178 	return button->priv->online;
179 }
180 
181 void
182 e_online_button_set_online (EOnlineButton *button,
183                             gboolean online)
184 {
185 	GtkImage *image;
186 	GtkIconInfo *icon_info;
187 	GtkIconTheme *icon_theme;
188 	const gchar *filename;
189 	const gchar *icon_name;
190 
191 	g_return_if_fail (E_IS_ONLINE_BUTTON (button));
192 
193 	if (button->priv->online == online)
194 		return;
195 
196 	button->priv->online = online;
197 
198 	image = GTK_IMAGE (button->priv->image);
199 	icon_name = online ? "online" : "offline";
200 	icon_theme = gtk_icon_theme_get_default ();
201 
202 	/* Prevent GTK+ from scaling these rectangular icons. */
203 	icon_info = gtk_icon_theme_lookup_icon (
204 		icon_theme, icon_name, GTK_ICON_SIZE_BUTTON, 0);
205 	filename = gtk_icon_info_get_filename (icon_info);
206 	gtk_image_set_from_file (image, filename);
207 	gtk_icon_info_free (icon_info);
208 
209 	g_object_notify (G_OBJECT (button), "online");
210 }