-
Notifications
You must be signed in to change notification settings - Fork 3
/
xep136_plugin.c
347 lines (261 loc) · 8.35 KB
/
xep136_plugin.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* filename: xep136_plugin.c
*/
#define PURPLE_PLUGINS
#define PREF_BASE "/plugins/gtk/xep136"
#define PREF_TOOL "/plugins/gtk/xep136/toolbar"
#define PREF_EDIT "/plugins/gtk/xep136/editbox"
#include "xep136_plugin.h"
PurplePlugin *xep136 = NULL; /* plugin id pointer */
GList *list = NULL; /* list of pointers to WindowStruct items */
/* xmlns for ejabberd and prosody to xml messages */
char *xmlns_ejabberd = "http://www.xmpp.org/extensions/xep-0136.html#ns";
char *xmlns_prosody = "urn:xmpp:archive";
/*------------------------------------------------------------
* plugin load and uload related functions
*------------------------------------------------------------*/
/* check if conversation is jabber */
static gboolean
if_jabber(PidginConversation *gtkconv)
{
PurpleConversation *conv = gtkconv->active_conv;
PurpleAccount *acc = conv->account;
char *jabber_id = "prpl-jabber";
if(!acc) {
purple_debug_misc(PLUGIN_ID, "prpl-jabber check:: ERROR acc\n");
return FALSE;
}
// test jabber conversation
if (strcmp(jabber_id, purple_account_get_protocol_id(acc)) == 0) {
return TRUE;
} else {
return FALSE;
}
}
static void
destroy_windows(WindowStruct *curr)
{
if (curr->coll)
g_list_free(curr->coll);
g_free(curr->id);
gtk_widget_destroy(curr->window);
}
static void
destroy_history_window(WindowStruct *curr, PidginConversation *gtkconv)
{
if (curr->gtkconv == gtkconv) {
destroy_windows(curr);
}
}
static void
conv_deleted(PurpleConversation *conv, gpointer null)
{
PidginConversation *gtkconv = NULL;
if (!conv)
purple_debug_error(PLUGIN_ID, "ERROR: 'conv_deleted': !conv\n");
gtkconv = PIDGIN_CONVERSATION(conv);
if (!gtkconv)
purple_debug_error(PLUGIN_ID, "ERROR: 'conv_deleted': !gtkconv\n");
g_return_if_fail(gtkconv != NULL);
if (!list) {
purple_debug_misc(PLUGIN_ID, "conv_deleted :: empty list\n");
return;
}
if (!if_jabber(gtkconv))
return;
g_list_foreach(list, (GFunc) destroy_history_window, (gpointer) gtkconv);
}
static void
detach_from_gtkconv(PidginConversation *gtkconv, gpointer null)
{
GtkWidget *toolbar_box, *hbox;
if (!if_jabber(gtkconv))
return;
/* detach from toolbar */
toolbar_box = gtkconv->toolbar;
hbox = g_object_get_data(G_OBJECT(toolbar_box), "xep136_hbox");
if (hbox)
gtk_container_remove(GTK_CONTAINER(toolbar_box), hbox);
gtk_widget_queue_draw(pidgin_conv_get_window(gtkconv)->window);
/* detach from editbox */
toolbar_box = gtkconv->lower_hbox;
hbox = g_object_get_data(G_OBJECT(toolbar_box), "xep136_hbox");
if (hbox)
gtk_container_remove(GTK_CONTAINER(toolbar_box), hbox);
gtk_widget_queue_draw(pidgin_conv_get_window(gtkconv)->window);
}
static void
attach_to_gtkconv(PidginConversation *gtkconv, gpointer null)
{
GtkWidget *hbox, *button;
//GtkWidget *menu_item;
if (!if_jabber(gtkconv))
return;
/* attach to toolbar */
if (purple_prefs_get_bool(PREF_TOOL)) {
button = gtk_button_new_with_label("History");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(history_button_clicked), (gpointer) gtkconv);
hbox = gtk_hbox_new(FALSE, 5);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
g_object_set_data(G_OBJECT(gtkconv->toolbar), "xep136_hbox", hbox);
gtk_box_pack_end(GTK_BOX(gtkconv->toolbar), hbox, FALSE, FALSE, 0);
gtk_widget_show_all(hbox);
gtk_widget_queue_draw(pidgin_conv_get_window(gtkconv)->window);
}
/* attach to editbox */
if (purple_prefs_get_bool(PREF_EDIT)) {
button = gtk_button_new_with_label("History");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(history_button_clicked), (gpointer) gtkconv);
hbox = gtk_hbox_new(FALSE, 5);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
g_object_set_data(G_OBJECT(gtkconv->lower_hbox), "xep136_hbox", hbox);
gtk_box_pack_end(GTK_BOX(gtkconv->lower_hbox), hbox, FALSE, FALSE, 0);
gtk_widget_show_all(hbox);
gtk_widget_queue_draw(pidgin_conv_get_window(gtkconv)->window);
}
}
static void
detach_from_pidgin_window(PidginWindow *win, gpointer null)
{
g_list_foreach(pidgin_conv_window_get_gtkconvs(win), (GFunc) detach_from_gtkconv, NULL);
}
static void
attach_to_pidgin_window(PidginWindow *win, gpointer null)
{
g_list_foreach(pidgin_conv_window_get_gtkconvs(win), (GFunc) attach_to_gtkconv, NULL);
}
static void
detach_from_all_windows()
{
g_list_foreach(pidgin_conv_windows_get_list(), (GFunc) detach_from_pidgin_window, NULL);
}
static void
attach_to_all_windows()
{
g_list_foreach(pidgin_conv_windows_get_list(), (GFunc) attach_to_pidgin_window, NULL);
}
static void
conv_created(PurpleConversation *conv, gpointer null)
{
PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
g_return_if_fail(gtkconv != NULL);
attach_to_gtkconv(gtkconv, NULL);
}
/*------------------------------------------------------------
* plugin load, unload, PurplePluginInfo, init_plugin
*------------------------------------------------------------*/
static gboolean
plugin_load(PurplePlugin *plugin)
{
PurplePlugin *jabber;
xep136 = plugin;
attach_to_all_windows();
purple_signal_connect(purple_conversations_get_handle(), "conversation-created", plugin,
PURPLE_CALLBACK(conv_created), NULL);
purple_signal_connect(purple_conversations_get_handle(), "deleting-conversation", plugin,
PURPLE_CALLBACK(conv_deleted), NULL);
jabber = purple_find_prpl("prpl-jabber");
if (!jabber)
return FALSE;
purple_signal_connect(jabber, "jabber-receiving-xmlnode", xep136,
PURPLE_CALLBACK(xmlnode_received), NULL);
return TRUE;
}
static gboolean
plugin_unload(PurplePlugin *plugin)
{
g_list_foreach(list, (GFunc) destroy_windows, NULL);
g_list_free(list);
list = NULL;
detach_from_all_windows();
return TRUE;
}
static void
toolbar_changed(GtkWidget *widget, gpointer data)
{
gboolean on = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
purple_prefs_set_bool(PREF_TOOL, on);
}
static void
editbox_changed(GtkWidget *widget, gpointer data)
{
gboolean on = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
purple_prefs_set_bool(PREF_EDIT, on);
}
static GtkWidget *
get_config_frame(PurplePlugin *plugin)
{
GtkWidget *ret, *frame, *vbox;
GtkWidget *toolbar, *editbox;
ret = gtk_vbox_new(FALSE, 18);
gtk_container_set_border_width (GTK_CONTAINER (ret), 12);
//frame = pidgin_make_frame(ret, _("History button position"));
frame = pidgin_make_frame(ret, "History button position");
vbox = gtk_vbox_new(FALSE, 5);
gtk_container_add(GTK_CONTAINER(frame), vbox);
/* history button position: toolbar*/
toolbar = gtk_check_button_new_with_mnemonic("_Toolbar");
gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar),
purple_prefs_get_bool(PREF_TOOL));
g_signal_connect(G_OBJECT(toolbar), "toggled",
G_CALLBACK(toolbar_changed), NULL);
/* history button position: editbox */
editbox = gtk_check_button_new_with_mnemonic("_Editbox");
gtk_box_pack_start(GTK_BOX(vbox), editbox, FALSE, FALSE, 0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(editbox),
purple_prefs_get_bool(PREF_EDIT));
g_signal_connect(G_OBJECT(editbox), "toggled",
G_CALLBACK(editbox_changed), NULL);
gtk_widget_show_all(ret);
return ret;
}
static PidginPluginUiInfo ui_info =
{
get_config_frame,
0, /* page_num (Reserved) */
/* padding */
NULL,
NULL,
NULL,
NULL
};
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
PIDGIN_PLUGIN_TYPE,
0,
NULL,
PURPLE_PRIORITY_DEFAULT,
PLUGIN_ID,
"XEP-0136 plugin",
"0.7",
"XEP-0136 plugin",
"Server Message Archiving",
"Daniel Kraic <danielkraic@gmail.com>",
"https://github.com/danielkraic/Pidgin-XEP-0136-plugin",
plugin_load, /* load */
plugin_unload, /* unload */
NULL, /* destroy */
&ui_info, /* ui_info */
NULL, /* extra_info */
NULL, /* prefs_into */
NULL, /* plugin_actions */
/* padding */
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin(PurplePlugin *plugin)
{
purple_prefs_add_none(PREF_BASE);
purple_prefs_add_bool(PREF_TOOL, TRUE);
purple_prefs_add_bool(PREF_EDIT, FALSE);
}
PURPLE_INIT_PLUGIN(xep136_plugin, init_plugin, info)