Mercurial > projects > xinhibit-applet
comparison xia-icon.c @ 0:9a16bf50daba
Initial revision
author | Guido Berhoerster <guido+xinhibit-applet@berhoerster.name> |
---|---|
date | Sat, 27 Apr 2013 00:33:11 +0200 |
parents | |
children | 14f31e92372f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9a16bf50daba |
---|---|
1 /* | |
2 * Copyright (C) 2013 Guido Berhoerster <guido+xinhibit-applet@berhoerster.name> | |
3 * | |
4 * Permission is hereby granted, free of charge, to any person obtaining | |
5 * a copy of this software and associated documentation files (the | |
6 * "Software"), to deal in the Software without restriction, including | |
7 * without limitation the rights to use, copy, modify, merge, publish, | |
8 * distribute, sublicense, and/or sell copies of the Software, and to | |
9 * permit persons to whom the Software is furnished to do so, subject to | |
10 * the following conditions: | |
11 * | |
12 * The above copyright notice and this permission notice shall be included | |
13 * in all copies or substantial portions of the Software. | |
14 * | |
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 */ | |
23 | |
24 #include <glib/gi18n.h> | |
25 #include "xia-debug.h" | |
26 #include "xia-inhibitor.h" | |
27 #include "xia-icon.h" | |
28 | |
29 G_DEFINE_TYPE(XiaIcon, xia_icon, G_TYPE_OBJECT) | |
30 | |
31 #define XIA_ICON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ | |
32 XIA_TYPE_ICON, XiaIconPrivate)) | |
33 | |
34 struct _XiaIconPrivate | |
35 { | |
36 XiaInhibitor *inhibitor; | |
37 | |
38 GtkStatusIcon *status_icon; | |
39 GtkWidget *status_icon_popup_menu; | |
40 GtkWidget *check_menu_item_inhibited; | |
41 }; | |
42 | |
43 static void icon_update(XiaIcon *self); | |
44 static GtkWidget* icon_popup_menu_create(XiaIcon *self); | |
45 static void icon_popup_menu_popup(GtkStatusIcon *status_icon, guint button, | |
46 guint activate_time, gpointer user_data); | |
47 static void icon_activate(GtkStatusIcon *status_icon, gpointer user_data); | |
48 | |
49 static void | |
50 xia_icon_finalize(GObject *gobject) | |
51 { | |
52 XiaIcon *self = XIA_ICON(gobject); | |
53 | |
54 gtk_widget_destroy(self->priv->status_icon_popup_menu); | |
55 g_object_unref(self->priv->status_icon_popup_menu); | |
56 g_object_unref(self->priv->status_icon); | |
57 g_object_unref(self->priv->inhibitor); | |
58 | |
59 G_OBJECT_CLASS(xia_icon_parent_class)->finalize(gobject); | |
60 } | |
61 | |
62 static void | |
63 xia_icon_class_init(XiaIconClass *klass) | |
64 { | |
65 GObjectClass *gobject_class = G_OBJECT_CLASS(klass); | |
66 | |
67 gobject_class->finalize = xia_icon_finalize; | |
68 | |
69 g_type_class_add_private(klass, sizeof (XiaIconPrivate)); | |
70 } | |
71 | |
72 static void | |
73 xia_icon_init(XiaIcon *self) | |
74 { | |
75 self->priv = XIA_ICON_GET_PRIVATE(self); | |
76 | |
77 self->priv->inhibitor = xia_inhibitor_new(); | |
78 | |
79 self->priv->status_icon = gtk_status_icon_new(); | |
80 gtk_status_icon_set_title(self->priv->status_icon, | |
81 _("Inhibit automatic power management")); | |
82 | |
83 self->priv->status_icon_popup_menu = icon_popup_menu_create(self); | |
84 g_object_ref(self->priv->status_icon_popup_menu); | |
85 g_object_ref_sink(GTK_OBJECT(self->priv->status_icon_popup_menu)); | |
86 | |
87 g_signal_connect(G_OBJECT(self->priv->status_icon), "activate", | |
88 G_CALLBACK(icon_activate), self); | |
89 g_signal_connect(G_OBJECT(self->priv->status_icon), "popup-menu", | |
90 G_CALLBACK(icon_popup_menu_popup), self); | |
91 | |
92 icon_update(self); | |
93 } | |
94 | |
95 static void | |
96 about_dialog_show(GtkMenuItem *item, gpointer user_data) | |
97 { | |
98 const gchar *copyright = "Copyright \302\251 2013 Guido Berhoerster\n"; | |
99 const gchar *comments = _("Allows users to inhibit automatic power " | |
100 "management\n\nTo Ai\n"); | |
101 const gchar *logo_icon_name = "xinhibit-applet"; | |
102 const gchar *authors[2] = { | |
103 "Guido Berhoerster <guido+xinhibit-applet@berhoerster.name>", | |
104 NULL | |
105 }; | |
106 const gchar *license = | |
107 "Copyright \302\251 2013 Guido Berhoerster\n\n" | |
108 "Permission is hereby granted, free of charge, to any person " | |
109 "obtaining a copy of this software and associated documentation " | |
110 "files (the \"Software\"), to deal in the Software without " | |
111 "restriction, including without limitation the rights to use, " | |
112 "copy, modify, merge, publish, distribute, sublicense, and/or sell " | |
113 "copies of the Software, and to permit persons to whom the " | |
114 "Software is furnished to do so, subject to the following " | |
115 "conditions:\n\n" | |
116 "The above copyright notice and this permission notice shall be " | |
117 "included in all copies or substantial portions of the Software." | |
118 "\n\n" | |
119 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " | |
120 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES " | |
121 "OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND " | |
122 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT " | |
123 "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, " | |
124 "WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING " | |
125 "FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR " | |
126 "OTHER DEALINGS IN THE SOFTWARE."; | |
127 const gchar *translators = _("translators"); | |
128 | |
129 if (strcmp(translators, "translators") == 0) { | |
130 translators = NULL; | |
131 } | |
132 | |
133 gtk_show_about_dialog(NULL, "version", VERSION, "copyright", copyright, | |
134 "logo-icon-name", logo_icon_name, "comments", comments, "authors", | |
135 authors, "translator-credits", translators, "license", license, | |
136 "wrap-license", TRUE, NULL); | |
137 } | |
138 | |
139 static void | |
140 icon_update(XiaIcon *self) | |
141 { | |
142 gboolean inhibited; | |
143 | |
144 inhibited = xia_inhibitor_get_inhibited(self->priv->inhibitor); | |
145 | |
146 g_debug("Updating icon, inhibit: %s", (inhibited) ? "on" : "off"); | |
147 | |
148 gtk_status_icon_set_tooltip_text(self->priv->status_icon, (inhibited) ? | |
149 _("Enable Automatic Power Management") : | |
150 _("Inhibit Automatic Power Management")); | |
151 gtk_status_icon_set_from_icon_name(self->priv->status_icon, | |
152 (inhibited) ? "xinhibit-applet-inhibited" : | |
153 "xinhibit-applet-uninhibited"); | |
154 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM( | |
155 self->priv->check_menu_item_inhibited), inhibited); | |
156 } | |
157 | |
158 static void | |
159 icon_activate(GtkStatusIcon *status_icon, gpointer user_data) | |
160 { | |
161 XiaIcon *self = XIA_ICON(user_data); | |
162 gboolean inhibited = xia_inhibitor_get_inhibited(self->priv->inhibitor); | |
163 | |
164 g_debug("Icon activated, inhibit: %s", (!inhibited) ? "on" : "off"); | |
165 | |
166 xia_inhibitor_set_inhibited(self->priv->inhibitor, !inhibited); | |
167 icon_update(self); | |
168 } | |
169 | |
170 static void | |
171 check_menu_item_inhibit_activate(GtkCheckMenuItem *menu_item, | |
172 gpointer user_data) | |
173 { | |
174 XiaIcon *self = XIA_ICON(user_data); | |
175 gboolean inhibited = gtk_check_menu_item_get_active(menu_item); | |
176 | |
177 g_debug("Inhibit menu item activated, inhibit: %s", (inhibited) ? "on" : | |
178 "off"); | |
179 | |
180 if (xia_inhibitor_get_inhibited(self->priv->inhibitor) != inhibited) { | |
181 xia_inhibitor_set_inhibited(self->priv->inhibitor, inhibited); | |
182 icon_update(self); | |
183 } | |
184 } | |
185 | |
186 static GtkWidget* | |
187 icon_popup_menu_create(XiaIcon *self) | |
188 { | |
189 GtkWidget *popup_menu = gtk_menu_new(); | |
190 GtkWidget *item; | |
191 GtkWidget *image; | |
192 | |
193 self->priv->check_menu_item_inhibited = \ | |
194 gtk_check_menu_item_new_with_mnemonic(_("_Inhibit Automatic Power " | |
195 "Management")); | |
196 gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu), | |
197 self->priv->check_menu_item_inhibited); | |
198 g_signal_connect(G_OBJECT(self->priv->check_menu_item_inhibited), | |
199 "activate", G_CALLBACK(check_menu_item_inhibit_activate), self); | |
200 | |
201 item = gtk_image_menu_item_new_with_mnemonic(_("_About")); | |
202 image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT, | |
203 GTK_ICON_SIZE_MENU); | |
204 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image); | |
205 gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu), item); | |
206 g_signal_connect(G_OBJECT(item), "activate", | |
207 G_CALLBACK(about_dialog_show), self); | |
208 | |
209 gtk_widget_show_all(GTK_WIDGET(popup_menu)); | |
210 | |
211 return (popup_menu); | |
212 } | |
213 | |
214 static void | |
215 icon_popup_menu_popup(GtkStatusIcon *status_icon, guint button, | |
216 guint activate_time, gpointer user_data) | |
217 { | |
218 XiaIcon *self = XIA_ICON(user_data); | |
219 | |
220 gtk_menu_popup(GTK_MENU(self->priv->status_icon_popup_menu), NULL, NULL, | |
221 NULL, NULL, button, activate_time); | |
222 } | |
223 | |
224 XiaIcon * | |
225 xia_icon_new(void) | |
226 { | |
227 return (g_object_new(XIA_TYPE_ICON, NULL)); | |
228 } |