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