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-conf.h>
28 #include <libempathy/empathy-debug.h>
29 #include <libempathy/empathy-utils.h>
30 #include <libempathy/empathy-marshal.h>
32 #include "empathy-chat.h"
33 #include "empathy-preferences.h"
34 #include "empathy-theme-utils.h"
35 #include "empathy-theme.h"
36 #include "empathy-smiley-manager.h"
38 #define DEBUG_DOMAIN "Theme"
40 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
41 #define TIMESTAMP_INTERVAL 300
43 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_THEME, EmpathyThemePriv))
45 typedef struct _EmpathyThemePriv EmpathyThemePriv;
47 struct _EmpathyThemePriv {
48 EmpathySmileyManager *smiley_manager;
49 gboolean show_avatars;
52 static void theme_finalize (GObject *object);
53 static void theme_get_property (GObject *object,
57 static void theme_set_property (GObject *object,
63 G_DEFINE_TYPE (EmpathyTheme, empathy_theme, G_TYPE_OBJECT);
75 static guint signals[LAST_SIGNAL] = { 0 };
78 empathy_theme_class_init (EmpathyThemeClass *class)
80 GObjectClass *object_class;
82 object_class = G_OBJECT_CLASS (class);
84 object_class->finalize = theme_finalize;
85 object_class->get_property = theme_get_property;
86 object_class->set_property = theme_set_property;
88 class->setup_with_view = NULL;
89 class->view_cleared = NULL;
90 class->append_message = NULL;
91 class->append_event = NULL;
92 class->append_timestamp = NULL;
93 class->append_spacing = NULL;
95 g_object_class_install_property (object_class,
97 g_param_spec_boolean ("show-avatars",
103 g_signal_new ("updated",
104 G_TYPE_FROM_CLASS (class),
108 empathy_marshal_VOID__VOID,
112 g_type_class_add_private (object_class, sizeof (EmpathyThemePriv));
116 empathy_theme_init (EmpathyTheme *presence)
118 EmpathyThemePriv *priv;
120 priv = GET_PRIV (presence);
122 priv->smiley_manager = empathy_smiley_manager_new ();
126 theme_finalize (GObject *object)
128 EmpathyThemePriv *priv;
130 priv = GET_PRIV (object);
132 if (priv->smiley_manager) {
133 g_object_unref (priv->smiley_manager);
136 (G_OBJECT_CLASS (empathy_theme_parent_class)->finalize) (object);
140 theme_get_property (GObject *object,
145 EmpathyThemePriv *priv;
147 priv = GET_PRIV (object);
150 case PROP_SHOW_AVATARS:
151 g_value_set_boolean (value, priv->show_avatars);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
160 theme_set_property (GObject *object,
165 EmpathyThemePriv *priv;
167 priv = GET_PRIV (object);
170 case PROP_SHOW_AVATARS:
171 priv->show_avatars = g_value_get_boolean (value);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
180 empathy_theme_maybe_append_date_and_time (EmpathyTheme *theme,
181 EmpathyThemeContext *context,
182 EmpathyChatView *view,
183 EmpathyMessage *message)
186 GDate *date, *last_date;
187 gboolean append_date, append_time;
189 date = empathy_message_get_date_and_time (message, ×tamp);
191 last_date = g_date_new ();
192 g_date_set_time (last_date, empathy_chat_view_get_last_timestamp (view));
197 if (g_date_compare (date, last_date) > 0) {
202 g_date_free (last_date);
205 if (empathy_chat_view_get_last_timestamp (view) + TIMESTAMP_INTERVAL < timestamp) {
209 if (append_time || append_date) {
210 empathy_theme_append_timestamp (theme, context,
212 append_date, append_time);
217 empathy_theme_new (void)
221 theme = g_object_new (EMPATHY_TYPE_THEME, NULL);
226 EmpathyThemeContext *
227 empathy_theme_setup_with_view (EmpathyTheme *theme,
228 EmpathyChatView *view)
230 if (!EMPATHY_THEME_GET_CLASS(theme)->setup_with_view) {
231 g_error ("Theme must override setup_with_view");
234 return EMPATHY_THEME_GET_CLASS(theme)->setup_with_view (theme, view);
238 empathy_theme_detach_from_view (EmpathyTheme *theme,
239 EmpathyThemeContext *context,
240 EmpathyChatView *view)
242 if (!EMPATHY_THEME_GET_CLASS(theme)->detach_from_view) {
243 g_error ("Theme must override detach_from_view");
246 return EMPATHY_THEME_GET_CLASS(theme)->detach_from_view (theme, context,
251 empathy_theme_view_cleared (EmpathyTheme *theme,
252 EmpathyThemeContext *context,
253 EmpathyChatView *view)
255 if (!EMPATHY_THEME_GET_CLASS(theme)->view_cleared) {
259 EMPATHY_THEME_GET_CLASS(theme)->view_cleared (theme, context, view);
263 empathy_theme_append_message (EmpathyTheme *theme,
264 EmpathyThemeContext *context,
265 EmpathyChatView *view,
266 EmpathyMessage *message)
268 if (!EMPATHY_THEME_GET_CLASS(theme)->append_message) {
269 g_warning ("Theme should override append_message");
273 EMPATHY_THEME_GET_CLASS(theme)->append_message (theme, context, view,
278 theme_insert_text_with_emoticons (GtkTextBuffer *buf,
281 EmpathySmileyManager *smiley_manager)
283 gboolean use_smileys = FALSE;
286 empathy_conf_get_bool (empathy_conf_get (),
287 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
291 gtk_text_buffer_insert (buf, iter, str, -1);
295 smileys = empathy_smiley_manager_parse (smiley_manager, str);
296 for (l = smileys; l; l = l->next) {
297 EmpathySmiley *smiley;
300 if (smiley->pixbuf) {
301 gtk_text_buffer_insert_pixbuf (buf, iter, smiley->pixbuf);
303 gtk_text_buffer_insert (buf, iter, smiley->str, -1);
305 empathy_smiley_free (smiley);
307 g_slist_free (smileys);
311 empathy_theme_append_text (EmpathyTheme *theme,
312 EmpathyThemeContext *context,
313 EmpathyChatView *view,
316 const gchar *link_tag)
318 EmpathyThemePriv *priv;
319 GtkTextBuffer *buffer;
320 GtkTextIter start_iter, end_iter;
326 priv = GET_PRIV (theme);
327 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
329 gtk_text_buffer_get_end_iter (buffer, &start_iter);
330 mark = gtk_text_buffer_create_mark (buffer, NULL, &start_iter, TRUE);
332 start = g_array_new (FALSE, FALSE, sizeof (gint));
333 end = g_array_new (FALSE, FALSE, sizeof (gint));
335 num_matches = empathy_regex_match (EMPATHY_REGEX_ALL, body, start, end);
337 if (num_matches == 0) {
338 gtk_text_buffer_get_end_iter (buffer, &iter);
339 theme_insert_text_with_emoticons (buffer, &iter, body, priv->smiley_manager);
345 for (i = 0; i < num_matches; i++) {
346 s = g_array_index (start, gint, i);
347 e = g_array_index (end, gint, i);
350 tmp = empathy_substring (body, last, s);
352 gtk_text_buffer_get_end_iter (buffer, &iter);
353 theme_insert_text_with_emoticons (buffer,
356 priv->smiley_manager);
360 tmp = empathy_substring (body, s, e);
362 gtk_text_buffer_get_end_iter (buffer, &iter);
364 gtk_text_buffer_insert (buffer, &iter,
367 gtk_text_buffer_insert_with_tags_by_name (buffer,
381 if (e < strlen (body)) {
382 tmp = empathy_substring (body, e, strlen (body));
384 gtk_text_buffer_get_end_iter (buffer, &iter);
385 theme_insert_text_with_emoticons (buffer,
388 priv->smiley_manager);
393 g_array_free (start, TRUE);
394 g_array_free (end, TRUE);
396 gtk_text_buffer_get_end_iter (buffer, &iter);
397 gtk_text_buffer_insert (buffer, &iter, "\n", 1);
399 /* Apply the style to the inserted text. */
400 gtk_text_buffer_get_iter_at_mark (buffer, &start_iter, mark);
401 gtk_text_buffer_get_end_iter (buffer, &end_iter);
403 gtk_text_buffer_apply_tag_by_name (buffer,
408 gtk_text_buffer_delete_mark (buffer, mark);
412 empathy_theme_append_event (EmpathyTheme *theme,
413 EmpathyThemeContext *context,
414 EmpathyChatView *view,
417 if (!EMPATHY_THEME_GET_CLASS(theme)->append_event) {
421 EMPATHY_THEME_GET_CLASS(theme)->append_event (theme, context, view, str);
425 empathy_theme_append_spacing (EmpathyTheme *theme,
426 EmpathyThemeContext *context,
427 EmpathyChatView *view)
429 if (!EMPATHY_THEME_GET_CLASS(theme)->append_spacing) {
433 EMPATHY_THEME_GET_CLASS(theme)->append_spacing (theme, context, view);
438 empathy_theme_append_timestamp (EmpathyTheme *theme,
439 EmpathyThemeContext *context,
440 EmpathyChatView *view,
441 EmpathyMessage *message,
445 if (!EMPATHY_THEME_GET_CLASS(theme)->append_timestamp) {
449 EMPATHY_THEME_GET_CLASS(theme)->append_timestamp (theme, context, view,
455 empathy_theme_get_show_avatars (EmpathyTheme *theme)
457 EmpathyThemePriv *priv;
459 g_return_val_if_fail (EMPATHY_IS_THEME (theme), FALSE);
461 priv = GET_PRIV (theme);
463 return priv->show_avatars;
467 empathy_theme_set_show_avatars (EmpathyTheme *theme, gboolean show)
469 EmpathyThemePriv *priv;
471 g_return_if_fail (EMPATHY_IS_THEME (theme));
473 priv = GET_PRIV (theme);
475 priv->show_avatars = show;
477 g_object_notify (G_OBJECT (theme), "show-avatars");