]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-irc.c
factor out start_gnome_contacts()
[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                           gboolean             should_highlight)
55 {
56         GtkTextBuffer *buffer;
57         const gchar   *name;
58         const gchar   *nick_tag;
59         GtkTextIter    iter;
60         gchar         *tmp;
61         EmpathyContact *contact;
62
63         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
64
65         contact = empathy_message_get_sender (message);
66         name = empathy_contact_get_logged_alias (contact);
67
68         if (empathy_message_get_tptype (message) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
69                 tmp = g_strdup_printf (" * %s %s",
70                                        empathy_contact_get_logged_alias (contact),
71                                        empathy_message_get_body (message));
72                 empathy_chat_text_view_append_body (view, tmp,
73                                                     EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION);
74                 g_free (tmp);
75                 return;
76         }
77
78         if (empathy_contact_is_user (contact)) {
79                 nick_tag = EMPATHY_THEME_IRC_TAG_NICK_SELF;
80         } else {
81                 if (should_highlight) {
82                         nick_tag = EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT;
83                 } else {
84                         nick_tag = EMPATHY_THEME_IRC_TAG_NICK_OTHER;
85                 }
86         }
87
88         gtk_text_buffer_get_end_iter (buffer, &iter);
89
90         /* The nickname. */
91         tmp = g_strdup_printf ("%s: ", name);
92         gtk_text_buffer_insert_with_tags_by_name (buffer,
93                                                   &iter,
94                                                   tmp,
95                                                   -1,
96                                                   "cut",
97                                                   nick_tag,
98                                                   NULL);
99         g_free (tmp);
100
101         /* The text body. */
102         empathy_chat_text_view_append_body (view,
103                                             empathy_message_get_body (message),
104                                             EMPATHY_CHAT_TEXT_VIEW_TAG_BODY);
105 }
106
107 static void
108 empathy_theme_irc_class_init (EmpathyThemeIrcClass *class)
109 {
110         GObjectClass             *object_class;
111         EmpathyChatTextViewClass *chat_text_view_class;
112
113         object_class = G_OBJECT_CLASS (class);
114         chat_text_view_class = EMPATHY_CHAT_TEXT_VIEW_CLASS (class);
115
116         chat_text_view_class->append_message = theme_irc_append_message;
117
118         g_type_class_add_private (object_class, sizeof (EmpathyThemeIrcPriv));
119 }
120
121 static void
122 empathy_theme_irc_init (EmpathyThemeIrc *theme)
123 {
124         EmpathyThemeIrcPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (theme,
125                 EMPATHY_TYPE_THEME_IRC, EmpathyThemeIrcPriv);
126
127         theme->priv = priv;
128
129         theme_irc_create_tags (theme);
130
131         /* Define margin */
132         g_object_set (theme,
133                       "left-margin", 3,
134                       "right-margin", 3,
135                       NULL);
136 }
137
138 EmpathyThemeIrc *
139 empathy_theme_irc_new (void)
140 {
141         return g_object_new (EMPATHY_TYPE_THEME_IRC, NULL);
142 }
143