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