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-lib.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 SCHEMES "(https?|ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
39 #define BODY "([^\\ ]+)"
40 #define END_BODY "([^\\ ]*[^,;\?><()\\ \"\\.])"
41 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
42 "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
43 "|((www|ftp)\\."END_BODY")"
44 static GRegex *uri_regex = NULL;
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTheme)
49 EmpathySmileyManager *smiley_manager;
50 gboolean show_avatars;
53 static void theme_finalize (GObject *object);
54 static void theme_get_property (GObject *object,
58 static void theme_set_property (GObject *object,
64 G_DEFINE_TYPE (EmpathyTheme, empathy_theme, G_TYPE_OBJECT);
72 empathy_theme_class_init (EmpathyThemeClass *class)
74 GObjectClass *object_class;
76 object_class = G_OBJECT_CLASS (class);
78 object_class->finalize = theme_finalize;
79 object_class->get_property = theme_get_property;
80 object_class->set_property = theme_set_property;
82 class->update_view = NULL;
83 class->append_message = NULL;
84 class->append_event = NULL;
85 class->append_timestamp = NULL;
86 class->append_spacing = NULL;
88 g_object_class_install_property (object_class,
90 g_param_spec_boolean ("show-avatars",
95 g_type_class_add_private (object_class, sizeof (EmpathyThemePriv));
99 empathy_theme_init (EmpathyTheme *theme)
101 EmpathyThemePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
102 EMPATHY_TYPE_THEME, EmpathyThemePriv);
105 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
109 theme_finalize (GObject *object)
111 EmpathyThemePriv *priv;
113 priv = GET_PRIV (object);
115 if (priv->smiley_manager) {
116 g_object_unref (priv->smiley_manager);
119 (G_OBJECT_CLASS (empathy_theme_parent_class)->finalize) (object);
123 theme_get_property (GObject *object,
128 EmpathyThemePriv *priv;
130 priv = GET_PRIV (object);
133 case PROP_SHOW_AVATARS:
134 g_value_set_boolean (value, priv->show_avatars);
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
143 theme_set_property (GObject *object,
148 EmpathyThemePriv *priv;
150 priv = GET_PRIV (object);
153 case PROP_SHOW_AVATARS:
154 empathy_theme_set_show_avatars (EMPATHY_THEME (object),
155 g_value_get_boolean (value));
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
164 empathy_theme_maybe_append_date_and_time (EmpathyTheme *theme,
165 EmpathyChatView *view,
166 EmpathyMessage *message)
169 GDate *date, *last_date;
170 gboolean append_date, append_time;
172 date = empathy_message_get_date_and_time (message, ×tamp);
174 last_date = g_date_new ();
175 g_date_set_time_t (last_date, empathy_chat_view_get_last_timestamp (view));
180 if (g_date_compare (date, last_date) > 0) {
185 g_date_free (last_date);
188 if (empathy_chat_view_get_last_timestamp (view) + TIMESTAMP_INTERVAL < timestamp) {
192 if (append_time || append_date) {
193 empathy_theme_append_timestamp (theme, view, message,
194 append_date, append_time);
199 empathy_theme_update_view (EmpathyTheme *theme,
200 EmpathyChatView *view)
202 if (!EMPATHY_THEME_GET_CLASS(theme)->update_view) {
203 g_error ("Theme must override update_view");
206 EMPATHY_THEME_GET_CLASS(theme)->update_view (theme, view);
210 empathy_theme_append_message (EmpathyTheme *theme,
211 EmpathyChatView *view,
212 EmpathyMessage *message)
214 if (!EMPATHY_THEME_GET_CLASS(theme)->append_message) {
215 g_warning ("Theme should override append_message");
219 EMPATHY_THEME_GET_CLASS(theme)->append_message (theme, view, message);
223 theme_insert_text_with_emoticons (GtkTextBuffer *buf,
226 EmpathySmileyManager *smiley_manager)
228 gboolean use_smileys = FALSE;
231 empathy_conf_get_bool (empathy_conf_get (),
232 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
236 gtk_text_buffer_insert (buf, iter, str, -1);
240 smileys = empathy_smiley_manager_parse (smiley_manager, str);
241 for (l = smileys; l; l = l->next) {
242 EmpathySmiley *smiley;
245 if (smiley->pixbuf) {
246 gtk_text_buffer_insert_pixbuf (buf, iter, smiley->pixbuf);
248 gtk_text_buffer_insert (buf, iter, smiley->str, -1);
250 empathy_smiley_free (smiley);
252 g_slist_free (smileys);
256 empathy_theme_append_text (EmpathyTheme *theme,
257 EmpathyChatView *view,
260 const gchar *link_tag)
262 EmpathyThemePriv *priv;
263 GtkTextBuffer *buffer;
264 GtkTextIter start_iter, end_iter;
267 GMatchInfo *match_info;
273 priv = GET_PRIV (theme);
274 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
276 gtk_text_buffer_get_end_iter (buffer, &start_iter);
277 mark = gtk_text_buffer_create_mark (buffer, NULL, &start_iter, TRUE);
280 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
283 for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
284 match = g_match_info_next (match_info, NULL)) {
285 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
289 tmp = empathy_substring (body, last, s);
291 gtk_text_buffer_get_end_iter (buffer, &iter);
292 theme_insert_text_with_emoticons (buffer,
295 priv->smiley_manager);
299 tmp = empathy_substring (body, s, e);
301 gtk_text_buffer_get_end_iter (buffer, &iter);
303 gtk_text_buffer_insert (buffer, &iter,
306 gtk_text_buffer_insert_with_tags_by_name (buffer,
318 g_match_info_free (match_info);
320 if (last < strlen (body)) {
321 gtk_text_buffer_get_end_iter (buffer, &iter);
322 theme_insert_text_with_emoticons (buffer,
325 priv->smiley_manager);
328 gtk_text_buffer_get_end_iter (buffer, &iter);
329 gtk_text_buffer_insert (buffer, &iter, "\n", 1);
331 /* Apply the style to the inserted text. */
332 gtk_text_buffer_get_iter_at_mark (buffer, &start_iter, mark);
333 gtk_text_buffer_get_end_iter (buffer, &end_iter);
335 gtk_text_buffer_apply_tag_by_name (buffer,
340 gtk_text_buffer_delete_mark (buffer, mark);
344 empathy_theme_append_event (EmpathyTheme *theme,
345 EmpathyChatView *view,
348 if (!EMPATHY_THEME_GET_CLASS(theme)->append_event) {
352 EMPATHY_THEME_GET_CLASS(theme)->append_event (theme, view, str);
356 empathy_theme_append_spacing (EmpathyTheme *theme,
357 EmpathyChatView *view)
359 if (!EMPATHY_THEME_GET_CLASS(theme)->append_spacing) {
363 EMPATHY_THEME_GET_CLASS(theme)->append_spacing (theme, view);
368 empathy_theme_append_timestamp (EmpathyTheme *theme,
369 EmpathyChatView *view,
370 EmpathyMessage *message,
374 if (!EMPATHY_THEME_GET_CLASS(theme)->append_timestamp) {
378 EMPATHY_THEME_GET_CLASS(theme)->append_timestamp (theme, view,
384 empathy_theme_get_show_avatars (EmpathyTheme *theme)
386 EmpathyThemePriv *priv;
388 g_return_val_if_fail (EMPATHY_IS_THEME (theme), FALSE);
390 priv = GET_PRIV (theme);
392 return priv->show_avatars;
396 empathy_theme_set_show_avatars (EmpathyTheme *theme, gboolean show)
398 EmpathyThemePriv *priv;
400 g_return_if_fail (EMPATHY_IS_THEME (theme));
402 priv = GET_PRIV (theme);
404 priv->show_avatars = show;
406 g_object_notify (G_OBJECT (theme), "show-avatars");