]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-irc.c
Merge branch 'debugger'
[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  * Copyright (C) 2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <glib/gi18n-lib.h>
27
28 #include <libempathy/empathy-utils.h>
29 #include "empathy-theme-irc.h"
30 #include "empathy-ui-utils.h"
31
32 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeIrc)
33 typedef struct {
34         gpointer dummy;
35 } EmpathyThemeIrcPriv;
36
37 G_DEFINE_TYPE (EmpathyThemeIrc, empathy_theme_irc, EMPATHY_TYPE_CHAT_TEXT_VIEW);
38
39 static void
40 theme_irc_create_tags (EmpathyThemeIrc *theme)
41 {
42         GtkTextBuffer *buffer;
43
44         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (theme));
45
46         gtk_text_buffer_create_tag (buffer, EMPATHY_THEME_IRC_TAG_NICK_SELF, NULL);
47         gtk_text_buffer_create_tag (buffer, EMPATHY_THEME_IRC_TAG_NICK_OTHER, NULL);
48         gtk_text_buffer_create_tag (buffer, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT, NULL);
49 }
50
51 static void
52 theme_irc_append_message (EmpathyChatTextView *view,
53                           EmpathyMessage      *message)
54 {
55         GtkTextBuffer *buffer;
56         const gchar   *name;
57         const gchar   *nick_tag;
58         GtkTextIter    iter;
59         gchar         *tmp;
60         EmpathyContact *contact;
61
62         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
63
64         contact = empathy_message_get_sender (message);
65         name = empathy_contact_get_name (contact);
66
67         if (empathy_message_get_tptype (message) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
68                 tmp = g_strdup_printf (" * %s %s",
69                                        empathy_contact_get_name (contact),
70                                        empathy_message_get_body (message));
71                 empathy_chat_text_view_append_body (view, tmp,
72                                                     EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION);
73                 g_free (tmp);
74                 return;
75         }
76
77         if (empathy_contact_is_user (contact)) {
78                 nick_tag = EMPATHY_THEME_IRC_TAG_NICK_SELF;
79         } else {
80                 if (empathy_message_should_highlight (message)) {
81                         nick_tag = EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT;
82                 } else {
83                         nick_tag = EMPATHY_THEME_IRC_TAG_NICK_OTHER;
84                 }
85         }
86
87         gtk_text_buffer_get_end_iter (buffer, &iter);
88
89         /* The nickname. */
90         tmp = g_strdup_printf ("%s: ", name);
91         gtk_text_buffer_insert_with_tags_by_name (buffer,
92                                                   &iter,
93                                                   tmp,
94                                                   -1,
95                                                   "cut",
96                                                   nick_tag,
97                                                   NULL);
98         g_free (tmp);
99
100         /* The text body. */
101         empathy_chat_text_view_append_body (view,
102                                             empathy_message_get_body (message),
103                                             EMPATHY_CHAT_TEXT_VIEW_TAG_BODY);
104 }
105
106 static void
107 empathy_theme_irc_class_init (EmpathyThemeIrcClass *class)
108 {
109         GObjectClass             *object_class;
110         EmpathyChatTextViewClass *chat_text_view_class;
111
112         object_class = G_OBJECT_CLASS (class);
113         chat_text_view_class = EMPATHY_CHAT_TEXT_VIEW_CLASS (class);
114
115         chat_text_view_class->append_message = theme_irc_append_message;
116
117         g_type_class_add_private (object_class, sizeof (EmpathyThemeIrcPriv));
118 }
119
120 static void
121 empathy_theme_irc_init (EmpathyThemeIrc *theme)
122 {
123         EmpathyThemeIrcPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
124                 EMPATHY_TYPE_THEME_IRC, EmpathyThemeIrcPriv);
125
126         theme->priv = priv;
127
128         theme_irc_create_tags (theme);
129
130         /* Define margin */
131         g_object_set (theme,
132                       "left-margin", 3,
133                       "right-margin", 3,
134                       NULL);
135 }
136
137 EmpathyThemeIrc *
138 empathy_theme_irc_new (void)
139 {
140         return g_object_new (EMPATHY_TYPE_THEME_IRC, NULL);
141 }
142