Render message text to an offscreen cairo surface only once to keep low CPU load...
[conv-inbox] / src / el-home-applet.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright (C) 2009 Artem Garmash. All rights reserved.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (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
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Contact: Artem Garmash <artemgarmash@gmail.com>
20  *
21  */
22
23 #include "config.h"
24 #include "el-home-applet.h"
25
26 #include <hildon/hildon.h>
27 #include <rtcom-eventlogger/eventlogger.h>
28 #include <sqlite3.h>
29 #include <string.h>
30
31 #define EL_HOME_APPLET_GET_PRIVATE(obj) ( \
32         G_TYPE_INSTANCE_GET_PRIVATE (obj, \
33                 EL_TYPE_HOME_APPLET, ELHomeAppletPrivate))
34
35 #define BOX_WIDTH 352
36 #define BOX_HEIGHT 266
37
38 #define C_WIDTH (BOX_WIDTH - 2*HILDON_MARGIN_HALF)
39 #define C_HEIGHT (BOX_HEIGHT - 2*HILDON_MARGIN_HALF)
40 #define C_X HILDON_MARGIN_HALF
41 #define C_Y HILDON_MARGIN_HALF
42
43 #define HEADER_HEIGHT 48
44 #define MESSAGE_HEIGHT (C_HEIGHT - HEADER_HEIGHT)
45 #define MESSAGE_WIDTH (C_WIDTH - 2*HILDON_MARGIN_DEFAULT)
46
47 #define BOX_RADIOUS 10
48
49 #define SCROLL_PERIOD 100 /* ms */
50 #define SCROLL_STEP 1 /* pixel */
51
52 struct _ELHomeAppletPrivate
53 {
54         RTComEl *eventlogger;
55
56         GtkWidget *sender;
57         GtkWidget *icon;
58         GtkWidget *unread;
59         GtkWidget *received;
60         GtkWidget *empty;
61
62         gchar *message;
63         gint event_id;
64
65         gboolean active;
66
67         guint unread_count;
68
69         struct {
70                 float red;
71                 float green;
72                 float blue;
73         } active_color;
74         PangoFontDescription *font_desc;
75
76         guint idle_id;
77
78         cairo_surface_t *message_surface;
79
80         gboolean scroll_on_click;
81         gint scroll_offset;
82         gint hidden_message_height;
83         guint scroll_anim_id;
84 };
85
86 HD_DEFINE_PLUGIN_MODULE (ELHomeApplet, el_home_applet, HD_TYPE_HOME_PLUGIN_ITEM);
87
88 const gchar* g_module_check_init (GModule *module);
89 const gchar*
90 g_module_check_init (GModule *module)
91 {
92         g_module_make_resident (module);
93         return NULL;
94 }
95
96 static void
97 el_home_applet_class_finalize (ELHomeAppletClass *klass)
98 {
99 }
100
101 static void
102 el_home_applet_realize (GtkWidget *widget)
103 {
104         GdkScreen *screen;
105
106         screen = gtk_widget_get_screen (widget);
107         gtk_widget_set_colormap (widget,
108                                  gdk_screen_get_rgba_colormap (screen));
109
110         gtk_widget_set_app_paintable (widget,
111                                       TRUE);
112
113         GTK_WIDGET_CLASS (el_home_applet_parent_class)->realize (widget);
114 }
115
116 /*
117  * Thanks http://cairographics.org/cookbook/roundedrectangles/
118  */
119 static void
120 rounded_rectangle (cairo_t *cr,
121                    double   x,
122                    double   y,
123                    double   w,
124                    double   h,
125                    double   r)
126 {
127         cairo_move_to (cr, x + r, y);
128         cairo_line_to (cr, x + w - r, y);
129         cairo_curve_to (cr, x + w, y,
130                         x + w, y,
131                         x + w, y + r);
132         cairo_line_to (cr, x + w, y + h - r);
133         cairo_curve_to (cr, x + w, y + h,
134                         x + w, y + h,
135                         x + w - r, y + h);
136         cairo_line_to (cr, x + r, y + h);
137         cairo_curve_to (cr, x, y + h,
138                         x, y + h,
139                         x, y + h - r);
140         cairo_line_to (cr, x, y + r);
141         cairo_curve_to (cr, x, y,
142                         x, y,
143                         x + r, y);
144 }
145
146 static cairo_surface_t*
147 draw_text (cairo_t              *cr,
148            PangoFontDescription *desc,
149            const gchar          *text,
150            gint                  width,
151            gint                 *height)
152 {
153         PangoLayout *layout;
154         PangoRectangle extent;
155
156         cairo_surface_t *gdk_surface, *result_surface;
157         cairo_t *msg_cr;
158
159         /* Create a PangoLayout, set the font and text */
160         layout = pango_cairo_create_layout (cr);
161         pango_layout_set_text (layout,
162                                text,
163                                -1);
164         pango_layout_set_font_description (layout, desc);
165
166         pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
167         pango_layout_set_width (layout, PANGO_SCALE*width);
168
169         pango_layout_get_pixel_extents (layout, NULL, &extent);
170         *height = extent.height;
171
172         gdk_surface = cairo_get_target (cr);
173         result_surface = cairo_surface_create_similar
174                 (gdk_surface,
175                  CAIRO_CONTENT_COLOR_ALPHA,
176                  width,
177                  extent.height);
178         msg_cr = cairo_create (result_surface);
179
180         pango_cairo_update_layout (msg_cr, layout);
181         /* draw shadow */
182         cairo_move_to (msg_cr, 1, 1);
183         cairo_set_source_rgba (msg_cr, 0.2, 0.2, 0.2, 0.8);
184         pango_cairo_show_layout (msg_cr, layout);
185
186         /* draw fg */
187         cairo_move_to (msg_cr, 0, 0);
188         cairo_set_source_rgba (msg_cr, 1.0, 1.0, 1.0, 1.0);
189         pango_cairo_show_layout (msg_cr, layout);
190
191         cairo_destroy (msg_cr);
192         g_object_unref (layout);
193
194         return result_surface;
195 }
196
197 static void
198 stop_scroll_anim (ELHomeAppletPrivate *priv)
199 {
200         if (priv->scroll_anim_id > 0) {
201                 g_source_remove (priv->scroll_anim_id);
202                 priv->scroll_anim_id = 0;
203                 priv->scroll_on_click = FALSE;
204         }
205 }
206
207 static void
208 style_set_cb (GtkWidget *widget,
209               GtkStyle  *previous_style,
210               ELHomeApplet *self)
211 {
212         ELHomeAppletPrivate *priv = self->priv;
213         GdkColor color;
214         GtkStyle *font_style;
215
216         font_style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
217                                                 "SystemFont",
218                                                 NULL,
219                                                 G_TYPE_NONE);
220         if (font_style && font_style->font_desc) {
221                 if (priv->font_desc)
222                         pango_font_description_free (priv->font_desc);
223                 priv->font_desc = pango_font_description_copy (font_style->font_desc);
224         }
225
226         if (gtk_style_lookup_color (widget->style,
227                                     "ActiveTextColor",
228                                     &color)) {
229                 priv->active_color.red = color.red/(float)G_MAXUINT16;
230                 priv->active_color.green = color.green/(float)G_MAXUINT16;
231                 priv->active_color.blue = color.blue/(float)G_MAXUINT16;
232         }
233 }
234
235 static void
236 notify_on_current_desktop (GObject      *object,
237                            GParamSpec   *unused G_GNUC_UNUSED,
238                            ELHomeApplet *self)
239 {
240         ELHomeAppletPrivate *priv = self->priv;
241         gboolean on;
242
243         g_object_get (object, "is-on-current-desktop", &on, NULL);
244         if (!on) {
245                 stop_scroll_anim (self->priv);
246                 priv->scroll_on_click = priv->scroll_offset;
247                 priv->scroll_offset = 0;
248                 gtk_widget_queue_draw (GTK_WIDGET (self));
249         }
250 }
251
252 static gboolean
253 expose_event (GtkWidget *self, GdkEventExpose *event)
254 {
255         ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
256         cairo_t *cr;
257         int message_height;
258
259         message_height = C_HEIGHT - priv->received->allocation.height - HEADER_HEIGHT;
260
261         cr = gdk_cairo_create (self->window);
262         gdk_cairo_region (cr, event->region);
263         cairo_clip (cr);
264
265         /* draw bound box */
266         cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
267
268         cairo_set_source_rgba (cr, 0.4f, 0.4f, 0.4f, 0.1f);
269         cairo_set_line_width (cr, 3.0f);
270
271         rounded_rectangle (cr,
272                            C_X,
273                            C_Y,
274                            BOX_WIDTH - 2*C_X,
275                            BOX_HEIGHT - 2*C_Y,
276                            BOX_RADIOUS);
277
278         cairo_close_path (cr);
279         cairo_stroke (cr);
280
281         /* draw header */
282         cairo_set_line_width (cr, 1.0f);
283
284         cairo_translate (cr, C_X, C_Y);
285         cairo_move_to (cr, 0, HEADER_HEIGHT);
286         cairo_line_to (cr, 0, BOX_RADIOUS);
287         cairo_curve_to (cr, 0, 0, 0, 0, BOX_RADIOUS, 0);
288         cairo_line_to (cr, C_WIDTH - BOX_RADIOUS, 0);
289         cairo_curve_to (cr, C_WIDTH, 0, C_WIDTH, 0, C_WIDTH, BOX_RADIOUS);
290         cairo_line_to (cr, C_WIDTH, HEADER_HEIGHT);
291         cairo_line_to (cr, 0, HEADER_HEIGHT);
292
293         cairo_close_path (cr);
294
295         cairo_set_source_rgba (cr, 0.2f, 0.2f, 0.2f, 0.8f);
296         cairo_fill_preserve (cr);
297         cairo_set_source_rgba (cr,
298                                priv->active_color.red,
299                                priv->active_color.green,
300                                priv->active_color.blue,
301                                1.0f);
302         cairo_stroke (cr);
303
304         /* draw body */
305         cairo_move_to (cr, 0, HEADER_HEIGHT);
306         cairo_line_to (cr, 0, C_HEIGHT - BOX_RADIOUS);
307         cairo_curve_to (cr, 0, C_HEIGHT, 0, C_HEIGHT, BOX_RADIOUS, C_HEIGHT);
308         cairo_line_to (cr, C_WIDTH - BOX_RADIOUS, C_HEIGHT);
309         cairo_curve_to (cr, C_WIDTH, C_HEIGHT, C_WIDTH, C_HEIGHT, C_WIDTH, C_HEIGHT - BOX_RADIOUS);
310         cairo_line_to (cr, C_WIDTH, HEADER_HEIGHT);
311         cairo_line_to (cr, 0, HEADER_HEIGHT);
312         cairo_close_path (cr);
313
314         /* draw body filling depending on (in)active state */
315         cairo_pattern_t *grad;
316         grad = cairo_pattern_create_linear (0, HEADER_HEIGHT,
317                                             0, C_HEIGHT);
318
319         if (priv->active) {
320                 cairo_pattern_add_color_stop_rgba (grad,
321                                                    0.5f,
322                                                    priv->active_color.red,
323                                                    priv->active_color.green,
324                                                    priv->active_color.blue,
325                                                    0.8f);
326                 cairo_pattern_add_color_stop_rgba (grad,
327                                                    1.0f,
328                                                    priv->active_color.red/2,
329                                                    priv->active_color.green/2,
330                                                    priv->active_color.blue/2,
331                                                    0.8f);
332         }
333         else {
334                 cairo_pattern_add_color_stop_rgba (grad, 0.5f,
335                                                    0.4f, 0.4f, 0.4f, 0.8f);
336                 cairo_pattern_add_color_stop_rgba (grad, 1.0f,
337                                                    0.2f, 0.2f, 0.2f, 0.8f);
338         }
339         cairo_set_source (cr, grad);
340         cairo_fill (cr);
341
342         /* cairo_set_source_rgba (cr, red, green, blue, 1.0f); */
343         /* cairo_translate (cr, -C_X, -C_Y); */
344         /* rounded_rectangle (cr, */
345         /*                    C_X, */
346         /*                    C_Y, */
347         /*                    BOX_WIDTH - 2*C_X, */
348         /*                    BOX_HEIGHT - 2*C_Y, */
349         /*                    BOX_RADIOUS); */
350         /* cairo_close_path (cr); */
351         /* cairo_stroke (cr); */
352
353         /* draw message */
354         if (!priv->message_surface) {
355                 gint height;
356
357                 priv->message_surface = draw_text (cr,
358                                                    priv->font_desc,
359                                                    priv->message,
360                                                    MESSAGE_WIDTH,
361                                                    &height);
362
363                 priv->hidden_message_height = height - message_height;
364                 priv->scroll_on_click = priv->hidden_message_height > 0;
365         }
366
367         cairo_rectangle (cr,
368                          2*C_X,
369                          HEADER_HEIGHT,
370                          MESSAGE_WIDTH,
371                          message_height);
372         cairo_clip (cr);
373
374         cairo_set_source_surface (cr,
375                                   priv->message_surface,
376                                   2*C_X,
377                                   HEADER_HEIGHT - priv->scroll_offset);
378         cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
379         cairo_paint (cr);
380
381         cairo_pattern_destroy (grad);
382         cairo_destroy (cr);
383
384         return GTK_WIDGET_CLASS (el_home_applet_parent_class)->expose_event (self, event);
385 }
386
387 static void
388 dispose (GObject *self)
389 {
390         ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
391
392         stop_scroll_anim (priv);
393         if (priv->idle_id) {
394                 g_source_remove (priv->idle_id);
395                 priv->idle_id = 0;
396         }
397         if (priv->eventlogger) {
398                 g_object_unref (priv->eventlogger);
399                 priv->eventlogger = NULL;
400         }
401
402         if (priv->message) {
403                 g_free (priv->message);
404                 priv->message = NULL;
405         }
406         if (priv->font_desc) {
407                 pango_font_description_free (priv->font_desc);
408                 priv->font_desc = NULL;
409         }
410
411         G_OBJECT_CLASS (el_home_applet_parent_class)->dispose (self);
412 }
413
414 static void
415 finalize (GObject *self)
416 {
417         G_OBJECT_CLASS (el_home_applet_parent_class)->finalize (self);
418 }
419
420 static gchar*
421 format_time (time_t t)
422 {
423         static const guint RESULT_SIZE = 32;
424
425         time_t now;
426         struct tm now_tm, t_tm;
427         const gchar *format = "%Y.%m.%d %T";
428         gchar *result = g_malloc0 (RESULT_SIZE);
429
430         now = time (NULL);
431         localtime_r (&now, &now_tm);
432         localtime_r (&t, &t_tm);
433
434         if ((now_tm.tm_year == t_tm.tm_year) &&
435             (now_tm.tm_mon  == t_tm.tm_mon) &&
436             (now_tm.tm_mday == t_tm.tm_mday))
437                 format = "%T";
438
439         strftime (result, RESULT_SIZE, format, &t_tm);
440
441         return result;
442 }
443
444 static void
445 show_event (ELHomeApplet *self, RTComElIter *it)
446 {
447         ELHomeAppletPrivate *priv = self->priv;
448
449         gchar *remote = NULL;
450         gchar *received = NULL;
451         const gchar *icon_name = NULL;
452
453         if (priv->message) {
454                 g_free (priv->message);
455                 priv->message = NULL;
456         }
457
458         if (it && rtcom_el_iter_first (it)) {
459                 rtcom_el_iter_dup_string (it, "free-text", &priv->message);
460                 if (priv->message) {
461                         const gchar *service;
462                         time_t received_t;
463
464                         rtcom_el_iter_get_int (it, "id", &priv->event_id);
465                         if (rtcom_el_iter_get_int (it, "start-time", (gint*)&received_t))
466                                 received = format_time (received_t);
467
468                         if (!rtcom_el_iter_dup_string (it, "remote-name", &remote))
469                                 rtcom_el_iter_dup_string (it, "remote-id", &remote);
470                         service = rtcom_el_iter_get_service (it);
471                         if (!g_strcmp0 (service, "RTCOM_EL_SERVICE_SMS"))
472                                 icon_name = "chat_unread_sms";
473                         else if (!g_strcmp0 (service, "RTCOM_EL_SERVICE_CHAT"))
474                                 icon_name = "chat_unread_chat";
475                 }
476         }
477         else {
478                 priv->event_id = -1;
479         }
480
481         if (priv->message)
482                 gtk_widget_hide (priv->empty);
483         else
484                 gtk_widget_show (priv->empty);
485
486         gtk_label_set_text (GTK_LABEL (priv->sender), remote);
487         gtk_label_set_text (GTK_LABEL (priv->received), received);
488
489         if (icon_name) {
490                 const gchar *current_icon_name;
491                 gtk_image_get_icon_name (GTK_IMAGE (priv->icon),
492                                          &current_icon_name,
493                                          NULL);
494                 if (g_strcmp0 (current_icon_name, icon_name))
495                         gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
496                                                       icon_name,
497                                                       HILDON_ICON_SIZE_FINGER);
498                 gtk_widget_show (priv->icon);
499         }
500         else
501                 gtk_widget_hide (priv->icon);
502
503         g_free (remote);
504
505         stop_scroll_anim (priv);
506         priv->scroll_offset = 0;
507         if (priv->message_surface) {
508                 cairo_surface_destroy (priv->message_surface);
509                 priv->message_surface = NULL;
510         }
511         gtk_widget_queue_draw (GTK_WIDGET (self));
512 }
513
514 static RTComElIter*
515 make_query (RTComEl *el, gint event_id)
516 {
517         RTComElQuery *query = NULL;
518         RTComElIter *it = NULL;
519
520         static const gchar *services[] = {"RTCOM_EL_SERVICE_SMS",
521                                           "RTCOM_EL_SERVICE_CHAT",
522                                           NULL};
523         static const gchar *event_types[] = {"RTCOM_EL_EVENTTYPE_SMS_INBOUND",
524                                              "RTCOM_EL_EVENTTYPE_CHAT_INBOUND",
525                                              NULL};
526
527         query = rtcom_el_query_new (el);
528         rtcom_el_query_set_limit (query, 1);
529         if (event_id >= 0) {
530                 rtcom_el_query_prepare (query,
531                                         "is-read", FALSE, RTCOM_EL_OP_EQUAL,
532                                         "id", event_id, RTCOM_EL_OP_EQUAL,
533                                         "service", services, RTCOM_EL_OP_IN_STRV,
534                                         "event-type", event_types, RTCOM_EL_OP_IN_STRV,
535                                         NULL);
536         }
537         else {
538                 rtcom_el_query_prepare (query,
539                                         "is-read", FALSE, RTCOM_EL_OP_EQUAL,
540                                         "service", services, RTCOM_EL_OP_IN_STRV,
541                                         "event-type", event_types, RTCOM_EL_OP_IN_STRV,
542                                         NULL);
543         }
544         it = rtcom_el_get_events (el, query);
545         g_object_unref (query);
546
547         return it;
548 }
549
550 static void
551 update_unread_label (ELHomeApplet *self)
552 {
553         ELHomeAppletPrivate *priv = self->priv;
554         gchar *text;
555
556         if (priv->unread_count > 0) {
557                 text = g_strdup_printf ("%d", priv->unread_count);
558                 gtk_label_set_text (GTK_LABEL (priv->unread), text);
559                 g_free (text);
560         }
561         else
562                 gtk_label_set_text (GTK_LABEL (priv->unread), NULL);
563 }
564
565 static gint
566 query_unread_events (RTComEl *el)
567 {
568         sqlite3 *db;
569         sqlite3_stmt *stmt;
570         int ret;
571         gint count = 0;
572
573         g_object_get (el, "db", &db, NULL);
574
575         if (sqlite3_prepare_v2 (db,
576                                 "SELECT SUM(total_events)-SUM(read_events) FROM GroupCache;",
577                                 -1,
578                                 &stmt,
579                                 NULL) != SQLITE_OK) {
580                 g_error ("%s: can't compile SQL", G_STRFUNC);
581                 return -1;
582         }
583
584         while (SQLITE_BUSY == (ret = sqlite3_step (stmt)));
585
586         if (ret == SQLITE_ROW) {
587                 count = sqlite3_column_int (stmt, 0);
588         }
589         else {
590                 g_error ("%s: error while executing SQL", G_STRFUNC);
591         }
592
593         sqlite3_finalize (stmt);
594
595         return count;
596 }
597
598 static void
599 read_event (ELHomeApplet *self)
600 {
601         ELHomeAppletPrivate *priv = self->priv;
602         RTComElIter *it = NULL;
603
604         it = make_query (priv->eventlogger, -1);
605         show_event (self, it);
606         if (it) g_object_unref (it);
607 }
608
609 static void
610 mark_as_read (ELHomeApplet *self)
611 {
612         ELHomeAppletPrivate *priv = self->priv;
613
614         if (priv->event_id >= 0) {
615                 rtcom_el_set_read_event (priv->eventlogger,
616                                          priv->event_id,
617                                          TRUE,
618                                          NULL);
619                 read_event (self);
620                 priv->unread_count--;
621                 update_unread_label (self);
622         }
623 }
624
625 static gboolean
626 read_new_event (ELHomeApplet *self)
627 {
628         ELHomeAppletPrivate *priv = self->priv;
629
630         read_event (self);
631         priv->unread_count = query_unread_events (priv->eventlogger);
632         update_unread_label (self);
633
634         priv->idle_id = 0;
635
636         return FALSE;
637 }
638
639 static void
640 add_new_idle (ELHomeApplet *self)
641 {
642         ELHomeAppletPrivate *priv = self->priv;
643
644         if (priv->idle_id)
645                 g_source_remove (priv->idle_id);
646         priv->idle_id = g_idle_add ((GSourceFunc)read_new_event,
647                                     self);
648 }
649
650 static void
651 new_event_cb (RTComEl      *backend,
652               gint          event_id,
653               const gchar  *local_uid,
654               const gchar  *remote_uid,
655               const gchar  *remote_ebook_uid,
656               const gchar  *group_uid,
657               const gchar  *service,
658               ELHomeApplet *self)
659 {
660         add_new_idle (self);
661 }
662
663 static gboolean
664 scroll_anim_cb (ELHomeApplet *self)
665 {
666         ELHomeAppletPrivate *priv = self->priv;
667         gboolean to_continue;
668
669         priv->scroll_offset += SCROLL_STEP;
670         gtk_widget_queue_draw_area (GTK_WIDGET (self),
671                                     3*C_X,
672                                     HEADER_HEIGHT + C_Y,
673                                     MESSAGE_WIDTH,
674                                     C_HEIGHT - priv->received->allocation.height - HEADER_HEIGHT);
675
676         to_continue = priv->scroll_offset <= priv->hidden_message_height;
677         if (!to_continue)
678                 priv->scroll_anim_id = 0;
679
680         return to_continue;
681 }
682
683 static gboolean
684 button_press_event_cb (GtkWidget      *widget,
685                        GdkEventButton *event,
686                        ELHomeApplet   *self)
687 {
688         ELHomeAppletPrivate *priv = self->priv;
689
690         if (priv->event_id > 0) {
691                 priv->active = TRUE;
692                 gtk_widget_queue_draw (widget);
693         }
694
695         return TRUE;
696 }
697
698 static gboolean
699 button_release_event_cb (GtkWidget      *widget,
700                          GdkEventButton *event,
701                          ELHomeApplet   *self)
702 {
703         ELHomeAppletPrivate *priv = self->priv;
704
705         if (priv->active) {
706                 priv->active = FALSE;
707                 stop_scroll_anim (priv);
708                 if (priv->scroll_on_click) {
709                         priv->scroll_on_click = FALSE;
710                         priv->scroll_anim_id = g_timeout_add (SCROLL_PERIOD,
711                                                               (GSourceFunc)scroll_anim_cb,
712                                                               self);
713                 }
714                 else {
715 #ifndef DEBUG_LAYOUT
716                         mark_as_read (self);
717 #endif
718                 }
719
720                 gtk_widget_queue_draw (widget);
721         }
722
723         return TRUE;
724 }
725
726 static gboolean
727 leave_notify_event_cb (GtkWidget        *widget,
728                        GdkEventCrossing *event,
729                        ELHomeApplet     *self)
730 {
731         ELHomeAppletPrivate *priv = self->priv;
732
733         if (priv->active) {
734                 priv->active = FALSE;
735                 stop_scroll_anim (priv);
736                 gtk_widget_queue_draw (widget);
737         }
738
739         return FALSE;
740 }
741
742 static void
743 el_home_applet_init (ELHomeApplet *self)
744 {
745         ELHomeAppletPrivate *priv;
746         GtkWidget *event_box;
747         GtkWidget *hbox, *vbox, *align;
748
749         self->priv = EL_HOME_APPLET_GET_PRIVATE (self);
750         priv = self->priv;
751
752         gtk_widget_set_app_paintable (GTK_WIDGET (self), TRUE);
753
754         priv->unread = gtk_label_new ("12");
755         hildon_helper_set_logical_color (priv->unread,
756                                          GTK_RC_FG,
757                                          GTK_STATE_NORMAL,
758                                          "ActiveTextColor");
759         gtk_misc_set_alignment (GTK_MISC (priv->unread),
760                                 1.0f,
761                                 0.5f);
762         gtk_widget_set_size_request (priv->unread,
763                                      -1,
764                                      HEADER_HEIGHT);
765
766         priv->icon = gtk_image_new_from_icon_name ("chat_unread_sms",
767                                                    HILDON_ICON_SIZE_FINGER);
768         gtk_misc_set_alignment (GTK_MISC (priv->icon),
769                                 0.5f,
770                                 0.5f);
771
772         priv->sender = gtk_label_new ("asdf asdf asdf asdf asdf");
773         gtk_misc_set_alignment (GTK_MISC (priv->sender),
774                                 0.5f,
775                                 0.5f);
776         gtk_label_set_ellipsize (GTK_LABEL (priv->sender),
777                                  PANGO_ELLIPSIZE_END);
778         gtk_widget_set_name (priv->sender, "hildon-shadow-label");
779         hildon_helper_set_logical_font (priv->sender, "SystemFont");
780
781         priv->message = g_strdup ("One two three four five six seven eight nine ten");
782
783         /* TODO: l10n */
784         priv->empty = gtk_label_new ("No new messages");
785         gtk_widget_set_name (priv->empty, "hildon-shadow-label");
786         GTK_WIDGET_SET_FLAGS (priv->empty, GTK_NO_SHOW_ALL);
787
788         priv->received = gtk_label_new ("aewf aewf aewf awef");
789         gtk_misc_set_alignment (GTK_MISC (priv->received),
790                                 1.0f,
791                                 0.5f);
792         gtk_widget_set_size_request (priv->received,
793                                      MESSAGE_WIDTH,
794                                      -1);
795         hildon_helper_set_logical_font (priv->received, "SmallSystemFont");
796         gtk_widget_set_name (priv->received, "hildon-shadow-label");
797
798         hbox = gtk_hbox_new (FALSE, 0);
799         gtk_box_pack_start (GTK_BOX (hbox), priv->unread, FALSE, FALSE, 0);
800         gtk_box_pack_start (GTK_BOX (hbox), priv->icon, FALSE, FALSE, 0);
801         gtk_box_pack_start (GTK_BOX (hbox), priv->sender, TRUE, TRUE, 0);
802
803         vbox = gtk_vbox_new (FALSE, 0);
804         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
805         gtk_box_pack_start (GTK_BOX (vbox), priv->empty, TRUE, TRUE, 0);
806         gtk_box_pack_end (GTK_BOX (vbox), priv->received, FALSE, FALSE, 0);
807
808         align = gtk_alignment_new (0.5f, 0.0f, 1.0f, 1.0f);
809         gtk_alignment_set_padding (GTK_ALIGNMENT (align),
810                                    0, 0, HILDON_MARGIN_DEFAULT, HILDON_MARGIN_DEFAULT);
811
812         gtk_container_set_border_width (GTK_CONTAINER (vbox), HILDON_MARGIN_HALF);
813
814         event_box = gtk_event_box_new ();
815         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
816         gtk_widget_set_size_request (event_box, BOX_WIDTH, BOX_HEIGHT);
817
818         gtk_container_add (GTK_CONTAINER (align), vbox);
819         gtk_container_add (GTK_CONTAINER (event_box), align);
820         gtk_container_add (GTK_CONTAINER (self), event_box);
821
822         g_signal_connect (event_box,
823                           "button-press-event",
824                           G_CALLBACK (button_press_event_cb),
825                           self);
826         g_signal_connect (event_box,
827                           "button-release-event",
828                           G_CALLBACK (button_release_event_cb),
829                           self);
830         g_signal_connect (event_box,
831                           "leave-notify-event",
832                           G_CALLBACK (leave_notify_event_cb),
833                           self);
834
835         g_signal_connect (event_box,
836                           "style-set",
837                           G_CALLBACK (style_set_cb),
838                           self);
839         g_signal_connect (self,
840                           "notify::is-on-current-desktop",
841                           G_CALLBACK (notify_on_current_desktop),
842                           self);
843
844         gtk_widget_show_all (GTK_WIDGET (event_box));
845
846 #ifndef DEBUG_LAYOUT
847         priv->eventlogger = rtcom_el_new ();
848         g_signal_connect (priv->eventlogger,
849                           "new-event",
850                           G_CALLBACK (new_event_cb),
851                           self);
852         g_signal_connect (priv->eventlogger,
853                           "event-updated",
854                           G_CALLBACK (new_event_cb),
855                           self);
856
857         read_new_event (self);
858 #endif
859 }
860
861 static void
862 el_home_applet_class_init (ELHomeAppletClass *klass)
863 {
864         GObjectClass *object_class = G_OBJECT_CLASS (klass);
865         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
866
867         object_class->dispose = dispose;
868         object_class->finalize = finalize;
869         widget_class->expose_event = expose_event;
870         widget_class->realize = el_home_applet_realize;
871
872         g_type_class_add_private (klass, sizeof (ELHomeAppletPrivate));
873 }
874