File: hideconv.c

package info (click to toggle)
purple-plugin-pack 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,176 kB
  • ctags: 3,409
  • sloc: ansic: 38,210; sh: 11,016; makefile: 1,555; python: 400
file content (291 lines) | stat: -rw-r--r-- 9,032 bytes parent folder | download | duplicates (4)
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
/*
 * Hide Conversations - You can hide conversations without having to close them.
 * Copyright (C) 2007-2008 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02111-1301, USA.
 */

/* If you can't figure out what this line is for, DON'T TOUCH IT. */
#include "../common/pp_internal.h"

#define PLUGIN_ID			"gtk-plugin_pack-hideconv"
#define PLUGIN_STATIC_NAME	"hideconv"
#define PLUGIN_AUTHOR		"Sadrul H Chowdhury <sadrul@users.sourceforge.net>"

/* System headers */
#include <gdk/gdk.h>
#include <gtk/gtk.h>

/* Purple headers */
#include <gtkconv.h>
#include <gtkconvwin.h>
#include <gtkplugin.h>
#include <gtkutils.h>

#define MENUSET "hideconv::menuset"

static PidginWindow *window = NULL;
static void (*orig_conv_present)(PurpleConversation *conv);

static void conv_created(PurpleConversation *conv, gpointer null);

static void
create_hidden_convwin()
{
	/* This is a 'wee bit' hacky. Create two conv windows, remove the second
	 * one from the list, and then destroy the first one. This is because we
	 * want to hide this entire conversation window from pidgin itself.
	 */
	GList *null;
	PidginWindow *tmp = pidgin_conv_window_new();
	window = pidgin_conv_window_new();
	null = pidgin_conv_windows_get_list();
	null = g_list_remove(null, window);
	pidgin_conv_window_hide(window);
	pidgin_conv_window_destroy(tmp);
}

static void
gtkconv_redisplaying(PidginConversation *gtkconv)
{
	conv_created(gtkconv->active_conv, NULL);
	g_signal_handlers_disconnect_by_func(G_OBJECT(gtkconv->imhtml),
			G_CALLBACK(gtkconv_redisplaying), gtkconv);
}

static void
hide_gtkconv(PidginConversation *gtkconv)
{
	pidgin_conv_window_add_gtkconv(window, gtkconv);
	g_signal_connect_swapped(G_OBJECT(gtkconv->imhtml), "visibility_notify_event",
			G_CALLBACK(gtkconv_redisplaying), gtkconv);
}

static void
hide_conv_cb(GtkWidget *wid, PidginWindow *win)
{
	PidginConversation *gtkconv = pidgin_conv_window_get_active_gtkconv(win);
	pidgin_conv_window_remove_gtkconv(win, gtkconv);
	hide_gtkconv(gtkconv);
}

static void
show_convs_cb(PurplePluginAction *dontcare)
{
	GList *list = g_list_copy(pidgin_conv_window_get_gtkconvs(window)), *iter;

	for (iter = list; iter; iter = iter->next) {
		PidginConversation *gtkconv = iter->data;
		pidgin_conv_window_remove_gtkconv(window, gtkconv);
		pidgin_conv_placement_place(gtkconv);
		purple_conversation_present(gtkconv->active_conv);
		conv_created(gtkconv->active_conv, NULL);
	}
	g_list_free(list);

	create_hidden_convwin();
}

static void
attach_menu_to_window(PidginWindow *win)
{
	GtkWidget *widget, *item;
	if (g_object_get_data(G_OBJECT(win->window), MENUSET))
		return;
	g_object_set_data(G_OBJECT(win->window), MENUSET, GINT_TO_POINTER(TRUE));

	widget = gtk_item_factory_get_widget(win->menu.item_factory, N_("/Options"));

	/* We cannot use pidgin_separator, unfortunately. */
	item = gtk_separator_menu_item_new();
	gtk_widget_show(item);
	gtk_menu_shell_append(GTK_MENU_SHELL(widget), item);
	g_object_set_data(G_OBJECT(item), MENUSET, GINT_TO_POINTER(TRUE));

	item = gtk_menu_item_new_with_mnemonic(_("_Hide Conversation"));
	g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(hide_conv_cb), win);
	gtk_widget_show(item);
	gtk_menu_shell_append(GTK_MENU_SHELL(widget), item);
	g_object_set_data(G_OBJECT(item), MENUSET, GINT_TO_POINTER(TRUE));

	item = gtk_menu_item_new_with_mnemonic(_("Show Hidden Conversations"));
	g_signal_connect_swapped(G_OBJECT(item), "activate", G_CALLBACK(show_convs_cb), NULL);
	gtk_widget_show(item);
	gtk_menu_shell_append(GTK_MENU_SHELL(widget), item);
	g_object_set_data(G_OBJECT(item), MENUSET, GINT_TO_POINTER(TRUE));
}

static void
detach_menu_from_window(PidginWindow *win)
{
	GtkWidget *widget;
	GList *children;
	
	widget = gtk_item_factory_get_widget(win->menu.item_factory, N_("/Options"));
	children = gtk_container_get_children(GTK_CONTAINER(widget));
	while (children) {
		GtkWidget *item = children->data;
		children = children->next;
		if (g_object_get_data(G_OBJECT(item), MENUSET))
			gtk_widget_destroy(item);
	}
	g_object_set_data(G_OBJECT(win->window), MENUSET, GINT_TO_POINTER(FALSE));
}

static gboolean
conv_created_to(PurpleConversation *conv)
{
	PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);

	if (!gtkconv || !gtkconv->win || !gtkconv->win->window)
		return TRUE;
	if (!GTK_WIDGET_VISIBLE(gtkconv->win->window))
		return TRUE;

	attach_menu_to_window(gtkconv->win);
	return FALSE;
}

static void
conv_created(PurpleConversation *conv, gpointer null)
{
	g_timeout_add(1000, (GSourceFunc)conv_created_to, conv);
}

static void
twisted_present(PurpleConversation *conv)
{
	PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
	if (gtkconv && gtkconv->win == window) {
		gboolean last = (g_list_length(window->gtkconvs) == 1);
		pidgin_conv_window_remove_gtkconv(window, gtkconv);
		pidgin_conv_placement_place(gtkconv);
		if (last)
			create_hidden_convwin();
	}
	orig_conv_present(conv);
	conv_created(conv, NULL);
}

static void
hide_all_conv(PurplePluginAction *dontcare)
{
	GList *iter = pidgin_conv_windows_get_list();
	while (iter) {
		GList *it = pidgin_conv_window_get_gtkconvs(iter->data);
		iter = iter->next;
		while (it) {
			PidginConversation *gtkconv = it->data;
			it = it->next;
			pidgin_conv_window_remove_gtkconv(gtkconv->win, gtkconv);
			hide_gtkconv(gtkconv);
		}
	}
}

static gboolean
plugin_load(PurplePlugin *plugin)
{
	PurpleConversationUiOps *ops = pidgin_conversations_get_conv_ui_ops();
	orig_conv_present = ops->present;
	ops->present = twisted_present;

	create_hidden_convwin();

	purple_signal_connect(purple_conversations_get_handle(), "conversation-created",
			plugin, PURPLE_CALLBACK(conv_created), NULL);

	g_list_foreach(pidgin_conv_windows_get_list(), (GFunc)attach_menu_to_window, NULL);

	return TRUE;
}

static gboolean
plugin_unload(PurplePlugin *plugin)
{
	PurpleConversationUiOps *ops = pidgin_conversations_get_conv_ui_ops();
	ops->present = orig_conv_present;

	show_convs_cb(NULL);
	pidgin_conv_window_destroy(window);
	window = NULL;

	g_list_foreach(pidgin_conv_windows_get_list(), (GFunc)detach_menu_from_window, NULL);

	return TRUE;
}

static GList *
actions(PurplePlugin *plugin, gpointer context)
{
	PurplePluginAction *act;
	GList *list = NULL;
	
	act = purple_plugin_action_new(_("Show All Hidden Conversations"), show_convs_cb);
	list = g_list_append(list, act);

	act = purple_plugin_action_new(_("Hide All Conversations"), hide_all_conv);
	list = g_list_append(list, act);

	return list;
}

static PurplePluginInfo info =
{
	PURPLE_PLUGIN_MAGIC,       /* Magic              */
	PURPLE_MAJOR_VERSION,      /* Purple Major Version */
	PURPLE_MINOR_VERSION,      /* Purple Minor Version */
	PURPLE_PLUGIN_STANDARD,    /* plugin type        */
	PIDGIN_PLUGIN_TYPE,        /* ui requirement     */
	0,                         /* flags              */
	NULL,                      /* dependencies       */
	PURPLE_PRIORITY_DEFAULT,   /* priority           */

	PLUGIN_ID,                 /* plugin id          */
	NULL,                      /* name               */
	PP_VERSION,                /* version            */
	NULL,                      /* summary            */
	NULL,                      /* description        */
	PLUGIN_AUTHOR,             /* author             */
	PP_WEBSITE,                /* website            */

	plugin_load,               /* load               */
	plugin_unload,             /* unload             */
	NULL,                      /* destroy            */

	NULL,                      /* ui_info            */
	NULL,                      /* extra_info         */
	NULL,                      /* prefs_info         */
	actions,                  /* actions            */
	NULL,                      /* reserved 1         */
	NULL,                      /* reserved 2         */
	NULL,                      /* reserved 3         */
	NULL                       /* reserved 4         */
};

static void
init_plugin(PurplePlugin *plugin) {
#ifdef ENABLE_NLS
	bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#endif /* ENABLE_NLS */

	info.name = _("Hide Conversation");
	info.summary = _("Hide conversations without closing them.");
	info.description = _("Hide conversations without closing them.");
}

PURPLE_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)