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
|
/*
* Copyright 2012 Canonical Ltd.
*
* Authors: Lars Uebernickel <lars.uebernickel@canonical.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "indicator-printers.h"
#include "indicator-menu-item.h"
#include "dbus-names.h"
#include <gtk/gtk.h>
#include <libindicator/indicator.h>
#include <libindicator/indicator-image-helper.h>
#include <libdbusmenu-gtk3/menu.h>
#include <libdbusmenu-gtk3/menuitem.h>
INDICATOR_SET_VERSION
INDICATOR_SET_TYPE(INDICATOR_PRINTERS_TYPE)
G_DEFINE_TYPE (IndicatorPrinters, indicator_printers, INDICATOR_OBJECT_TYPE)
#define INDICATOR_PRINTERS_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_PRINTERS_TYPE, IndicatorPrintersPrivate))
struct _IndicatorPrintersPrivate
{
IndicatorObjectEntry entry;
};
static void
dispose (GObject *object)
{
IndicatorPrintersPrivate *priv = INDICATOR_PRINTERS_GET_PRIVATE (object);
g_clear_object (&priv->entry.menu);
g_clear_object (&priv->entry.image);
G_OBJECT_CLASS (indicator_printers_parent_class)->dispose (object);
}
static GList *
get_entries (IndicatorObject *io)
{
IndicatorPrintersPrivate *priv = INDICATOR_PRINTERS_GET_PRIVATE (io);
return g_list_append (NULL, &priv->entry);
}
static void
indicator_printers_class_init (IndicatorPrintersClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
IndicatorObjectClass *io_class = INDICATOR_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (IndicatorPrintersPrivate));
object_class->dispose = dispose;
io_class->get_entries = get_entries;
}
static GdkPixbuf *
gdk_pixbuf_new_from_encoded_data (guchar *data,
gsize length)
{
GInputStream * input;
GError *err = NULL;
GdkPixbuf *img;
input = g_memory_input_stream_new_from_data(data, length, NULL);
if (input == NULL)
return NULL;
img = gdk_pixbuf_new_from_stream(input, NULL, &err);
if (err) {
g_warning("%s", err->message);
g_error_free(err);
}
g_object_unref(input);
return img;
}
static GdkPixbuf *
g_variant_get_image (GVariant *value)
{
const gchar *strvalue = NULL;
gsize length = 0;
guchar *icondata;
GdkPixbuf *img;
if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
strvalue = g_variant_get_string(value, NULL);
if (!strvalue || !*strvalue) {
g_warning ("%s: value does not contain a base64 encoded image",
__func__);
return NULL;
}
icondata = g_base64_decode(strvalue, &length);
img = gdk_pixbuf_new_from_encoded_data (icondata, length);
g_free(icondata);
return img;
}
static gboolean
is_string_property (const gchar *name,
const gchar *prop,
GVariant *value)
{
return !g_strcmp0 (name, prop) &&
g_variant_is_of_type (value, G_VARIANT_TYPE_STRING);
}
static gboolean
is_image_property (const gchar *name,
const gchar *prop,
GVariant *value)
{
return !g_strcmp0 (name, prop) &&
g_variant_is_of_type (value, G_VARIANT_TYPE_STRING);
}
static void
indicator_prop_change_cb (DbusmenuMenuitem *mi,
gchar *prop,
GVariant *value,
gpointer user_data)
{
IndicatorMenuItem *menuitem = user_data;
if (is_string_property (prop, "indicator-label", value))
indicator_menu_item_set_label (menuitem, g_variant_get_string (value, NULL));
else if (is_string_property (prop, "indicator-right", value))
indicator_menu_item_set_right (menuitem, g_variant_get_string (value, NULL));
else if (is_string_property (prop, "indicator-icon-name", value))
indicator_menu_item_set_icon_name (menuitem, g_variant_get_string (value, NULL));
else if (is_image_property (prop, "indicator-icon", value)) {
GdkPixbuf *pb = g_variant_get_image (value);
indicator_menu_item_set_icon (menuitem, pb);
g_object_unref (pb);
}
}
static gboolean
new_indicator_item (DbusmenuMenuitem *newitem,
DbusmenuMenuitem *parent,
DbusmenuClient *client,
gpointer user_data)
{
GtkWidget *menuitem;
const gchar *icon_name, *text, *right_text;
GVariant *icon;
gboolean is_lozenge;
icon_name = dbusmenu_menuitem_property_get (newitem, "indicator-icon-name");
icon = dbusmenu_menuitem_property_get_variant (newitem, "indicator-icon");
text = dbusmenu_menuitem_property_get (newitem, "indicator-label");
right_text = dbusmenu_menuitem_property_get (newitem, "indicator-right");
is_lozenge = dbusmenu_menuitem_property_get_bool (newitem, "indicator-right-is-lozenge");
menuitem = g_object_new (INDICATOR_TYPE_MENU_ITEM,
"icon-name", icon_name,
"label", text,
"right", right_text,
"right-is-lozenge", is_lozenge,
NULL);
if (icon) {
GdkPixbuf *pb = g_variant_get_image (icon);
indicator_menu_item_set_icon (INDICATOR_MENU_ITEM (menuitem), pb);
g_object_unref (pb);
}
gtk_widget_show_all (menuitem);
dbusmenu_gtkclient_newitem_base(DBUSMENU_GTKCLIENT(client),
newitem,
GTK_MENU_ITEM (menuitem),
parent);
g_signal_connect(G_OBJECT(newitem),
"property-changed",
G_CALLBACK(indicator_prop_change_cb),
menuitem);
return TRUE;
}
static void
indicator_printers_init (IndicatorPrinters *io)
{
IndicatorPrintersPrivate *priv = INDICATOR_PRINTERS_GET_PRIVATE (io);
DbusmenuGtkMenu *menu;
DbusmenuClient *client;
GtkImage *image;
menu = dbusmenu_gtkmenu_new(INDICATOR_PRINTERS_DBUS_NAME,
INDICATOR_PRINTERS_DBUS_OBJECT_PATH);
client = DBUSMENU_CLIENT (dbusmenu_gtkmenu_get_client (menu));
dbusmenu_client_add_type_handler(client,
"indicator-item",
new_indicator_item);
image = indicator_image_helper ("printer-symbolic");
gtk_widget_show (GTK_WIDGET (image));
priv->entry.name_hint = PACKAGE_NAME;
priv->entry.accessible_desc = "Printers";
priv->entry.menu = GTK_MENU (g_object_ref_sink (menu));
priv->entry.image = g_object_ref_sink (image);
}
IndicatorPrinters *
indicator_printers_new (void)
{
return g_object_new (INDICATOR_PRINTERS_TYPE, NULL);
}
|