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
285
286
287
288
289
290
291
292
293
|
/*
* Pidgin - Internet Messenger
* Copyright (C) Pidgin Developers <devel@pidgin.im>
*
* Pidgin is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#include "pidgin/pidginpresenceicon.h"
#include "pidgin/pidginiconname.h"
struct _PidginPresenceIcon {
GtkBox parent;
GtkWidget *icon;
PurplePresence *presence;
gchar *fallback;
};
enum {
PROP_0,
PROP_PRESENCE,
PROP_FALLBACK,
PROP_ICON_SIZE,
N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES] = { NULL, };
G_DEFINE_TYPE(PidginPresenceIcon, pidgin_presence_icon, GTK_TYPE_BOX)
/******************************************************************************
* Implementation
*****************************************************************************/
static void
pidgin_presence_icon_update(PidginPresenceIcon *icon) {
const gchar *icon_name = NULL;
icon_name = pidgin_icon_name_from_presence(icon->presence, icon->fallback);
gtk_image_set_from_icon_name(GTK_IMAGE(icon->icon), icon_name);
}
static void
pidgin_presence_icon_active_status_changed_cb(G_GNUC_UNUSED GObject *obj,
G_GNUC_UNUSED GParamSpec *pspec,
gpointer data)
{
pidgin_presence_icon_update(PIDGIN_PRESENCE_ICON(data));
}
/******************************************************************************
* GObject Implementation
*****************************************************************************/
static void
pidgin_presence_icon_get_property(GObject *obj, guint param_id, GValue *value,
GParamSpec *pspec)
{
PidginPresenceIcon *icon = PIDGIN_PRESENCE_ICON(obj);
switch(param_id) {
case PROP_PRESENCE:
g_value_set_object(value, pidgin_presence_icon_get_presence(icon));
break;
case PROP_FALLBACK:
g_value_set_string(value, pidgin_presence_icon_get_fallback(icon));
break;
case PROP_ICON_SIZE:
g_value_set_enum(value, pidgin_presence_icon_get_icon_size(icon));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
pidgin_presence_icon_set_property(GObject *obj, guint param_id,
const GValue *value, GParamSpec *pspec)
{
PidginPresenceIcon *icon = PIDGIN_PRESENCE_ICON(obj);
switch(param_id) {
case PROP_PRESENCE:
pidgin_presence_icon_set_presence(icon, g_value_get_object(value));
break;
case PROP_FALLBACK:
pidgin_presence_icon_set_fallback(icon, g_value_get_string(value));
break;
case PROP_ICON_SIZE:
pidgin_presence_icon_set_icon_size(icon, g_value_get_enum(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
pidgin_presence_icon_finalize(GObject *obj) {
PidginPresenceIcon *icon = PIDGIN_PRESENCE_ICON(obj);
g_clear_object(&icon->presence);
g_clear_pointer(&icon->fallback, g_free);
G_OBJECT_CLASS(pidgin_presence_icon_parent_class)->finalize(obj);
}
static void
pidgin_presence_icon_init(PidginPresenceIcon *presenceicon) {
gtk_widget_init_template(GTK_WIDGET(presenceicon));
}
static void
pidgin_presence_icon_class_init(PidginPresenceIconClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
obj_class->finalize = pidgin_presence_icon_finalize;
obj_class->get_property = pidgin_presence_icon_get_property;
obj_class->set_property = pidgin_presence_icon_set_property;
/**
* PidginPresenceIcon:presence:
*
* The presence that this icon will be representing.
*/
properties[PROP_PRESENCE] = g_param_spec_object(
"presence", "presence",
"The presence that this icon is representing",
PURPLE_TYPE_PRESENCE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
/**
* PidginPresenceIcon:fallback:
*
* The name of the icon to use as a fallback when no presence is set.
*/
properties[PROP_FALLBACK] = g_param_spec_string(
"fallback", "fallback",
"The name of the icon to use as a fallback",
"user-invisible",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
/**
* PidginPresenceIcon:icon-size:
*
* The size of the icon that should be used.
*/
properties[PROP_ICON_SIZE] = g_param_spec_enum(
"icon-size", "icon-size",
"The GtkIconSize to use",
GTK_TYPE_ICON_SIZE,
GTK_ICON_SIZE_NORMAL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
gtk_widget_class_set_template_from_resource(
widget_class,
"/im/pidgin/Pidgin3/presenceicon.ui"
);
gtk_widget_class_bind_template_child(widget_class, PidginPresenceIcon,
icon);
}
/******************************************************************************
* Public API
*****************************************************************************/
GtkWidget *
pidgin_presence_icon_new(PurplePresence *presence, const gchar *fallback,
GtkIconSize icon_size)
{
return g_object_new(
PIDGIN_TYPE_PRESENCE_ICON,
"presence", presence,
"fallback", fallback,
"icon_size", icon_size,
NULL);
}
PurplePresence *
pidgin_presence_icon_get_presence(PidginPresenceIcon *icon) {
g_return_val_if_fail(PIDGIN_IS_PRESENCE_ICON(icon), NULL);
return icon->presence;
}
void
pidgin_presence_icon_set_presence(PidginPresenceIcon *icon,
PurplePresence *presence)
{
PurplePresence *old = NULL;
g_return_if_fail(PIDGIN_IS_PRESENCE_ICON(icon));
/* We only want to disconnect signal handlers if the presence was changed,
* but to do that, we need to keep a reference to the old presence.
*/
if(PURPLE_IS_PRESENCE(icon->presence)) {
old = g_object_ref(icon->presence);
}
if(g_set_object(&icon->presence, presence)) {
if(G_IS_OBJECT(old)) {
/* If we previously had a presence, disconnect our signal handlers.
*/
g_signal_handlers_disconnect_by_data(old, icon);
}
if(PURPLE_IS_PRESENCE(icon->presence)) {
g_signal_connect(icon->presence, "notify::active-status",
G_CALLBACK(pidgin_presence_icon_active_status_changed_cb),
icon);
}
g_object_freeze_notify(G_OBJECT(icon));
pidgin_presence_icon_update(icon);
g_object_notify_by_pspec(G_OBJECT(icon), properties[PROP_PRESENCE]);
g_object_thaw_notify(G_OBJECT(icon));
}
g_clear_object(&old);
}
const gchar *
pidgin_presence_icon_get_fallback(PidginPresenceIcon *icon) {
g_return_val_if_fail(PIDGIN_IS_PRESENCE_ICON(icon), NULL);
return icon->fallback;
}
void
pidgin_presence_icon_set_fallback(PidginPresenceIcon *icon,
const gchar *fallback)
{
g_return_if_fail(PIDGIN_IS_PRESENCE_ICON(icon));
g_return_if_fail(fallback != NULL);
g_free(icon->fallback);
icon->fallback = g_strdup(fallback);
g_object_freeze_notify(G_OBJECT(icon));
pidgin_presence_icon_update(icon);
g_object_notify_by_pspec(G_OBJECT(icon), properties[PROP_FALLBACK]);
g_object_thaw_notify(G_OBJECT(icon));
}
GtkIconSize
pidgin_presence_icon_get_icon_size(PidginPresenceIcon *icon) {
g_return_val_if_fail(PIDGIN_IS_PRESENCE_ICON(icon), GTK_ICON_SIZE_INHERIT);
return gtk_image_get_icon_size(GTK_IMAGE(icon->icon));
}
void
pidgin_presence_icon_set_icon_size(PidginPresenceIcon *icon,
GtkIconSize icon_size)
{
g_return_if_fail(PIDGIN_IS_PRESENCE_ICON(icon));
gtk_image_set_icon_size(GTK_IMAGE(icon->icon), icon_size);
g_object_freeze_notify(G_OBJECT(icon));
pidgin_presence_icon_update(icon);
g_object_notify_by_pspec(G_OBJECT(icon), properties[PROP_ICON_SIZE]);
g_object_thaw_notify(G_OBJECT(icon));
}
|