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