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