]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-theme-boxes.c
merge git work
[empathy.git] / libempathy-gtk / empathy-theme-boxes.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 <string.h>
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include <libempathy/empathy-debug.h>
29
30 #include "empathy-ui-utils.h"
31 #include "empathy-main-window.h"
32 #include "empathy-theme-utils.h"
33 #include "empathy-theme-boxes.h"
34
35 #define DEBUG_DOMAIN "FancyTheme"
36
37 #define MARGIN 4
38 #define HEADER_PADDING 2
39
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_THEME_BOXES, EmpathyThemeBoxesPriv))
41
42 typedef struct _EmpathyThemeBoxesPriv EmpathyThemeBoxesPriv;
43
44 struct _EmpathyThemeBoxesPriv {
45         gchar *header_foreground;
46         gchar *header_background;
47         gchar *header_line_background;
48         gchar *text_foreground;
49         gchar *text_background;
50         gchar *action_foreground;
51         gchar *highlight_foreground;
52         gchar *time_foreground;
53         gchar *event_foreground;
54         gchar *invite_foreground;
55         gchar *link_foreground;
56 };
57
58 static void     theme_boxes_finalize          (GObject            *object);
59 static void     theme_boxes_get_property      (GObject            *object,
60                                                guint               param_id,
61                                                GValue             *value,
62                                                GParamSpec         *pspec);
63 static void     theme_boxes_set_property      (GObject            *object,
64                                                guint               param_id,
65                                                const GValue       *value,
66                                                GParamSpec         *pspec);
67 static void     theme_boxes_define_theme_tags (EmpathyTheme        *theme,
68                                                EmpathyChatView     *view);
69 static EmpathyThemeContext *
70 theme_boxes_setup_with_view                   (EmpathyTheme        *theme,
71                                                EmpathyChatView     *view);
72 static void     theme_boxes_detach_from_view  (EmpathyTheme        *theme,
73                                                EmpathyThemeContext *context,
74                                                EmpathyChatView     *view);
75 static void     theme_boxes_view_cleared      (EmpathyTheme        *theme,
76                                                EmpathyThemeContext *context,
77                                                EmpathyChatView     *view);
78
79 static void     theme_boxes_append_message    (EmpathyTheme        *theme,
80                                                EmpathyThemeContext *context,
81                                                EmpathyChatView     *view,
82                                                EmpathyMessage      *message);
83 static void     theme_boxes_append_event      (EmpathyTheme        *theme,
84                                                EmpathyThemeContext *context,
85                                                EmpathyChatView     *view,
86                                                const gchar        *str);
87 static void     theme_boxes_append_timestamp  (EmpathyTheme        *theme,
88                                                EmpathyThemeContext *context,
89                                                EmpathyChatView     *view,
90                                                EmpathyMessage      *message,
91                                                gboolean            show_date,
92                                                gboolean            show_time);
93 static void     theme_boxes_append_spacing    (EmpathyTheme        *theme,
94                                                EmpathyThemeContext *context,
95                                                EmpathyChatView     *view);
96
97 enum {
98         PROP_0,
99         PROP_HEADER_FOREGROUND,
100         PROP_HEADER_BACKGROUND,
101         PROP_HEADER_LINE_BACKGROUND,
102         PROP_TEXT_FOREGROUND,
103         PROP_TEXT_BACKGROUND,
104         PROP_ACTION_FOREGROUND,
105         PROP_HIGHLIGHT_FOREGROUND,
106         PROP_TIME_FOREGROUND,
107         PROP_EVENT_FOREGROUND,
108         PROP_INVITE_FOREGROUND,
109         PROP_LINK_FOREGROUND
110 };
111
112 enum {
113         PROP_FLOP,
114         PROP_MY_PROP
115 };
116
117 G_DEFINE_TYPE (EmpathyThemeBoxes, empathy_theme_boxes, EMPATHY_TYPE_THEME);
118
119 static void
120 empathy_theme_boxes_class_init (EmpathyThemeBoxesClass *class)
121 {
122         GObjectClass     *object_class;
123         EmpathyThemeClass *theme_class;
124
125         object_class = G_OBJECT_CLASS (class);
126         theme_class  = EMPATHY_THEME_CLASS (class);
127
128         object_class->finalize       = theme_boxes_finalize;
129         object_class->get_property   = theme_boxes_get_property;
130         object_class->set_property   = theme_boxes_set_property;
131
132         theme_class->setup_with_view  = theme_boxes_setup_with_view;
133         theme_class->detach_from_view = theme_boxes_detach_from_view;
134         theme_class->view_cleared     = theme_boxes_view_cleared;
135         theme_class->append_message   = theme_boxes_append_message;
136         theme_class->append_event     = theme_boxes_append_event;
137         theme_class->append_timestamp = theme_boxes_append_timestamp;
138         theme_class->append_spacing   = theme_boxes_append_spacing;
139
140         g_object_class_install_property (object_class,
141                                          PROP_HEADER_FOREGROUND,
142                                          g_param_spec_string ("header-foreground",
143                                                               "",
144                                                               "",
145                                                               NULL,
146                                                               G_PARAM_READWRITE));
147
148         g_object_class_install_property (object_class,
149                                          PROP_HEADER_BACKGROUND,
150                                          g_param_spec_string ("header-background",
151                                                               "",
152                                                               "",
153                                                               NULL,
154                                                               G_PARAM_READWRITE));
155
156         g_object_class_install_property (object_class,
157                                          PROP_HEADER_LINE_BACKGROUND,
158                                          g_param_spec_string ("header-line-background",
159                                                               "",
160                                                               "",
161                                                               NULL,
162                                                               G_PARAM_READWRITE));
163
164
165         g_object_class_install_property (object_class,
166                                          PROP_TEXT_FOREGROUND,
167                                          g_param_spec_string ("text-foreground",
168                                                               "",
169                                                               "",
170                                                               NULL,
171                                                               G_PARAM_READWRITE));
172
173         g_object_class_install_property (object_class,
174                                          PROP_TEXT_BACKGROUND,
175                                          g_param_spec_string ("text-background",
176                                                               "",
177                                                               "",
178                                                               NULL,
179                                                               G_PARAM_READWRITE));
180
181         g_object_class_install_property (object_class,
182                                          PROP_ACTION_FOREGROUND,
183                                          g_param_spec_string ("action-foreground",
184                                                               "",
185                                                               "",
186                                                               NULL,
187                                                               G_PARAM_READWRITE));
188
189         g_object_class_install_property (object_class,
190                                          PROP_HIGHLIGHT_FOREGROUND,
191                                          g_param_spec_string ("highlight-foreground",
192                                                               "",
193                                                               "",
194                                                               NULL,
195                                                               G_PARAM_READWRITE));
196
197         g_object_class_install_property (object_class,
198                                          PROP_TIME_FOREGROUND,
199                                          g_param_spec_string ("time-foreground",
200                                                               "",
201                                                               "",
202                                                               NULL,
203                                                               G_PARAM_READWRITE));
204
205         g_object_class_install_property (object_class,
206                                          PROP_EVENT_FOREGROUND,
207                                          g_param_spec_string ("event-foreground",
208                                                               "",
209                                                               "",
210                                                               NULL,
211                                                               G_PARAM_READWRITE));
212
213         g_object_class_install_property (object_class,
214                                          PROP_INVITE_FOREGROUND,
215                                          g_param_spec_string ("invite-foreground",
216                                                               "",
217                                                               "",
218                                                               NULL,
219                                                               G_PARAM_READWRITE));
220
221         g_object_class_install_property (object_class,
222                                          PROP_LINK_FOREGROUND,
223                                          g_param_spec_string ("link-foreground",
224                                                               "",
225                                                               "",
226                                                               NULL,
227                                                               G_PARAM_READWRITE));
228
229         g_type_class_add_private (object_class, sizeof (EmpathyThemeBoxesPriv));
230 }
231
232 static void
233 empathy_theme_boxes_init (EmpathyThemeBoxes *theme)
234 {
235         EmpathyThemeBoxesPriv *priv;
236
237         priv = GET_PRIV (theme);
238 }
239
240 static void
241 theme_boxes_finalize (GObject *object)
242 {
243         EmpathyThemeBoxesPriv *priv;
244
245         priv = GET_PRIV (object);
246
247         g_free (priv->header_foreground);
248         g_free (priv->header_background);
249         g_free (priv->header_line_background);
250         g_free (priv->text_foreground);
251         g_free (priv->text_background);
252         g_free (priv->action_foreground);
253         g_free (priv->highlight_foreground);
254         g_free (priv->time_foreground);
255         g_free (priv->event_foreground);
256         g_free (priv->invite_foreground);
257         g_free (priv->link_foreground);
258         
259         (G_OBJECT_CLASS (empathy_theme_boxes_parent_class)->finalize) (object);
260 }
261
262 static void
263 theme_boxes_get_property (GObject    *object,
264                           guint       param_id,
265                           GValue     *value,
266                           GParamSpec *pspec)
267 {
268         EmpathyThemeBoxesPriv *priv;
269
270         priv = GET_PRIV (object);
271
272         switch (param_id) {
273         case PROP_HEADER_FOREGROUND:
274                 g_value_set_string (value, priv->header_foreground);
275                 break;
276         case PROP_HEADER_BACKGROUND:
277                 g_value_set_string (value, priv->header_background);
278                 break;
279         case PROP_HEADER_LINE_BACKGROUND:
280                 g_value_set_string (value, priv->header_line_background);
281                 break;
282         case PROP_TEXT_FOREGROUND:
283                 g_value_set_string (value, priv->text_foreground);
284                 break;
285         case PROP_TEXT_BACKGROUND:
286                 g_value_set_string (value, priv->text_background);
287                 break;
288         case PROP_ACTION_FOREGROUND:
289                 g_value_set_string (value, priv->action_foreground);
290                 break;
291         case PROP_HIGHLIGHT_FOREGROUND:
292                 g_value_set_string (value, priv->highlight_foreground);
293                 break;
294         case PROP_TIME_FOREGROUND:
295                 g_value_set_string (value, priv->time_foreground);
296                 break;
297         case PROP_EVENT_FOREGROUND:
298                 g_value_set_string (value, priv->event_foreground);
299                 break;
300         case PROP_INVITE_FOREGROUND:
301                 g_value_set_string (value, priv->invite_foreground);
302                 break;
303         case PROP_LINK_FOREGROUND:
304                 g_value_set_string (value, priv->link_foreground);
305                 break;
306         default:
307                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
308                 break;
309         }
310 }
311 static void
312 theme_boxes_set_property (GObject      *object,
313                     guint         param_id,
314                     const GValue *value,
315                     GParamSpec   *pspec)
316 {
317         EmpathyThemeBoxesPriv *priv;
318
319         priv = GET_PRIV (object);
320
321         switch (param_id) {
322         case PROP_HEADER_FOREGROUND:
323                 g_free (priv->header_foreground);
324                 priv->header_foreground = g_value_dup_string (value);
325                 break;
326         case PROP_HEADER_BACKGROUND:
327                 g_free (priv->header_background);
328                 priv->header_background = g_value_dup_string (value);
329                 break;
330         case PROP_HEADER_LINE_BACKGROUND:
331                 g_free (priv->header_line_background);
332                 priv->header_line_background = g_value_dup_string (value);
333                 break;
334         case PROP_TEXT_FOREGROUND:
335                 g_free (priv->text_foreground);
336                 priv->text_foreground = g_value_dup_string (value);
337                 break;
338         case PROP_TEXT_BACKGROUND:
339                 g_free (priv->text_background);
340                 priv->text_background = g_value_dup_string (value);
341                 break;
342         case PROP_ACTION_FOREGROUND:
343                 g_free (priv->action_foreground);
344                 priv->action_foreground = g_value_dup_string (value);
345                 break;
346         case PROP_HIGHLIGHT_FOREGROUND:
347                 g_free (priv->highlight_foreground);
348                 priv->highlight_foreground = g_value_dup_string (value);
349                 break;
350         case PROP_TIME_FOREGROUND:
351                 g_free (priv->time_foreground);
352                 priv->time_foreground = g_value_dup_string (value);
353                 break;
354         case PROP_EVENT_FOREGROUND:
355                 g_free (priv->event_foreground);
356                 priv->event_foreground = g_value_dup_string (value);
357                 break;
358         case PROP_INVITE_FOREGROUND:
359                 g_free (priv->invite_foreground);
360                 priv->invite_foreground = g_value_dup_string (value);
361                 break;
362         case PROP_LINK_FOREGROUND:
363                 g_free (priv->link_foreground);
364                 priv->link_foreground = g_value_dup_string (value);
365                 break;
366         default:
367                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
368                 break;
369         }
370 }
371
372 static void
373 theme_boxes_define_theme_tags (EmpathyTheme *theme, EmpathyChatView *view)
374 {
375         EmpathyThemeBoxesPriv *priv;
376         GtkTextBuffer   *buffer;
377         GtkTextTagTable *table;
378         GtkTextTag      *tag;
379
380         priv = GET_PRIV (theme);
381
382         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
383         table = gtk_text_buffer_get_tag_table (buffer);
384
385         tag = empathy_theme_utils_init_tag_by_name (table, "fancy-spacing");
386         g_object_set (tag,
387                       "size", 3000,
388                       "pixels-above-lines", 8,
389                       NULL);
390         empathy_theme_utils_add_tag (table, tag);
391
392         tag = empathy_theme_utils_init_tag_by_name (table, 
393                                                    "fancy-header");
394         g_object_set (tag,
395                       "weight", PANGO_WEIGHT_BOLD,
396                       "pixels-above-lines", HEADER_PADDING,
397                       "pixels-below-lines", HEADER_PADDING,
398                       NULL);
399         if (priv->header_foreground) {
400                 g_object_set (tag,
401                               "foreground", priv->header_foreground,
402                               "paragraph-background", priv->header_background,
403                               NULL);
404         }
405         empathy_theme_utils_add_tag (table, tag);
406
407         tag = empathy_theme_utils_init_tag_by_name (table, "fancy-header-line");
408         g_object_set (tag,
409                       "size", 1,
410                       NULL);
411         if (priv->header_line_background) {
412                 g_object_set (tag,
413                               "paragraph-background", priv->header_line_background,
414                               NULL);
415         }
416
417         empathy_theme_utils_add_tag (table, tag);
418
419         tag = empathy_theme_utils_init_tag_by_name (table, "fancy-body");
420         g_object_set (tag,
421                       "pixels-above-lines", 4,
422                       NULL);
423         if (priv->text_background) {
424                 g_object_set (tag,
425                               "paragraph-background", priv->text_background,
426                               NULL);
427         }
428
429         if (priv->text_foreground) {
430                 g_object_set (tag,
431                               "foreground", priv->text_foreground,
432                               NULL);
433         }
434         empathy_theme_utils_add_tag (table, tag);
435
436         tag = empathy_theme_utils_init_tag_by_name (table, "fancy-action");
437         g_object_set (tag,
438                       "style", PANGO_STYLE_ITALIC,
439                       "pixels-above-lines", 4,
440                       NULL);
441
442         if (priv->text_background) {
443                 g_object_set (tag,
444                               "paragraph-background", priv->text_background,
445                               NULL);
446         }
447
448         if (priv->action_foreground) {
449                 g_object_set (tag,
450                               "foreground", priv->action_foreground,
451                               NULL);
452         } 
453
454         empathy_theme_utils_add_tag (table, tag);
455
456         tag = empathy_theme_utils_init_tag_by_name (table,
457                                                    "fancy-highlight");
458         g_object_set (tag,
459                       "weight", PANGO_WEIGHT_BOLD,
460                       "pixels-above-lines", 4,
461                       NULL);
462         if (priv->text_background) {
463                 g_object_set (tag,
464                               "paragraph-background", priv->text_background,
465                               NULL);
466         }
467
468
469         if (priv->highlight_foreground) {
470                 g_object_set (tag,
471                               "foreground", priv->highlight_foreground,
472                               NULL);
473         }
474         empathy_theme_utils_add_tag (table, tag);
475
476         tag = empathy_theme_utils_init_tag_by_name (table, "fancy-time");
477         g_object_set (tag,
478                       "justification", GTK_JUSTIFY_CENTER,
479                       NULL);
480         if (priv->time_foreground) {
481                 g_object_set (tag,
482                               "foreground", priv->time_foreground,
483                               NULL);
484         }
485         empathy_theme_utils_add_tag (table, tag);
486
487         tag = empathy_theme_utils_init_tag_by_name (table, "fancy-event");
488         g_object_set (tag,
489                       "justification", GTK_JUSTIFY_LEFT,
490                       NULL);
491         if (priv->event_foreground) {
492                 g_object_set (tag,
493                               "foreground", priv->event_foreground,
494                               NULL);
495         }
496         empathy_theme_utils_add_tag (table, tag);
497
498         tag = empathy_theme_utils_init_tag_by_name (table, "invite");
499         if (priv->invite_foreground) {
500                 g_object_set (tag,
501                               "foreground", priv->invite_foreground,
502                               NULL);
503         }
504         empathy_theme_utils_add_tag (table, tag);
505
506         tag = empathy_theme_utils_init_tag_by_name (table, "fancy-link");
507         g_object_set (tag,
508                       "underline", PANGO_UNDERLINE_SINGLE,
509                       NULL);
510         if (priv->link_foreground) {
511                 g_object_set (tag,
512                               "foreground", priv->link_foreground,
513                               NULL);
514         } 
515         empathy_theme_utils_add_tag (table, tag);
516 }
517
518 static void
519 theme_boxes_fixup_tag_table (EmpathyTheme *theme, EmpathyChatView *view)
520 {
521         GtkTextBuffer *buffer;
522
523         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
524
525         /* "Fancy" style tags. */
526         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-header");
527         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-header-line");
528         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-body");
529         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-action");
530         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-highlight");
531         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-spacing");
532         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-time");
533         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-event");
534         empathy_theme_utils_ensure_tag_by_name (buffer, "fancy-link");
535 }
536
537 typedef struct {
538         BlockType last_block_type;
539         time_t    last_timestamp;
540 } FancyContext;
541
542 static EmpathyThemeContext *
543 theme_boxes_setup_with_view (EmpathyTheme *theme, EmpathyChatView *view)
544 {
545         EmpathyThemeBoxesPriv *priv;
546
547         g_return_val_if_fail (EMPATHY_IS_THEME_BOXES (theme), NULL);
548
549         priv = GET_PRIV (theme);
550
551         theme_boxes_fixup_tag_table (theme, view);
552
553         theme_boxes_define_theme_tags (theme, view);
554         
555         empathy_chat_view_set_margin (view, MARGIN);
556
557         return NULL;
558 }
559
560 static void
561 theme_boxes_detach_from_view (EmpathyTheme        *theme,
562                               EmpathyThemeContext *context,
563                               EmpathyChatView     *view)
564 {
565         /* FIXME: Free the context */
566 }
567
568 static void 
569 theme_boxes_view_cleared (EmpathyTheme        *theme,
570                           EmpathyThemeContext *context,
571                           EmpathyChatView     *view)
572 {
573         /* FIXME: clear the context data */
574 }
575
576 static void
577 table_size_allocate_cb (GtkWidget     *view,
578                         GtkAllocation *allocation,
579                         GtkWidget     *box)
580 {
581         gint width, height;
582
583         gtk_widget_get_size_request (box, NULL, &height);
584
585         width = allocation->width;
586         
587         width -= \
588                 gtk_text_view_get_right_margin (GTK_TEXT_VIEW (view)) - \
589                 gtk_text_view_get_left_margin (GTK_TEXT_VIEW (view));
590         width -= 2 * MARGIN;
591         width -= 2 * HEADER_PADDING;
592
593         gtk_widget_set_size_request (box, width, height);
594 }
595
596 static void
597 theme_boxes_maybe_append_header (EmpathyTheme        *theme,
598                                  EmpathyThemeContext *context,
599                                  EmpathyChatView     *view,
600                                  EmpathyMessage      *msg)
601 {
602         EmpathyThemeBoxesPriv *priv;
603         EmpathyContact        *contact;
604         GdkPixbuf            *avatar = NULL;
605         GtkTextBuffer        *buffer;
606         const gchar          *name;
607         gboolean              header;
608         GtkTextIter           iter;
609         GtkWidget            *label1, *label2;
610         GtkTextChildAnchor   *anchor;
611         GtkWidget            *box;
612         gchar                *str;
613         time_t                time;
614         gchar                *tmp;
615         GtkTextIter           start;
616         GdkColor              color;
617         gboolean              parse_success;
618         gboolean              from_self;
619
620         priv = GET_PRIV (theme);
621
622         contact = empathy_message_get_sender (msg);
623         from_self = empathy_contact_is_user (contact);
624         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
625
626         empathy_debug (DEBUG_DOMAIN, "Maybe add fancy header");
627
628         name = empathy_contact_get_name (contact);
629
630         header = FALSE;
631
632         /* Only insert a header if the previously inserted block is not the same
633          * as this one. This catches all the different cases:
634          */
635         if (empathy_chat_view_get_last_block_type (view) != BLOCK_TYPE_SELF &&
636             empathy_chat_view_get_last_block_type (view) != BLOCK_TYPE_OTHER) {
637                 header = TRUE;
638         }
639         else if (from_self &&
640                  empathy_chat_view_get_last_block_type (view) == BLOCK_TYPE_OTHER) {
641                 header = TRUE;
642         }
643         else if (!from_self && 
644                  empathy_chat_view_get_last_block_type (view) == BLOCK_TYPE_SELF) {
645                 header = TRUE;
646         }
647         else if (!from_self &&
648                  (!empathy_chat_view_get_last_contact (view) ||
649                   !empathy_contact_equal (contact, empathy_chat_view_get_last_contact (view)))) {
650                 header = TRUE;
651         }
652
653         if (!header) {
654                 return;
655         }
656
657         empathy_theme_append_spacing (theme, context, view);
658
659         gtk_text_buffer_get_end_iter (buffer, &iter);
660         gtk_text_buffer_insert_with_tags_by_name (buffer,
661                                                   &iter,
662                                                   "\n",
663                                                   -1,
664                                                   "fancy-header-line",
665                                                   NULL);
666
667         gtk_text_buffer_get_end_iter (buffer, &iter);
668         anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
669
670         box = gtk_hbox_new (FALSE, 0);
671
672
673         avatar = empathy_chat_view_get_avatar_pixbuf_with_cache (contact);
674         if (avatar && empathy_theme_get_show_avatars (theme)) {
675                 GtkWidget *image;
676
677                 image = gtk_image_new_from_pixbuf (avatar);
678
679                 gtk_box_pack_start (GTK_BOX (box), image,
680                                     FALSE, TRUE, 2);
681
682         }
683
684         g_signal_connect_object (view, "size-allocate",
685                                  G_CALLBACK (table_size_allocate_cb),
686                                  box, 0);
687
688         str = g_strdup_printf ("<b>%s</b>", name);
689
690         label1 = g_object_new (GTK_TYPE_LABEL,
691                                "label", str,
692                                "use-markup", TRUE,
693                                "xalign", 0.0,
694                                NULL);
695
696         parse_success = gdk_color_parse (priv->header_foreground, &color);
697
698         if (parse_success) {
699                 gtk_widget_modify_fg (label1, GTK_STATE_NORMAL, &color);
700         }
701
702         g_free (str);
703
704         time = empathy_message_get_timestamp (msg);
705
706         tmp = empathy_time_to_string_local (time, 
707                                            EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
708         str = g_strdup_printf ("<i>%s</i>", tmp);
709         g_free (tmp);
710
711         label2 = g_object_new (GTK_TYPE_LABEL,
712                                "label", str,
713                                "use-markup", TRUE,
714                                "xalign", 1.0,
715                                NULL);
716         
717         if (parse_success) {
718                 gtk_widget_modify_fg (label2, GTK_STATE_NORMAL, &color);
719         }
720
721         g_free (str);
722
723         gtk_misc_set_alignment (GTK_MISC (label1), 0.0, 0.5);
724         gtk_misc_set_alignment (GTK_MISC (label2), 1.0, 0.5);
725
726         gtk_box_pack_start (GTK_BOX (box), label1, TRUE, TRUE, 0);
727         gtk_box_pack_start (GTK_BOX (box), label2, TRUE, TRUE, 0);
728
729         gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view),
730                                            box,
731                                            anchor);
732
733         gtk_widget_show_all (box);
734
735         gtk_text_buffer_get_end_iter (buffer, &iter);
736         start = iter;
737         gtk_text_iter_backward_char (&start);
738         gtk_text_buffer_apply_tag_by_name (buffer,
739                                            "fancy-header",
740                                            &start, &iter);
741
742         gtk_text_buffer_insert_with_tags_by_name (buffer,
743                                                   &iter,
744                                                   "\n",
745                                                   -1,
746                                                   "fancy-header",
747                                                   NULL);
748
749         gtk_text_buffer_get_end_iter (buffer, &iter);
750         gtk_text_buffer_insert_with_tags_by_name (buffer,
751                                                   &iter,
752                                                   "\n",
753                                                   -1,
754                                                   "fancy-header-line",
755                                                   NULL);
756 }
757
758 static void
759 theme_boxes_append_message (EmpathyTheme        *theme,
760                             EmpathyThemeContext *context,
761                             EmpathyChatView     *view,
762                             EmpathyMessage      *message)
763 {
764         EmpathyContact *sender;
765
766         empathy_theme_maybe_append_date_and_time (theme, context, view, message);
767         theme_boxes_maybe_append_header (theme, context, view, message);
768
769         sender = empathy_message_get_sender (message);
770
771         if (empathy_message_get_type (message) == EMPATHY_MESSAGE_TYPE_ACTION) {
772                 gchar *body;
773
774                 body = g_strdup_printf (" * %s %s", 
775                                         empathy_contact_get_name (sender),
776                                         empathy_message_get_body (message));
777                 empathy_theme_append_text (theme, context, view, body,
778                                            "fancy-action", "fancy-link");
779         } else {
780                 empathy_theme_append_text (theme, context, view,
781                                            empathy_message_get_body (message),
782                                            "fancy-body", "fancy-link");
783         }
784         
785         if (empathy_contact_is_user (sender)) {
786                 empathy_chat_view_set_last_block_type (view, BLOCK_TYPE_SELF);
787                 empathy_chat_view_set_last_contact (view, NULL);
788         } else {
789                 empathy_chat_view_set_last_block_type (view, BLOCK_TYPE_OTHER);
790                 empathy_chat_view_set_last_contact (view, sender);
791         }
792 }
793
794 static void
795 theme_boxes_append_event (EmpathyTheme        *theme,
796                           EmpathyThemeContext *context,
797                           EmpathyChatView     *view,
798                           const gchar        *str)
799 {
800         GtkTextBuffer *buffer;
801         GtkTextIter    iter;
802         gchar         *msg;
803
804         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
805
806         empathy_theme_maybe_append_date_and_time (theme, context, view, NULL);
807
808         gtk_text_buffer_get_end_iter (buffer, &iter);
809
810         msg = g_strdup_printf (" - %s\n", str);
811
812         gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
813                                                   msg, -1,
814                                                   "fancy-event",
815                                                   NULL);
816         g_free (msg);
817
818         empathy_chat_view_set_last_block_type (view, BLOCK_TYPE_EVENT);
819 }
820
821 static void
822 theme_boxes_append_timestamp (EmpathyTheme        *theme,
823                               EmpathyThemeContext *context,
824                               EmpathyChatView     *view,
825                               EmpathyMessage      *message,
826                               gboolean            show_date,
827                               gboolean            show_time)
828 {
829         GtkTextBuffer *buffer;
830         time_t         timestamp;
831         GDate         *date;
832         GtkTextIter    iter;
833         GString       *str;
834         
835         if (!show_date) {
836                 return;
837         }
838
839         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
840
841         date = empathy_message_get_date_and_time (message, &timestamp);
842
843         str = g_string_new (NULL);
844
845         if (show_time || show_date) {
846                 empathy_theme_append_spacing (theme, 
847                                              context,
848                                              view);
849
850                 g_string_append (str, "- ");
851         }
852
853         if (show_date) {
854                 gchar buf[256];
855
856                 g_date_strftime (buf, 256, _("%A %d %B %Y"), date);
857                 g_string_append (str, buf);
858
859                 if (show_time) {
860                         g_string_append (str, ", ");
861                 }
862         }
863
864         g_date_free (date);
865
866         if (show_time) {
867                 gchar *tmp;
868
869                 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
870                 g_string_append (str, tmp);
871                 g_free (tmp);
872         }
873
874         if (show_time || show_date) {
875                 g_string_append (str, " -\n");
876
877                 gtk_text_buffer_get_end_iter (buffer, &iter);
878                 gtk_text_buffer_insert_with_tags_by_name (buffer,
879                                                           &iter,
880                                                           str->str, -1,
881                                                           "fancy-time",
882                                                           NULL);
883
884                 empathy_chat_view_set_last_block_type (view, BLOCK_TYPE_TIME);
885                 empathy_chat_view_set_last_timestamp (view, timestamp);
886         }
887
888         g_string_free (str, TRUE);
889         
890 }
891
892 static void
893 theme_boxes_append_spacing (EmpathyTheme        *theme,
894                             EmpathyThemeContext *context,
895                             EmpathyChatView     *view)
896 {
897         GtkTextBuffer *buffer;
898         GtkTextIter    iter;
899
900         g_return_if_fail (EMPATHY_IS_THEME (theme));
901         g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
902
903         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
904
905         gtk_text_buffer_get_end_iter (buffer, &iter);
906         gtk_text_buffer_insert_with_tags_by_name (buffer,
907                                                   &iter,
908                                                   "\n",
909                                                   -1,
910                                                   "cut",
911                                                   "fancy-spacing",
912                                                   NULL);
913 }
914
915 static void
916 theme_boxes_setup_clean (EmpathyTheme *theme)
917 {
918         g_object_set (theme,
919                       "header-foreground", "black",
920                       "header-background", "#efefdf",
921                       "header_line_background", "#e3e3d3",
922                       "action_foreground", "brown4",
923                       "time_foreground", "darkgrey",
924                       "event_foreground", "darkgrey",
925                       "invite_foreground", "sienna",
926                       "link_foreground","#49789e",
927                       NULL);
928 }
929
930 static void
931 theme_boxes_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
932 {
933         g_snprintf (str_color, 10, 
934                     "#%02x%02x%02x", 
935                     gdk_color->red >> 8, 
936                     gdk_color->green >> 8, 
937                     gdk_color->blue >> 8);
938 }
939
940 static void
941 theme_boxes_setup_themed (EmpathyTheme *theme)
942 {
943         EmpathyThemeBoxesPriv *priv;
944         GtkWidget            *widget;
945         GtkStyle             *style;
946         gchar                 color[10];
947
948         priv = GET_PRIV (theme);
949
950         widget = gtk_entry_new ();
951         style = gtk_widget_get_style (widget);
952         gtk_widget_destroy (widget);
953
954         theme_boxes_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color);
955
956         g_object_set (theme,
957                       "action-foreground", color,
958                       "link-foreground", color,
959                       NULL);
960
961         theme_boxes_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color);
962
963         g_object_set (theme,
964                       "header-background", color,
965                       NULL);
966
967         theme_boxes_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color);
968
969         g_object_set (theme,
970                       "header_line-background", color,
971                       NULL);
972
973         theme_boxes_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color);
974
975         g_object_set (theme,
976                       "header-foreground", color,
977                       NULL);
978 }
979
980 static void
981 theme_boxes_theme_changed_cb (GtkWidget *widget,
982                               GtkStyle  *previous_style,
983                               gpointer   user_data)
984 {
985         theme_boxes_setup_themed (EMPATHY_THEME (user_data));
986
987         g_signal_emit_by_name (G_OBJECT (user_data), "updated");
988 }
989
990 static void
991 theme_boxes_setup_blue (EmpathyTheme *theme)
992 {
993         g_object_set (theme,
994                       "header_foreground", "black",
995                       "header_background", "#88a2b4",
996                       "header_line_background", "#7f96a4",
997                       "text_foreground", "black",
998                       "text_background", "#adbdc8",
999                       "highlight_foreground", "black",
1000                       "action_foreground", "brown4",
1001                       "time_foreground", "darkgrey",
1002                       "event_foreground", "#7f96a4",
1003                       "invite_foreground", "sienna",
1004                       "link_foreground", "#49789e",
1005                       NULL);
1006 }
1007
1008 EmpathyTheme *
1009 empathy_theme_boxes_new (const gchar *name)
1010 {
1011         EmpathyTheme          *theme;
1012         EmpathyThemeBoxesPriv *priv;
1013
1014         theme = g_object_new (EMPATHY_TYPE_THEME_BOXES, NULL);
1015         priv  = GET_PRIV (theme);
1016
1017         if (strcmp (name, "clean") == 0) {
1018                 theme_boxes_setup_clean (theme);
1019         }
1020         else if (strcmp (name, "simple") == 0) {
1021                 /* FIXME: Make an actual accessor function */
1022                 g_signal_connect (empathy_main_window_show (),
1023                                   "style-set",
1024                                   G_CALLBACK (theme_boxes_theme_changed_cb),
1025                                   theme);
1026
1027                 theme_boxes_setup_themed (theme);
1028         }
1029         else if (strcmp (name, "blue") == 0) {
1030                 theme_boxes_setup_blue (theme);
1031         }
1032
1033         return theme;
1034 }
1035
1036