1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Imendio AB
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.
24 #include <glib/gi18n.h>
27 #include <libempathy/empathy-utils.h>
29 #include "empathy-chat.h"
30 #include "empathy-conf.h"
31 #include "empathy-theme.h"
32 #include "empathy-smiley-manager.h"
34 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
35 #define TIMESTAMP_INTERVAL 300
37 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_THEME, EmpathyThemePriv))
39 typedef struct _EmpathyThemePriv EmpathyThemePriv;
41 struct _EmpathyThemePriv {
42 EmpathySmileyManager *smiley_manager;
43 gboolean show_avatars;
46 static void theme_finalize (GObject *object);
47 static void theme_get_property (GObject *object,
51 static void theme_set_property (GObject *object,
57 G_DEFINE_TYPE (EmpathyTheme, empathy_theme, G_TYPE_OBJECT);
65 empathy_theme_class_init (EmpathyThemeClass *class)
67 GObjectClass *object_class;
69 object_class = G_OBJECT_CLASS (class);
71 object_class->finalize = theme_finalize;
72 object_class->get_property = theme_get_property;
73 object_class->set_property = theme_set_property;
75 class->update_view = NULL;
76 class->append_message = NULL;
77 class->append_event = NULL;
78 class->append_timestamp = NULL;
79 class->append_spacing = NULL;
81 g_object_class_install_property (object_class,
83 g_param_spec_boolean ("show-avatars",
88 g_type_class_add_private (object_class, sizeof (EmpathyThemePriv));
92 empathy_theme_init (EmpathyTheme *presence)
94 EmpathyThemePriv *priv;
96 priv = GET_PRIV (presence);
98 priv->smiley_manager = empathy_smiley_manager_new ();
102 theme_finalize (GObject *object)
104 EmpathyThemePriv *priv;
106 priv = GET_PRIV (object);
108 if (priv->smiley_manager) {
109 g_object_unref (priv->smiley_manager);
112 (G_OBJECT_CLASS (empathy_theme_parent_class)->finalize) (object);
116 theme_get_property (GObject *object,
121 EmpathyThemePriv *priv;
123 priv = GET_PRIV (object);
126 case PROP_SHOW_AVATARS:
127 g_value_set_boolean (value, priv->show_avatars);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
136 theme_set_property (GObject *object,
141 EmpathyThemePriv *priv;
143 priv = GET_PRIV (object);
146 case PROP_SHOW_AVATARS:
147 empathy_theme_set_show_avatars (EMPATHY_THEME (object),
148 g_value_get_boolean (value));
151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
157 empathy_theme_maybe_append_date_and_time (EmpathyTheme *theme,
158 EmpathyChatView *view,
159 EmpathyMessage *message)
162 GDate *date, *last_date;
163 gboolean append_date, append_time;
165 date = empathy_message_get_date_and_time (message, ×tamp);
167 last_date = g_date_new ();
168 g_date_set_time_t (last_date, empathy_chat_view_get_last_timestamp (view));
173 if (g_date_compare (date, last_date) > 0) {
178 g_date_free (last_date);
181 if (empathy_chat_view_get_last_timestamp (view) + TIMESTAMP_INTERVAL < timestamp) {
185 if (append_time || append_date) {
186 empathy_theme_append_timestamp (theme, view, message,
187 append_date, append_time);
192 empathy_theme_update_view (EmpathyTheme *theme,
193 EmpathyChatView *view)
195 if (!EMPATHY_THEME_GET_CLASS(theme)->update_view) {
196 g_error ("Theme must override update_view");
199 return EMPATHY_THEME_GET_CLASS(theme)->update_view (theme, view);
203 empathy_theme_append_message (EmpathyTheme *theme,
204 EmpathyChatView *view,
205 EmpathyMessage *message)
207 if (!EMPATHY_THEME_GET_CLASS(theme)->append_message) {
208 g_warning ("Theme should override append_message");
212 EMPATHY_THEME_GET_CLASS(theme)->append_message (theme, view, message);
216 theme_insert_text_with_emoticons (GtkTextBuffer *buf,
219 EmpathySmileyManager *smiley_manager)
221 gboolean use_smileys = FALSE;
224 empathy_conf_get_bool (empathy_conf_get (),
225 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
229 gtk_text_buffer_insert (buf, iter, str, -1);
233 smileys = empathy_smiley_manager_parse (smiley_manager, str);
234 for (l = smileys; l; l = l->next) {
235 EmpathySmiley *smiley;
238 if (smiley->pixbuf) {
239 gtk_text_buffer_insert_pixbuf (buf, iter, smiley->pixbuf);
241 gtk_text_buffer_insert (buf, iter, smiley->str, -1);
243 empathy_smiley_free (smiley);
245 g_slist_free (smileys);
249 empathy_theme_append_text (EmpathyTheme *theme,
250 EmpathyChatView *view,
253 const gchar *link_tag)
255 EmpathyThemePriv *priv;
256 GtkTextBuffer *buffer;
257 GtkTextIter start_iter, end_iter;
263 priv = GET_PRIV (theme);
264 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
266 gtk_text_buffer_get_end_iter (buffer, &start_iter);
267 mark = gtk_text_buffer_create_mark (buffer, NULL, &start_iter, TRUE);
269 start = g_array_new (FALSE, FALSE, sizeof (gint));
270 end = g_array_new (FALSE, FALSE, sizeof (gint));
272 num_matches = empathy_regex_match (EMPATHY_REGEX_ALL, body, start, end);
274 if (num_matches == 0) {
275 gtk_text_buffer_get_end_iter (buffer, &iter);
276 theme_insert_text_with_emoticons (buffer, &iter, body, priv->smiley_manager);
282 for (i = 0; i < num_matches; i++) {
283 s = g_array_index (start, gint, i);
284 e = g_array_index (end, gint, i);
287 tmp = empathy_substring (body, last, s);
289 gtk_text_buffer_get_end_iter (buffer, &iter);
290 theme_insert_text_with_emoticons (buffer,
293 priv->smiley_manager);
297 tmp = empathy_substring (body, s, e);
299 gtk_text_buffer_get_end_iter (buffer, &iter);
301 gtk_text_buffer_insert (buffer, &iter,
304 gtk_text_buffer_insert_with_tags_by_name (buffer,
318 if (e < strlen (body)) {
319 tmp = empathy_substring (body, e, strlen (body));
321 gtk_text_buffer_get_end_iter (buffer, &iter);
322 theme_insert_text_with_emoticons (buffer,
325 priv->smiley_manager);
330 g_array_free (start, TRUE);
331 g_array_free (end, TRUE);
333 gtk_text_buffer_get_end_iter (buffer, &iter);
334 gtk_text_buffer_insert (buffer, &iter, "\n", 1);
336 /* Apply the style to the inserted text. */
337 gtk_text_buffer_get_iter_at_mark (buffer, &start_iter, mark);
338 gtk_text_buffer_get_end_iter (buffer, &end_iter);
340 gtk_text_buffer_apply_tag_by_name (buffer,
345 gtk_text_buffer_delete_mark (buffer, mark);
349 empathy_theme_append_event (EmpathyTheme *theme,
350 EmpathyChatView *view,
353 if (!EMPATHY_THEME_GET_CLASS(theme)->append_event) {
357 EMPATHY_THEME_GET_CLASS(theme)->append_event (theme, view, str);
361 empathy_theme_append_spacing (EmpathyTheme *theme,
362 EmpathyChatView *view)
364 if (!EMPATHY_THEME_GET_CLASS(theme)->append_spacing) {
368 EMPATHY_THEME_GET_CLASS(theme)->append_spacing (theme, view);
373 empathy_theme_append_timestamp (EmpathyTheme *theme,
374 EmpathyChatView *view,
375 EmpathyMessage *message,
379 if (!EMPATHY_THEME_GET_CLASS(theme)->append_timestamp) {
383 EMPATHY_THEME_GET_CLASS(theme)->append_timestamp (theme, view,
389 empathy_theme_get_show_avatars (EmpathyTheme *theme)
391 EmpathyThemePriv *priv;
393 g_return_val_if_fail (EMPATHY_IS_THEME (theme), FALSE);
395 priv = GET_PRIV (theme);
397 return priv->show_avatars;
401 empathy_theme_set_show_avatars (EmpathyTheme *theme, gboolean show)
403 EmpathyThemePriv *priv;
405 g_return_if_fail (EMPATHY_IS_THEME (theme));
407 priv = GET_PRIV (theme);
409 priv->show_avatars = show;
411 g_object_notify (G_OBJECT (theme), "show-avatars");