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) EMPATHY_GET_PRIV (obj, EmpathyTheme)
39 EmpathySmileyManager *smiley_manager;
40 gboolean show_avatars;
43 static void theme_finalize (GObject *object);
44 static void theme_get_property (GObject *object,
48 static void theme_set_property (GObject *object,
54 G_DEFINE_TYPE (EmpathyTheme, empathy_theme, G_TYPE_OBJECT);
62 empathy_theme_class_init (EmpathyThemeClass *class)
64 GObjectClass *object_class;
66 object_class = G_OBJECT_CLASS (class);
68 object_class->finalize = theme_finalize;
69 object_class->get_property = theme_get_property;
70 object_class->set_property = theme_set_property;
72 class->update_view = NULL;
73 class->append_message = NULL;
74 class->append_event = NULL;
75 class->append_timestamp = NULL;
76 class->append_spacing = NULL;
78 g_object_class_install_property (object_class,
80 g_param_spec_boolean ("show-avatars",
85 g_type_class_add_private (object_class, sizeof (EmpathyThemePriv));
89 empathy_theme_init (EmpathyTheme *theme)
91 EmpathyThemePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
92 EMPATHY_TYPE_THEME, EmpathyThemePriv);
95 priv->smiley_manager = empathy_smiley_manager_new ();
99 theme_finalize (GObject *object)
101 EmpathyThemePriv *priv;
103 priv = GET_PRIV (object);
105 if (priv->smiley_manager) {
106 g_object_unref (priv->smiley_manager);
109 (G_OBJECT_CLASS (empathy_theme_parent_class)->finalize) (object);
113 theme_get_property (GObject *object,
118 EmpathyThemePriv *priv;
120 priv = GET_PRIV (object);
123 case PROP_SHOW_AVATARS:
124 g_value_set_boolean (value, priv->show_avatars);
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
133 theme_set_property (GObject *object,
138 EmpathyThemePriv *priv;
140 priv = GET_PRIV (object);
143 case PROP_SHOW_AVATARS:
144 empathy_theme_set_show_avatars (EMPATHY_THEME (object),
145 g_value_get_boolean (value));
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
154 empathy_theme_maybe_append_date_and_time (EmpathyTheme *theme,
155 EmpathyChatView *view,
156 EmpathyMessage *message)
159 GDate *date, *last_date;
160 gboolean append_date, append_time;
162 date = empathy_message_get_date_and_time (message, ×tamp);
164 last_date = g_date_new ();
165 g_date_set_time_t (last_date, empathy_chat_view_get_last_timestamp (view));
170 if (g_date_compare (date, last_date) > 0) {
175 g_date_free (last_date);
178 if (empathy_chat_view_get_last_timestamp (view) + TIMESTAMP_INTERVAL < timestamp) {
182 if (append_time || append_date) {
183 empathy_theme_append_timestamp (theme, view, message,
184 append_date, append_time);
189 empathy_theme_update_view (EmpathyTheme *theme,
190 EmpathyChatView *view)
192 if (!EMPATHY_THEME_GET_CLASS(theme)->update_view) {
193 g_error ("Theme must override update_view");
196 return EMPATHY_THEME_GET_CLASS(theme)->update_view (theme, view);
200 empathy_theme_append_message (EmpathyTheme *theme,
201 EmpathyChatView *view,
202 EmpathyMessage *message)
204 if (!EMPATHY_THEME_GET_CLASS(theme)->append_message) {
205 g_warning ("Theme should override append_message");
209 EMPATHY_THEME_GET_CLASS(theme)->append_message (theme, view, message);
213 theme_insert_text_with_emoticons (GtkTextBuffer *buf,
216 EmpathySmileyManager *smiley_manager)
218 gboolean use_smileys = FALSE;
221 empathy_conf_get_bool (empathy_conf_get (),
222 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
226 gtk_text_buffer_insert (buf, iter, str, -1);
230 smileys = empathy_smiley_manager_parse (smiley_manager, str);
231 for (l = smileys; l; l = l->next) {
232 EmpathySmiley *smiley;
235 if (smiley->pixbuf) {
236 gtk_text_buffer_insert_pixbuf (buf, iter, smiley->pixbuf);
238 gtk_text_buffer_insert (buf, iter, smiley->str, -1);
240 empathy_smiley_free (smiley);
242 g_slist_free (smileys);
246 empathy_theme_append_text (EmpathyTheme *theme,
247 EmpathyChatView *view,
250 const gchar *link_tag)
252 EmpathyThemePriv *priv;
253 GtkTextBuffer *buffer;
254 GtkTextIter start_iter, end_iter;
260 priv = GET_PRIV (theme);
261 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
263 gtk_text_buffer_get_end_iter (buffer, &start_iter);
264 mark = gtk_text_buffer_create_mark (buffer, NULL, &start_iter, TRUE);
266 start = g_array_new (FALSE, FALSE, sizeof (gint));
267 end = g_array_new (FALSE, FALSE, sizeof (gint));
269 num_matches = empathy_regex_match (EMPATHY_REGEX_ALL, body, start, end);
271 if (num_matches == 0) {
272 gtk_text_buffer_get_end_iter (buffer, &iter);
273 theme_insert_text_with_emoticons (buffer, &iter, body, priv->smiley_manager);
279 for (i = 0; i < num_matches; i++) {
280 s = g_array_index (start, gint, i);
281 e = g_array_index (end, gint, i);
284 tmp = empathy_substring (body, last, s);
286 gtk_text_buffer_get_end_iter (buffer, &iter);
287 theme_insert_text_with_emoticons (buffer,
290 priv->smiley_manager);
294 tmp = empathy_substring (body, s, e);
296 gtk_text_buffer_get_end_iter (buffer, &iter);
298 gtk_text_buffer_insert (buffer, &iter,
301 gtk_text_buffer_insert_with_tags_by_name (buffer,
315 if (e < strlen (body)) {
316 tmp = empathy_substring (body, e, strlen (body));
318 gtk_text_buffer_get_end_iter (buffer, &iter);
319 theme_insert_text_with_emoticons (buffer,
322 priv->smiley_manager);
327 g_array_free (start, TRUE);
328 g_array_free (end, TRUE);
330 gtk_text_buffer_get_end_iter (buffer, &iter);
331 gtk_text_buffer_insert (buffer, &iter, "\n", 1);
333 /* Apply the style to the inserted text. */
334 gtk_text_buffer_get_iter_at_mark (buffer, &start_iter, mark);
335 gtk_text_buffer_get_end_iter (buffer, &end_iter);
337 gtk_text_buffer_apply_tag_by_name (buffer,
342 gtk_text_buffer_delete_mark (buffer, mark);
346 empathy_theme_append_event (EmpathyTheme *theme,
347 EmpathyChatView *view,
350 if (!EMPATHY_THEME_GET_CLASS(theme)->append_event) {
354 EMPATHY_THEME_GET_CLASS(theme)->append_event (theme, view, str);
358 empathy_theme_append_spacing (EmpathyTheme *theme,
359 EmpathyChatView *view)
361 if (!EMPATHY_THEME_GET_CLASS(theme)->append_spacing) {
365 EMPATHY_THEME_GET_CLASS(theme)->append_spacing (theme, view);
370 empathy_theme_append_timestamp (EmpathyTheme *theme,
371 EmpathyChatView *view,
372 EmpathyMessage *message,
376 if (!EMPATHY_THEME_GET_CLASS(theme)->append_timestamp) {
380 EMPATHY_THEME_GET_CLASS(theme)->append_timestamp (theme, view,
386 empathy_theme_get_show_avatars (EmpathyTheme *theme)
388 EmpathyThemePriv *priv;
390 g_return_val_if_fail (EMPATHY_IS_THEME (theme), FALSE);
392 priv = GET_PRIV (theme);
394 return priv->show_avatars;
398 empathy_theme_set_show_avatars (EmpathyTheme *theme, gboolean show)
400 EmpathyThemePriv *priv;
402 g_return_if_fail (EMPATHY_IS_THEME (theme));
404 priv = GET_PRIV (theme);
406 priv->show_avatars = show;
408 g_object_notify (G_OBJECT (theme), "show-avatars");