]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-irc.c
7cd9588bb61a3cdd05ee9027a0c4fe6ed5a1cb2e
[empathy.git] / libempathy-gtk / empathy-theme-irc.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Imendio AB
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24 #include <libempathy/empathy-debug.h>
25
26 #include "empathy-chat.h"
27 #include "empathy-ui-utils.h"
28 #include "empathy-theme-irc.h"
29
30 #define DEBUG_DOMAIN "Theme"
31
32 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_THEME_IRC, EmpathyThemeIrcPriv))
33
34 typedef struct _EmpathyThemeIrcPriv EmpathyThemeIrcPriv;
35
36 struct _EmpathyThemeIrcPriv {
37         gint my_prop;
38 };
39
40 static void         theme_irc_finalize         (GObject             *object);
41 static void         theme_irc_update_view      (EmpathyTheme         *theme,
42                                                 EmpathyChatView      *view);
43 static void         theme_irc_append_message   (EmpathyTheme        *theme,
44                                                 EmpathyChatView     *view,
45                                                 EmpathyMessage      *message);
46 static void         theme_irc_append_event     (EmpathyTheme        *theme,
47                                                 EmpathyChatView     *view,
48                                                 const gchar        *str);
49 static void         theme_irc_append_timestamp (EmpathyTheme        *theme,
50                                                 EmpathyChatView     *view,
51                                                 EmpathyMessage      *message,
52                                                 gboolean            show_date,
53                                                 gboolean            show_time);
54 static void         theme_irc_append_spacing   (EmpathyTheme        *theme,
55                                                 EmpathyChatView     *view);
56
57
58 enum {
59         PROP_0,
60         PROP_MY_PROP
61 };
62
63 G_DEFINE_TYPE (EmpathyThemeIrc, empathy_theme_irc, EMPATHY_TYPE_THEME);
64
65 static void
66 empathy_theme_irc_class_init (EmpathyThemeIrcClass *class)
67 {
68         GObjectClass *object_class;
69         EmpathyThemeClass *theme_class;
70
71         object_class = G_OBJECT_CLASS (class);
72         theme_class  = EMPATHY_THEME_CLASS (class);
73
74         object_class->finalize     = theme_irc_finalize;
75
76         theme_class->update_view      = theme_irc_update_view;
77         theme_class->append_message   = theme_irc_append_message;
78         theme_class->append_event     = theme_irc_append_event;
79         theme_class->append_timestamp = theme_irc_append_timestamp;
80         theme_class->append_spacing   = theme_irc_append_spacing;
81
82         g_type_class_add_private (object_class, sizeof (EmpathyThemeIrcPriv));
83 }
84
85 static void
86 empathy_theme_irc_init (EmpathyThemeIrc *presence)
87 {
88         EmpathyThemeIrcPriv *priv;
89
90         priv = GET_PRIV (presence);
91 }
92
93 static void
94 theme_irc_finalize (GObject *object)
95 {
96         EmpathyThemeIrcPriv *priv;
97
98         priv = GET_PRIV (object);
99
100         (G_OBJECT_CLASS (empathy_theme_irc_parent_class)->finalize) (object);
101 }
102
103 static void
104 theme_irc_apply_theme_classic (EmpathyTheme *theme, EmpathyChatView *view)
105 {
106         EmpathyThemeIrcPriv *priv;
107         GtkTextBuffer       *buffer;
108
109         priv = GET_PRIV (theme);
110
111         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
112
113         empathy_text_buffer_tag_set (buffer, "irc-spacing",
114                                      "size", 2000,
115                                      NULL);
116
117         empathy_text_buffer_tag_set (buffer, "irc-nick-self",
118                                      "foreground", "sea green",
119                                      NULL);
120
121         empathy_text_buffer_tag_set (buffer, "irc-body-self",
122                                      /* To get the default theme color: */
123                                      "foreground-set", FALSE,
124                                      NULL);
125
126         empathy_text_buffer_tag_set (buffer, "irc-action-self",
127                                      "foreground", "brown4",
128                                      "style", PANGO_STYLE_ITALIC,
129                                      NULL);
130
131         empathy_text_buffer_tag_set (buffer, "irc-nick-highlight",
132                                      "foreground", "indian red",
133                                      "weight", PANGO_WEIGHT_BOLD,
134                                      NULL);
135
136         empathy_text_buffer_tag_set (buffer, "irc-nick-other",
137                                      "foreground", "skyblue4",
138                                      NULL);
139
140         empathy_text_buffer_tag_set (buffer, "irc-body-other",
141                                      /* To get the default theme color: */
142                                      "foreground-set", FALSE,
143                                      NULL);
144
145         empathy_text_buffer_tag_set (buffer, "irc-action-other",
146                                      "foreground", "brown4",
147                                      "style", PANGO_STYLE_ITALIC,
148                                      NULL);
149
150         empathy_text_buffer_tag_set (buffer, "irc-time",
151                                      "foreground", "darkgrey",
152                                      "justification", GTK_JUSTIFY_CENTER,
153                                      NULL);
154
155         empathy_text_buffer_tag_set (buffer, "irc-event",
156                                      "foreground", "PeachPuff4",
157                                      "justification", GTK_JUSTIFY_LEFT,
158                                      NULL);
159
160         empathy_text_buffer_tag_set (buffer, "invite",
161                                      "foreground", "sienna",
162                                      NULL);
163
164         empathy_text_buffer_tag_set (buffer, "irc-link",
165                                      "foreground", "steelblue",
166                                      "underline", PANGO_UNDERLINE_SINGLE,
167                                      NULL);
168 }
169
170
171 static void
172 theme_irc_update_view (EmpathyTheme *theme, EmpathyChatView *view)
173 {
174         theme_irc_apply_theme_classic (theme, view);
175         empathy_chat_view_set_margin (view, 3);
176 }
177
178 static void
179 theme_irc_append_message (EmpathyTheme        *theme,
180                           EmpathyChatView     *view,
181                           EmpathyMessage      *message)
182 {
183         GtkTextBuffer *buffer;
184         const gchar   *name;
185         const gchar   *nick_tag;
186         const gchar   *body_tag;
187         GtkTextIter    iter;
188         gchar         *tmp;
189         EmpathyContact *contact;
190
191         empathy_theme_maybe_append_date_and_time (theme, view, message);
192
193         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
194
195         contact = empathy_message_get_sender (message);
196         name = empathy_contact_get_name (contact);
197
198         if (empathy_message_get_type (message) == EMPATHY_MESSAGE_TYPE_ACTION) {
199                 if (empathy_contact_is_user (contact)) {
200                         body_tag = "irc-action-self";
201                 } else {
202                         body_tag = "irc-action-other";
203                 }
204
205                 tmp = g_strdup_printf (" * %s %s", 
206                                        empathy_contact_get_name (contact),
207                                        empathy_message_get_body (message));
208                 empathy_theme_append_text (theme, view, tmp,
209                                            body_tag, "irc-link");
210                 g_free (tmp);
211                 return;
212         }
213
214         if (empathy_contact_is_user (contact)) {
215                 nick_tag = "irc-nick-self";
216                 body_tag = "irc-body-self";
217         } else {
218                 if (empathy_chat_should_highlight_nick (message)) {
219                         nick_tag = "irc-nick-highlight";
220                 } else {
221                         nick_tag = "irc-nick-other";
222                 }
223
224                 body_tag = "irc-body-other";
225         }
226                 
227         gtk_text_buffer_get_end_iter (buffer, &iter);
228
229         /* The nickname. */
230         tmp = g_strdup_printf ("%s: ", name);
231         gtk_text_buffer_insert_with_tags_by_name (buffer,
232                                                   &iter,
233                                                   tmp,
234                                                   -1,
235                                                   "cut",
236                                                   nick_tag,
237                                                   NULL);
238         g_free (tmp);
239
240         /* The text body. */
241         empathy_theme_append_text (theme, view, 
242                                   empathy_message_get_body (message),
243                                   body_tag, "irc-link");
244 }
245
246 static void
247 theme_irc_append_event (EmpathyTheme        *theme,
248                     EmpathyChatView     *view,
249                     const gchar        *str)
250 {
251         GtkTextBuffer *buffer;
252         GtkTextIter    iter;
253         gchar         *msg;
254
255         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
256         
257         empathy_theme_maybe_append_date_and_time (theme, view, NULL);
258
259         gtk_text_buffer_get_end_iter (buffer, &iter);
260
261         msg = g_strdup_printf (" - %s\n", str);
262         gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
263                                                   msg, -1,
264                                                   "irc-event",
265                                                   NULL);
266         g_free (msg);
267 }
268
269 static void
270 theme_irc_append_timestamp (EmpathyTheme        *theme,
271                             EmpathyChatView     *view,
272                             EmpathyMessage      *message,
273                             gboolean            show_date,
274                             gboolean            show_time)
275 {
276         GtkTextBuffer *buffer;
277         time_t         timestamp;
278         GDate         *date;
279         GtkTextIter    iter;
280         GString       *str;
281
282         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
283
284         date = empathy_message_get_date_and_time (message, &timestamp);
285
286         str = g_string_new (NULL);
287
288         if (show_time || show_date) {
289                 empathy_theme_append_spacing (theme, view);
290
291                 g_string_append (str, "- ");
292         }
293
294         if (show_date) {
295                 gchar buf[256];
296
297                 g_date_strftime (buf, 256, _("%A %d %B %Y"), date);
298                 g_string_append (str, buf);
299
300                 if (show_time) {
301                         g_string_append (str, ", ");
302                 }
303         }
304
305         g_date_free (date);
306
307         if (show_time) {
308                 gchar *tmp;
309
310                 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
311                 g_string_append (str, tmp);
312                 g_free (tmp);
313         }
314
315         if (show_time || show_date) {
316                 g_string_append (str, " -\n");
317
318                 gtk_text_buffer_get_end_iter (buffer, &iter);
319                 gtk_text_buffer_insert_with_tags_by_name (buffer,
320                                                           &iter,
321                                                           str->str, -1,
322                                                           "irc-time",
323                                                           NULL);
324
325                 empathy_chat_view_set_last_timestamp (view, timestamp);
326         }
327
328         g_string_free (str, TRUE);
329 }
330
331 static void
332 theme_irc_append_spacing (EmpathyTheme        *theme,
333                           EmpathyChatView     *view)
334 {
335         GtkTextBuffer *buffer;
336         GtkTextIter    iter;
337
338         g_return_if_fail (EMPATHY_IS_THEME (theme));
339         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
340
341         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
342
343         gtk_text_buffer_get_end_iter (buffer, &iter);
344         gtk_text_buffer_insert_with_tags_by_name (buffer,
345                                                   &iter,
346                                                   "\n",
347                                                   -1,
348                                                   "cut",
349                                                   "irc-spacing",
350                                                   NULL);
351 }
352