1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <telepathy-glib/util.h>
27 #include "empathy-chat-view.h"
28 #include "empathy-smiley-manager.h"
29 #include "empathy-ui-utils.h"
31 static void chat_view_base_init (gpointer klass);
34 empathy_chat_view_get_type (void)
36 static GType type = 0;
39 static const GTypeInfo type_info = {
40 sizeof (EmpathyChatViewIface),
45 type = g_type_register_static (G_TYPE_INTERFACE,
49 g_type_interface_add_prerequisite (type, G_TYPE_OBJECT);
56 chat_view_base_init (gpointer klass)
58 static gboolean initialized = FALSE;
66 empathy_chat_view_scroll_down (EmpathyChatView *view)
68 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
70 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->scroll_down) {
71 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->scroll_down (view);
76 empathy_chat_view_append_message (EmpathyChatView *view,
79 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
81 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->append_message) {
82 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->append_message (view,
88 empathy_chat_view_append_event (EmpathyChatView *view,
91 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
93 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->append_event) {
94 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->append_event (view,
100 empathy_chat_view_append_button (EmpathyChatView *view,
101 const gchar *message,
105 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
107 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->append_button) {
108 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->append_button (view,
116 empathy_chat_view_scroll (EmpathyChatView *view,
117 gboolean allow_scrolling)
119 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
121 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->scroll) {
122 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->scroll (view,
128 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
132 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
134 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_selection_bounds) {
135 return EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_selection_bounds (view,
143 empathy_chat_view_clear (EmpathyChatView *view)
145 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
147 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->clear) {
148 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->clear (view);
153 empathy_chat_view_find_previous (EmpathyChatView *view,
154 const gchar *search_criteria,
157 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
159 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->find_previous) {
160 return EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->find_previous (view,
168 empathy_chat_view_find_next (EmpathyChatView *view,
169 const gchar *search_criteria,
172 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
174 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->find_next) {
175 return EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->find_next (view,
184 empathy_chat_view_find_abilities (EmpathyChatView *view,
185 const gchar *search_criteria,
186 gboolean *can_do_previous,
187 gboolean *can_do_next)
189 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
191 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->find_abilities) {
192 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->find_abilities (view,
200 empathy_chat_view_highlight (EmpathyChatView *view,
203 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
205 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->highlight) {
206 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->highlight (view, text);
211 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
213 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
215 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->copy_clipboard) {
216 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->copy_clipboard (view);
221 empathy_chat_view_get_theme (EmpathyChatView *view)
223 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
225 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_theme) {
226 return EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_theme (view);
232 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
234 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
236 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->set_theme) {
237 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->set_theme (view, theme);
242 empathy_chat_view_set_margin (EmpathyChatView *view,
245 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
247 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->set_margin) {
248 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->set_margin (view, margin);
253 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
255 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
257 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_last_timestamp) {
258 return EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_last_timestamp (view);
264 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
267 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
269 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->set_last_timestamp) {
270 EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->set_last_timestamp (view, timestamp);
275 empathy_chat_view_get_last_contact (EmpathyChatView *view)
277 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
279 if (EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_last_contact) {
280 return EMPATHY_TYPE_CHAT_VIEW_GET_IFACE (view)->get_last_contact (view);
285 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
286 * pixbuf. Returns a new ref.
289 chat_view_pad_to_size (GdkPixbuf *pixbuf,
292 gint extra_padding_right)
294 gint src_width, src_height;
296 gint x_offset, y_offset;
298 src_width = gdk_pixbuf_get_width (pixbuf);
299 src_height = gdk_pixbuf_get_height (pixbuf);
301 x_offset = (width - src_width) / 2;
302 y_offset = (height - src_height) / 2;
304 padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
306 gdk_pixbuf_get_bits_per_sample (pixbuf),
307 width + extra_padding_right,
310 gdk_pixbuf_fill (padded, 0);
312 gdk_pixbuf_copy_area (pixbuf,
313 0, /* source coords */
318 x_offset, /* dest coords */
330 chat_view_avatar_cache_data_free (gpointer ptr)
332 AvatarData *data = ptr;
334 g_object_unref (data->pixbuf);
335 g_free (data->token);
336 g_slice_free (AvatarData, data);
340 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
343 EmpathyAvatar *avatar;
344 GdkPixbuf *tmp_pixbuf;
345 GdkPixbuf *pixbuf = NULL;
347 /* Check if avatar is in cache and if it's up to date */
348 avatar = empathy_contact_get_avatar (contact);
349 data = g_object_get_data (G_OBJECT (contact), "chat-view-avatar-cache");
351 if (avatar && !tp_strdiff (avatar->token, data->token)) {
352 /* We have the avatar in cache */
357 /* Avatar not in cache, create pixbuf */
358 tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
360 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
361 g_object_unref (tmp_pixbuf);
367 /* Insert new pixbuf in cache */
368 data = g_slice_new0 (AvatarData);
369 data->token = g_strdup (avatar->token);
370 data->pixbuf = pixbuf;
372 g_object_set_data_full (G_OBJECT (contact), "chat-view-avatar-cache",
373 data, chat_view_avatar_cache_data_free);
379 empathy_chat_view_get_smiley_menu (GCallback callback,
382 EmpathySmileyManager *smiley_manager;
388 g_return_val_if_fail (callback != NULL, NULL);
390 menu = gtk_menu_new ();
392 smiley_manager = empathy_smiley_manager_new ();
393 smileys = empathy_smiley_manager_get_all (smiley_manager);
394 for (l = smileys; l; l = l->next) {
395 EmpathySmiley *smiley;
400 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
402 item = gtk_image_menu_item_new_with_label ("");
403 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
405 gtk_menu_attach (GTK_MENU (menu), item,
408 gtk_widget_set_tooltip_text (item, smiley->str);
410 g_object_set_data (G_OBJECT (item), "smiley_text", smiley->str);
411 g_signal_connect (item, "activate", callback, user_data);
420 g_object_unref (smiley_manager);
422 gtk_widget_show_all (menu);