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