Plub mem leak
[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 #include <libintl.h>
26 #include <hildon/hildon.h>
27 #include <rtcom-eventlogger/eventlogger.h>
28 #include <sqlite3.h>
29 #include <string.h>
30 #include <gconf/gconf-client.h>
31 #include <libosso-abook/osso-abook-init.h>
32 #include <libosso-abook/osso-abook-aggregator.h>
33 #include <libosso-abook/osso-abook-contact.h>
34 #include <libosso-abook/osso-abook-waitable.h>
35 #include <libosso-abook/osso-abook-presence.h>
36 #include <libosso-abook/osso-abook-touch-contact-starter.h>
37 #include <libosso-abook/osso-abook-temporary-contact-dialog.h>
38 #include <libosso-abook/osso-abook-account-manager.h>
39
40 #include <telepathy-glib/interfaces.h>
41 #include <telepathy-glib/dbus.h>
42 #include <rtcom-telepathy-glib/extensions.h>
43
44 #define EL_HOME_APPLET_GET_PRIVATE(obj) ( \
45         G_TYPE_INSTANCE_GET_PRIVATE (obj, \
46                 EL_TYPE_HOME_APPLET, ELHomeAppletPrivate))
47
48 #define BOX_WIDTH 352
49 #define BOX_HEIGHT 284
50
51 #define CONTENT_OFFSET_X HILDON_MARGIN_HALF
52 #define CONTENT_OFFSET_Y_TOP 4*HILDON_MARGIN_HALF
53 #define CONTENT_OFFSET_Y_BOTTOM HILDON_MARGIN_HALF
54 #define C_WIDTH (BOX_WIDTH - 2*CONTENT_OFFSET_X)
55 #define C_HEIGHT (BOX_HEIGHT - (CONTENT_OFFSET_Y_TOP + CONTENT_OFFSET_Y_BOTTOM))
56
57 #define HEADER_HEIGHT 48
58 #define FOOTER_HEIGHT 24
59 #define FOOTER_HEIGHT_PRESS FOOTER_HEIGHT*2 /* approx, used only for checking clicks */
60 #define FOOTER_WIDTH C_WIDTH/4
61 #define FOOTER_WIDTH_PRESS (FOOTER_WIDTH + FOOTER_WIDTH/2) /* approx, used only for checking clicks, bigger than controls */
62
63 #define MESSAGE_HEIGHT (C_HEIGHT - HEADER_HEIGHT - FOOTER_HEIGHT)
64 #define MESSAGE_WIDTH (C_WIDTH - 2*HILDON_MARGIN_DEFAULT)
65
66 #define AVATAR_SIZE HILDON_ICON_PIXEL_SIZE_THUMB
67
68 #define AVATAR_X (C_WIDTH - AVATAR_SIZE - HILDON_MARGIN_DEFAULT)
69 #define AVATAR_Y 3*HILDON_MARGIN_HALF
70
71 #define BOX_RADIOUS 20
72
73 #define SCROLL_PERIOD 100 /* ms */
74 #define SCROLL_STEP 1 /* pixel */
75 #define TEXT_Y_OFFSET (HEADER_HEIGHT + HILDON_MARGIN_HALF)
76
77 #define NOTIFICATION_UI_DBUS_NAME     "org.freedesktop.Telepathy.Client.NotificationUI"
78 #define NOTIFICATION_UI_DBUS_PATH     "/org/freedesktop/Telepathy/Client/NotificationUI"
79 #define NOTIFICATION_UI_DBUS_IFACE    "com.nokia.RtcomNotificationUi"
80
81 #define CONVERSATIONS_UI_DBUS_NAME     "com.nokia.MessagingUI"
82 #define CONVERSATIONS_UI_DBUS_PATH     "/com/nokia/MessagingUI"
83 #define CONVERSATIONS_UI_DBUS_IFACE    "com.nokia.MessagingUI"
84
85 static const gchar *conv_services[] = {"RTCOM_EL_SERVICE_SMS",
86                                        "RTCOM_EL_SERVICE_CHAT",
87                                        NULL};
88 static const gchar *conv_event_types[] = {"RTCOM_EL_EVENTTYPE_SMS_MESSAGE",
89                                           "RTCOM_EL_EVENTTYPE_CHAT_MESSAGE",
90                                           NULL};
91
92 typedef enum {
93         SELECTED_NONE,
94         SELECTED_HEADER,
95         SELECTED_BODY,
96         SELECTED_FOOTER
97 } WidgetActiveSelection;
98
99 struct _ELHomeAppletPrivate
100 {
101         RTComEl *eventlogger;
102
103         GtkWidget *sender;
104         GtkWidget *icon;
105         GtkWidget *unread;
106         GtkWidget *received;
107         GtkWidget *cut_message;
108
109         /* empty view*/
110         GtkWidget *empty;
111         GtkWidget *sms_total;
112         GtkWidget *chat_total;
113
114         gchar *message;
115         gint event_id;
116
117         WidgetActiveSelection active;
118
119         guint unread_count;
120
121         struct {
122                 float red;
123                 float green;
124                 float blue;
125         } active_color;
126         PangoFontDescription *font_desc;
127
128         GdkPixbuf *avatar_pixbuf;
129         GdkPixbuf *presence_pixbuf;
130
131         guint idle_id;
132
133         cairo_surface_t *message_surface;
134
135         gboolean scroll_on_click;
136         gint scroll_offset;
137         gint hidden_message_height;
138         guint scroll_anim_id;
139
140         OssoABookRoster *aggregator;
141         OssoABookWaitableClosure *aggregator_ready_closure;
142         gchar *contact_id;
143         gchar *remote_id;
144         gchar *local_id;
145         gchar *group_uid;
146         OssoABookContact *contact;
147
148         gboolean time_fmt_24h;
149
150         guint init_timer;
151 };
152
153 HD_DEFINE_PLUGIN_MODULE (ELHomeApplet, el_home_applet, HD_TYPE_HOME_PLUGIN_ITEM);
154
155 const gchar* g_module_check_init (GModule *module);
156 const gchar*
157 g_module_check_init (GModule *module)
158 {
159         g_module_make_resident (module);
160         return NULL;
161 }
162
163 static void
164 el_home_applet_class_finalize (ELHomeAppletClass *klass)
165 {
166 }
167
168 static void
169 el_home_applet_realize (GtkWidget *widget)
170 {
171         GdkScreen *screen;
172
173         screen = gtk_widget_get_screen (widget);
174         gtk_widget_set_colormap (widget,
175                                  gdk_screen_get_rgba_colormap (screen));
176
177         gtk_widget_set_app_paintable (widget,
178                                       TRUE);
179
180         GTK_WIDGET_CLASS (el_home_applet_parent_class)->realize (widget);
181 }
182
183 enum {
184         ROUND_CORNER_TL = 1,
185         ROUND_CORNER_TR = 1<<1,
186         ROUND_CORNER_BL = 1<<2,
187         ROUND_CORNER_BR = 1<<3,
188         ROUND_CORNER_ALL = ROUND_CORNER_TL | ROUND_CORNER_TR |
189                            ROUND_CORNER_BL | ROUND_CORNER_BR
190 };
191
192 /**
193  * Draw rectangle with optional round corners.
194  *
195  * @x
196  * @y
197  * @w width
198  * @h height
199  * @r round corner radious
200  * @round_corners define which corners draw round, ROUND_CORNER_TL,
201  *                ROUND_CORNER_TR, ROUND_CORNER_BL, ROUND_CORNER_BR
202  */
203 static void
204 rounded_rectangle (cairo_t *cr,
205                    double x,
206                    double y,
207                    double w,
208                    double h,
209                    double r,
210                    guint round_corners)
211 {
212         if (round_corners & ROUND_CORNER_TL)
213                 cairo_move_to (cr, x + r, y);
214         else
215                 cairo_move_to (cr, x, y);
216
217         if (round_corners & ROUND_CORNER_TR) {
218                 cairo_line_to (cr, x + w - r, y);
219                 cairo_rel_curve_to (cr,
220                                     r, 0,
221                                     r, 0,
222                                     r, r);
223         }
224         else
225                 cairo_line_to (cr, x + w, y);
226
227         if (round_corners & ROUND_CORNER_BR) {
228                 cairo_line_to (cr, x + w, y + h - r);
229                 cairo_rel_curve_to (cr,
230                                     0, r,
231                                     0, r,
232                                     -r, r);
233         }
234         else
235                 cairo_line_to (cr, x + w, y + h);
236
237         if (round_corners & ROUND_CORNER_BL) {
238                 cairo_line_to (cr, x + r, y + h);
239                 cairo_rel_curve_to (cr,
240                                     -r, 0,
241                                     -r, 0,
242                                     -r, -r);
243         }
244         else
245                 cairo_line_to (cr, x, y + h);
246
247         if (round_corners & ROUND_CORNER_TL) {
248                 cairo_line_to (cr, x, y + r);
249                 cairo_rel_curve_to (cr,
250                                     0, -r,
251                                     0, -r,
252                                     r, -r);
253         }
254         else
255                 cairo_line_to (cr, x, y);
256 }
257
258 static cairo_surface_t*
259 draw_text (cairo_t              *cr,
260            PangoFontDescription *desc,
261            const gchar          *text,
262            gint                  width,
263            gint                 *height)
264 {
265         PangoLayout *layout;
266         PangoRectangle extent;
267
268         cairo_surface_t *gdk_surface, *result_surface;
269         cairo_t *msg_cr;
270
271         /* Create a PangoLayout, set the font and text */
272         layout = pango_cairo_create_layout (cr);
273         pango_layout_set_text (layout,
274                                text,
275                                -1);
276         pango_layout_set_font_description (layout, desc);
277
278         pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
279         pango_layout_set_width (layout, PANGO_SCALE*width);
280
281         pango_layout_get_pixel_extents (layout, NULL, &extent);
282         *height = extent.height;
283
284         gdk_surface = cairo_get_target (cr);
285         result_surface = cairo_surface_create_similar
286                 (gdk_surface,
287                  CAIRO_CONTENT_COLOR_ALPHA,
288                  width,
289                  extent.height);
290         msg_cr = cairo_create (result_surface);
291
292         pango_cairo_update_layout (msg_cr, layout);
293         /* draw shadow */
294         cairo_move_to (msg_cr, 1, 1);
295         cairo_set_source_rgba (msg_cr, 0.2, 0.2, 0.2, 0.8);
296         pango_cairo_show_layout (msg_cr, layout);
297
298         /* draw fg */
299         cairo_move_to (msg_cr, 0, 0);
300         cairo_set_source_rgba (msg_cr, 1.0, 1.0, 1.0, 1.0);
301         pango_cairo_show_layout (msg_cr, layout);
302
303         cairo_destroy (msg_cr);
304         g_object_unref (layout);
305
306         return result_surface;
307 }
308
309 static gboolean
310 stop_scroll_anim (ELHomeAppletPrivate *priv)
311 {
312         gboolean result = priv->scroll_anim_id > 0;
313
314         if (result) {
315                 g_source_remove (priv->scroll_anim_id);
316                 priv->scroll_anim_id = 0;
317                 priv->scroll_on_click = FALSE;
318                 gtk_widget_hide (priv->cut_message);
319         }
320
321         return result;
322 }
323
324 static void
325 style_set_cb (GtkWidget *widget,
326               GtkStyle  *previous_style,
327               ELHomeApplet *self)
328 {
329         ELHomeAppletPrivate *priv = self->priv;
330         GdkColor color;
331         GtkStyle *font_style;
332
333         font_style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
334                                                 "SystemFont",
335                                                 NULL,
336                                                 G_TYPE_NONE);
337         if (font_style && font_style->font_desc) {
338                 if (priv->font_desc)
339                         pango_font_description_free (priv->font_desc);
340                 priv->font_desc = pango_font_description_copy (font_style->font_desc);
341         }
342
343         if (gtk_style_lookup_color (widget->style,
344                                     "ActiveTextColor",
345                                     &color)) {
346                 priv->active_color.red = color.red/(float)G_MAXUINT16;
347                 priv->active_color.green = color.green/(float)G_MAXUINT16;
348                 priv->active_color.blue = color.blue/(float)G_MAXUINT16;
349         }
350 }
351
352 static void
353 reset_scroll (ELHomeApplet *self)
354 {
355         ELHomeAppletPrivate *priv = self->priv;
356
357         if (stop_scroll_anim (self->priv)) {
358                 priv->scroll_on_click = TRUE;/* priv->scroll_offset; */
359                 priv->scroll_offset = 0;
360                 if (priv->scroll_on_click)
361                         gtk_widget_show (priv->cut_message);
362         }
363 }
364
365 static void
366 notify_on_current_desktop (GObject      *object,
367                            GParamSpec   *unused G_GNUC_UNUSED,
368                            ELHomeApplet *self)
369 {
370         gboolean on;
371
372         g_object_get (object, "is-on-current-desktop", &on, NULL);
373         if (!on) {
374                 reset_scroll (self);
375                 gtk_widget_queue_draw (GTK_WIDGET (self));
376         }
377 }
378
379 static gboolean
380 expose_event (GtkWidget *self, GdkEventExpose *event)
381 {
382         ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
383         cairo_t *cr;
384         cairo_pattern_t *grad;
385
386         cr = gdk_cairo_create (self->window);
387         gdk_cairo_region (cr, event->region);
388         cairo_clip (cr);
389
390         cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
391
392         /* draw bound box */
393         cairo_set_source_rgba (cr, 0.4f, 0.4f, 0.4f, 0.1f);
394         cairo_set_line_width (cr, 3.0f);
395
396         rounded_rectangle (cr,
397                            CONTENT_OFFSET_X,
398                            CONTENT_OFFSET_Y_TOP,
399                            BOX_WIDTH - 2*CONTENT_OFFSET_X,
400                            BOX_HEIGHT - (CONTENT_OFFSET_Y_TOP + CONTENT_OFFSET_Y_BOTTOM),
401                            BOX_RADIOUS,
402                            ROUND_CORNER_ALL);
403
404         cairo_close_path (cr);
405         cairo_stroke (cr);
406
407         /* draw header */
408         cairo_set_line_width (cr, 1.0f);
409
410         cairo_translate (cr, CONTENT_OFFSET_X, CONTENT_OFFSET_Y_TOP);
411         rounded_rectangle (cr,
412                            0, 0,
413                            C_WIDTH, HEADER_HEIGHT,
414                            BOX_RADIOUS,
415                            ROUND_CORNER_TL | ROUND_CORNER_TR);
416         cairo_close_path (cr);
417
418         switch (priv->active) {
419         case SELECTED_HEADER:
420                 cairo_set_source_rgba (cr,
421                                        priv->active_color.red,
422                                        priv->active_color.green,
423                                        priv->active_color.blue,
424                                        0.8f);
425                 break;
426         default:
427                 cairo_set_source_rgba (cr, 0.2f, 0.2f, 0.2f, 0.8f);
428         }
429
430         cairo_fill (cr);
431
432         cairo_move_to (cr, 0, HEADER_HEIGHT);
433         cairo_line_to (cr, C_WIDTH, HEADER_HEIGHT);
434         cairo_set_source_rgba (cr,
435                                priv->active_color.red,
436                                priv->active_color.green,
437                                priv->active_color.blue,
438                                1.0f);
439         cairo_stroke (cr);
440
441         /* draw body */
442         if (!priv->message) {
443                 rounded_rectangle (cr,
444                                    0, HEADER_HEIGHT,
445                                    C_WIDTH, C_HEIGHT,
446                                    BOX_RADIOUS,
447                                    ROUND_CORNER_BL | ROUND_CORNER_BR);
448                 cairo_close_path (cr);
449         }
450         else
451                 cairo_rectangle (cr, 0, HEADER_HEIGHT,
452                                  C_WIDTH, MESSAGE_HEIGHT);
453
454         /* draw body filling depending on (in)active state */
455         grad = cairo_pattern_create_linear (0, HEADER_HEIGHT,
456                                             0, C_HEIGHT - FOOTER_HEIGHT);
457
458         switch (priv->active) {
459         case SELECTED_BODY:
460                 cairo_pattern_add_color_stop_rgba (grad,
461                                                    0.5f,
462                                                    priv->active_color.red,
463                                                    priv->active_color.green,
464                                                    priv->active_color.blue,
465                                                    0.8f);
466                 cairo_pattern_add_color_stop_rgba (grad,
467                                                    1.0f,
468                                                    priv->active_color.red/2,
469                                                    priv->active_color.green/2,
470                                                    priv->active_color.blue/2,
471                                                    0.8f);
472                 break;
473         default:
474                 cairo_pattern_add_color_stop_rgba (grad, 0.5f,
475                                                    0.4f, 0.4f, 0.4f, 0.8f);
476                 cairo_pattern_add_color_stop_rgba (grad, 1.0f,
477                                                    0.2f, 0.2f, 0.2f, 0.8f);
478         }
479
480         cairo_set_source (cr, grad);
481         cairo_fill (cr);
482
483         cairo_pattern_destroy (grad);
484
485         /* draw avatar */
486         if (priv->avatar_pixbuf) {
487                 rounded_rectangle (cr,
488                                    AVATAR_X, -AVATAR_Y,
489                                    AVATAR_SIZE, AVATAR_SIZE,
490                                    BOX_RADIOUS,
491                                    ROUND_CORNER_ALL);
492                 cairo_close_path (cr);
493
494                 gdk_cairo_set_source_pixbuf (cr,
495                                              priv->avatar_pixbuf,
496                                              AVATAR_X,
497                                              -AVATAR_Y);
498                 cairo_fill_preserve (cr);
499
500                 cairo_set_source_rgba (cr,
501                                        priv->active_color.red,
502                                        priv->active_color.green,
503                                        priv->active_color.blue,
504                                        1.0f);
505                 cairo_stroke (cr);
506         }
507         if (priv->presence_pixbuf) {
508                 guint x = C_WIDTH - HILDON_ICON_PIXEL_SIZE_XSMALL - HILDON_MARGIN_DEFAULT;
509                 guint y = (HEADER_HEIGHT - HILDON_ICON_PIXEL_SIZE_XSMALL)/2;
510
511                 if (priv->avatar_pixbuf)
512                         x -= AVATAR_SIZE + HILDON_MARGIN_DEFAULT;
513
514                 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
515                 gdk_cairo_set_source_pixbuf (cr,
516                                              priv->presence_pixbuf,
517                                              x,
518                                              y);
519                 cairo_paint (cr);
520         }
521
522         if (priv->message) {
523                 /* draw footer unread part bg */
524                 rounded_rectangle (cr,
525                                    0, C_HEIGHT - FOOTER_HEIGHT,
526                                    FOOTER_WIDTH, FOOTER_HEIGHT,
527                                    BOX_RADIOUS,
528                                    ROUND_CORNER_BL);
529                 cairo_close_path (cr);
530
531                 /* draw body filling depending on (in)active state */
532                 switch (priv->active) {
533                 case SELECTED_FOOTER:
534                         cairo_set_source_rgba (cr,
535                                                priv->active_color.red,
536                                                priv->active_color.green,
537                                                priv->active_color.blue,
538                                                0.8f);
539                         break;
540                 default:
541                         cairo_set_source_rgba (cr, 0.1f, 0.1f, 0.1f, 0.9f);
542                 }
543                 cairo_fill (cr);
544
545                 /* draw footer received part bg */
546                 rounded_rectangle (cr,
547                                    FOOTER_WIDTH, C_HEIGHT - FOOTER_HEIGHT,
548                                    C_WIDTH - FOOTER_WIDTH, FOOTER_HEIGHT,
549                                    BOX_RADIOUS,
550                                    ROUND_CORNER_BR);
551                 cairo_close_path (cr);
552
553                 cairo_set_source_rgba (cr, 0.2f, 0.2f, 0.2f, 0.8f);
554                 cairo_fill (cr);
555
556                 /* draw message */
557                 if (!priv->message_surface) {
558                         gint height;
559
560                         priv->message_surface = draw_text (cr,
561                                                            priv->font_desc,
562                                                            priv->message,
563                                                            MESSAGE_WIDTH,
564                                                            &height);
565
566                         priv->hidden_message_height = height - MESSAGE_HEIGHT;
567                         priv->scroll_on_click = priv->hidden_message_height > 0;
568                         if (priv->scroll_on_click)
569                                 gtk_widget_show (priv->cut_message);
570                 }
571
572                 cairo_rectangle (cr,
573                                  2*CONTENT_OFFSET_X,
574                                  TEXT_Y_OFFSET,
575                                  MESSAGE_WIDTH,
576                                  MESSAGE_HEIGHT);
577                 cairo_clip (cr);
578
579                 cairo_set_source_surface (cr,
580                                           priv->message_surface,
581                                           2*CONTENT_OFFSET_X,
582                                           TEXT_Y_OFFSET - priv->scroll_offset);
583                 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
584                 cairo_paint (cr);
585         }
586
587         cairo_destroy (cr);
588
589         return GTK_WIDGET_CLASS (el_home_applet_parent_class)->expose_event (self, event);
590 }
591
592 static void
593 resize_sender (ELHomeAppletPrivate *priv)
594 {
595         guint width = C_WIDTH;
596
597         if (priv->avatar_pixbuf) {
598                 width -= AVATAR_SIZE + HILDON_MARGIN_DEFAULT;
599         }
600
601         if (priv->presence_pixbuf) {
602                 width -= HILDON_ICON_PIXEL_SIZE_XSMALL + HILDON_MARGIN_DEFAULT;
603         }
604
605         gtk_widget_set_size_request (priv->sender,
606                                      width,
607                                      HILDON_ICON_PIXEL_SIZE_THUMB);
608 }
609
610 static void
611 update_presence_pixbuf (ELHomeApplet *self,
612                         OssoABookPresence *presence)
613 {
614         ELHomeAppletPrivate *priv = self->priv;
615         const gchar *icon_name = osso_abook_presence_get_icon_name (presence);
616         gboolean resize = !!priv->presence_pixbuf ^ !!icon_name; /* logical via bit XOR */
617
618         if (priv->presence_pixbuf) {
619                 g_object_unref (priv->presence_pixbuf);
620                 priv->presence_pixbuf = NULL;
621         }
622
623         if (icon_name)
624                 priv->presence_pixbuf = gtk_icon_theme_load_icon
625                         (gtk_icon_theme_get_default (),
626                          icon_name,
627                          HILDON_ICON_PIXEL_SIZE_XSMALL,
628                          0, NULL);
629         if (resize)
630                 resize_sender (priv);
631
632         gtk_widget_queue_draw (GTK_WIDGET (self));
633 }
634
635 static void
636 presence_updated (OssoABookPresence *presence,
637                   GParamSpec *spec,
638                   gpointer *user_data)
639 {
640         ELHomeApplet *self = EL_HOME_APPLET(user_data);
641
642         if (!OSSO_ABOOK_IS_CONTACT(self->priv->contact))
643                 return;
644
645         update_presence_pixbuf (self, presence);
646 }
647
648 static void
649 show_contact (ELHomeApplet *self)
650 {
651         ELHomeAppletPrivate *priv = self->priv;
652
653         g_return_if_fail (priv->contact);
654
655         gtk_label_set_text (GTK_LABEL (priv->sender),
656                             osso_abook_contact_get_display_name (priv->contact));
657         resize_sender (priv);
658         gtk_widget_queue_draw (GTK_WIDGET (self));
659 }
660
661 static void
662 resolve_contact (ELHomeApplet *self)
663 {
664         ELHomeAppletPrivate *priv = self->priv;
665         GList *contacts = NULL;
666
667         if (priv->contact_id) {
668                 contacts = osso_abook_aggregator_lookup
669                         (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
670                          priv->contact_id);
671         }
672         else if (priv->local_id && priv->remote_id) {
673                 if (g_strcmp0 (priv->local_id, "ring/tel/ring" == 0)) {
674                         contacts = osso_abook_aggregator_find_contacts_for_phone_number
675                                 (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
676                                  priv->remote_id,
677                                  TRUE);
678                 }
679                 else {
680                         McAccount *account;
681                         account = osso_abook_account_manager_lookup_by_name
682                                 (NULL,
683                                  priv->local_id);
684                         if (account) {
685                                 contacts = osso_abook_aggregator_find_contacts_for_im_contact
686                                         (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
687                                          priv->remote_id,
688                                          account);
689                         }
690                 }
691         }
692
693         if (contacts && contacts->data) {
694                 priv->contact = g_object_ref (OSSO_ABOOK_CONTACT (contacts->data));
695                 g_signal_connect (priv->contact,
696                                   "notify::presence-status",
697                                   G_CALLBACK (presence_updated),
698                                   self);
699                 priv->avatar_pixbuf = osso_abook_avatar_get_image_scaled
700                         (OSSO_ABOOK_AVATAR (priv->contact),
701                          HILDON_ICON_PIXEL_SIZE_THUMB,
702                          HILDON_ICON_PIXEL_SIZE_THUMB,
703                          TRUE);
704                 update_presence_pixbuf (self,
705                                         OSSO_ABOOK_PRESENCE (priv->contact));
706                 show_contact (self);
707         }
708 }
709
710 static void
711 contacts_added (OssoABookRoster  *roster,
712                 OssoABookContact **contacts,
713                 gpointer          userdata)
714 {
715         ELHomeApplet *self = EL_HOME_APPLET (userdata);
716         ELHomeAppletPrivate *priv = self->priv;
717
718         if (!priv->contact)
719                 resolve_contact (self);
720 }
721
722 static void
723 reset_contact (ELHomeApplet *self, gboolean resize)
724 {
725         ELHomeAppletPrivate *priv = self->priv;
726
727         if (priv->avatar_pixbuf) {
728                 g_object_unref (priv->avatar_pixbuf);
729                 priv->avatar_pixbuf = NULL;
730         }
731
732         if (priv->presence_pixbuf) {
733                 g_object_unref (priv->presence_pixbuf);
734                 priv->presence_pixbuf = NULL;
735         }
736
737         if (priv->contact) {
738                 g_signal_handlers_disconnect_by_func (priv->contact,
739                                                       presence_updated,
740                                                       self);
741                 g_object_unref (priv->contact);
742                 priv->contact = NULL;
743         }
744
745         if (resize)
746                 resize_sender (priv);
747 }
748
749 static void
750 contacts_removed (OssoABookRoster *roster,
751                   const gchar     **ids,
752                   gpointer         userdata)
753 {
754         ELHomeApplet *self = EL_HOME_APPLET (userdata);
755         ELHomeAppletPrivate *priv = self->priv;
756
757         if (priv->contact) {
758                 const gchar **contact_id;
759                 const gchar *uid = osso_abook_contact_get_uid (priv->contact);
760
761                 for (contact_id = ids; *contact_id; contact_id++) {
762                         if (strcmp (*contact_id, priv->contact_id) == 0) {
763                                 reset_contact (self, TRUE);
764
765                                 gtk_widget_queue_draw (GTK_WIDGET (self));
766                                 return;
767                         }
768                         if (strcmp (*contact_id, uid) == 0) {
769                                 reset_contact (self, TRUE);
770                                 resolve_contact (self);
771                                 gtk_widget_queue_draw (GTK_WIDGET (self));
772                                 return;
773                         }
774                 }
775         }
776 }
777
778 static void
779 clean_contact (ELHomeApplet *self, gboolean resize)
780 {
781         ELHomeAppletPrivate *priv = self->priv;
782
783         reset_contact (self, resize);
784
785         if (priv->aggregator) {
786                 if (priv->aggregator_ready_closure){
787                         osso_abook_waitable_cancel (OSSO_ABOOK_WAITABLE (priv->aggregator),
788                                                     priv->aggregator_ready_closure);
789                         priv->aggregator_ready_closure = NULL;
790                 }
791                 g_signal_handlers_disconnect_by_func (priv->aggregator,
792                                                       contacts_added,
793                                                       self);
794                 g_signal_handlers_disconnect_by_func (priv->aggregator,
795                                                       contacts_removed,
796                                                       self);
797                 osso_abook_roster_stop (priv->aggregator);
798                 g_object_unref (priv->aggregator);
799                 priv->aggregator = NULL;
800         }
801 }
802
803 static void
804 clean_state (ELHomeApplet *self)
805 {
806         ELHomeAppletPrivate *priv = self->priv;
807
808         if (priv->message) {
809                 g_free (priv->message);
810                 priv->message = NULL;
811         }
812
813         if (priv->contact_id) {
814                 g_free (priv->contact_id);
815                 priv->contact_id = NULL;
816         }
817         if (priv->local_id) {
818                 g_free (priv->local_id);
819                 priv->local_id = NULL;
820         }
821         if (priv->remote_id) {
822                 g_free (priv->remote_id);
823                 priv->remote_id = NULL;
824         }
825         if (priv->group_uid) {
826                 g_free (priv->group_uid);
827                 priv->group_uid = NULL;
828         }
829 }
830
831 static void
832 dispose (GObject *self)
833 {
834         ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
835
836         if (priv->init_timer) {
837                 g_source_remove (priv->init_timer);
838                 priv->init_timer = 0;
839         }
840
841         stop_scroll_anim (priv);
842         if (priv->idle_id) {
843                 g_source_remove (priv->idle_id);
844                 priv->idle_id = 0;
845         }
846         if (priv->eventlogger) {
847                 g_object_unref (priv->eventlogger);
848                 priv->eventlogger = NULL;
849         }
850         if (priv->font_desc) {
851                 pango_font_description_free (priv->font_desc);
852                 priv->font_desc = NULL;
853         }
854
855         clean_state (EL_HOME_APPLET (self));
856         clean_contact (EL_HOME_APPLET (self), FALSE);
857
858         G_OBJECT_CLASS (el_home_applet_parent_class)->dispose (self);
859 }
860
861 static void
862 finalize (GObject *self)
863 {
864         G_OBJECT_CLASS (el_home_applet_parent_class)->finalize (self);
865 }
866
867 static void
868 aggregator_ready_cb (OssoABookWaitable *waitable,
869                      const GError      *error,
870                      gpointer           userdata)
871 {
872         ELHomeApplet *self = EL_HOME_APPLET (userdata);
873         ELHomeAppletPrivate *priv = self->priv;
874
875         priv->aggregator_ready_closure = NULL;
876
877         if (error) {
878                 g_warning ("Failed to create aggregator: %s", error->message);
879                 return;
880         }
881
882         g_signal_connect (priv->aggregator,
883                           "contacts-added",
884                           G_CALLBACK (contacts_added),
885                           self);
886         g_signal_connect (priv->aggregator,
887                           "contacts-removed",
888                           G_CALLBACK (contacts_removed),
889                           self);
890
891         resolve_contact (self);
892 }
893
894 static void
895 start_aggregator (ELHomeApplet *self)
896 {
897         ELHomeAppletPrivate *priv = self->priv;
898         EBookQuery *query = NULL;
899         GError *error = NULL;
900
901         if (priv->local_id && priv->remote_id) {
902                 const gchar *vcard = osso_abook_account_manager_get_vcard_field
903                         (NULL, priv->local_id);
904                 if (vcard)
905                         query = e_book_query_vcard_field_test (vcard,
906                                                                E_BOOK_QUERY_IS,
907                                                                priv->remote_id);
908                 else
909                         query = e_book_query_any_field_contains (priv->remote_id);
910         }
911
912         if (query) {
913                 priv->aggregator = osso_abook_aggregator_new_with_query (NULL,
914                                                                          query,
915                                                                          NULL,
916                                                                          1,
917                                                                          &error);
918                 e_book_query_unref (query);
919         }
920         if (error) {
921                 g_warning ("Failed to create aggregator: %s", error->message);
922                 g_error_free (error);
923                 return;
924         }
925
926         if (priv->aggregator) {
927                 priv->aggregator_ready_closure = osso_abook_waitable_call_when_ready
928                         (OSSO_ABOOK_WAITABLE (priv->aggregator),
929                          aggregator_ready_cb,
930                          self, NULL);
931
932                 osso_abook_roster_start (priv->aggregator);
933         }
934 }
935
936 static gchar*
937 format_time (time_t t, gboolean time_fmt_24h)
938 {
939         static const guint RESULT_SIZE = 64;
940
941         time_t now;
942         struct tm now_tm, t_tm;
943         const gchar *time_format;
944         gchar *result = g_malloc0 (RESULT_SIZE);
945
946         now = time (NULL);
947         localtime_r (&now, &now_tm);
948         localtime_r (&t, &t_tm);
949
950         if (time_fmt_24h)
951                 time_format = "wdgt_va_24h_time";
952         else
953                 time_format = now_tm.tm_hour > 11 ?
954                         "wdgt_va_12h_time_pm":
955                         "wdgt_va_12h_time_am";
956
957         if ((now_tm.tm_year == t_tm.tm_year) &&
958             (now_tm.tm_mon  == t_tm.tm_mon) &&
959             (now_tm.tm_mday == t_tm.tm_mday))
960                 strftime (result,
961                           RESULT_SIZE,
962                           dgettext ("hildon-libs", time_format),
963                           &t_tm);
964         else {
965                 gchar *full_format = g_strdup_printf ("%s %s",
966                                                       dgettext ("hildon-libs", "wdgt_va_date"),
967                                                       dgettext ("hildon-libs", time_format));
968                 strftime (result, RESULT_SIZE, full_format, &t_tm);
969                 g_free (full_format);
970         }
971
972         return result;
973 }
974
975 static void
976 show_event (ELHomeApplet *self, RTComElIter *it)
977 {
978         ELHomeAppletPrivate *priv = self->priv;
979         gchar *remote = NULL;
980         gchar *received = NULL;
981
982         if (it && rtcom_el_iter_first (it)) {
983                 time_t received_t;
984
985                 if (rtcom_el_iter_get_values (it,
986                                               "id", &priv->event_id,
987                                               "start-time", &received_t,
988                                               "local-uid", &priv->local_id,
989                                               "remote-uid", &priv->remote_id,
990                                               "remote-name", &remote,
991                                               "remote-ebook-uid", &priv->contact_id,
992                                               "free-text", &priv->message,
993                                               "group-uid", &priv->group_uid,
994                                               NULL)) {
995                         received = format_time (received_t, priv->time_fmt_24h);
996
997                         if (priv->remote_id && !priv->remote_id[0]) {
998                                 g_free (priv->remote_id);
999                                 priv->remote_id = NULL;
1000                         }
1001                 }
1002                 else
1003                         priv->event_id = -1;
1004         }
1005         else
1006                 priv->event_id = -1;
1007
1008         if (priv->message)
1009                 gtk_widget_hide (priv->empty);
1010         else
1011                 gtk_widget_show (priv->empty);
1012
1013         gtk_label_set_text (GTK_LABEL (priv->received), received);
1014
1015         if (remote && remote[0])
1016                 gtk_label_set_text (GTK_LABEL (priv->sender), remote);
1017         else
1018                 gtk_label_set_text (GTK_LABEL (priv->sender), priv->remote_id);
1019
1020         stop_scroll_anim (priv);
1021         priv->scroll_offset = 0;
1022         if (priv->message_surface) {
1023                 cairo_surface_destroy (priv->message_surface);
1024                 priv->message_surface = NULL;
1025         }
1026
1027         gtk_widget_hide (priv->cut_message);
1028         gtk_widget_queue_draw (GTK_WIDGET (self));
1029
1030         g_free (remote);
1031 }
1032
1033 static RTComElIter*
1034 make_query (RTComEl *el, gint event_id)
1035 {
1036         RTComElQuery *query = NULL;
1037         RTComElIter *it = NULL;
1038
1039         query = rtcom_el_query_new (el);
1040         rtcom_el_query_set_limit (query, 1);
1041         if (event_id >= 0) {
1042                 rtcom_el_query_prepare (query,
1043                                         "id", event_id, RTCOM_EL_OP_EQUAL,
1044                                         NULL);
1045         }
1046         else {
1047                 rtcom_el_query_prepare (query,
1048                                         "is-read", FALSE, RTCOM_EL_OP_EQUAL,
1049                                         "outgoing", FALSE, RTCOM_EL_OP_EQUAL,
1050                                         "service", conv_services, RTCOM_EL_OP_IN_STRV,
1051                                         "event-type", conv_event_types, RTCOM_EL_OP_IN_STRV,
1052                                         NULL);
1053         }
1054         it = rtcom_el_get_events (el, query);
1055         g_object_unref (query);
1056
1057         return it;
1058 }
1059
1060 static void
1061 update_unread_label (ELHomeApplet *self)
1062 {
1063         ELHomeAppletPrivate *priv = self->priv;
1064
1065         if (priv->unread_count > 0) {
1066                 gchar *text;
1067                 text = g_strdup_printf
1068                         ("%d<span foreground=\"red\" rise=\"5000\">*</span>",
1069                          priv->unread_count);
1070
1071                 gtk_label_set_markup (GTK_LABEL (priv->unread), text);
1072                 g_free (text);
1073         }
1074         else
1075                 gtk_label_set_text (GTK_LABEL (priv->unread), NULL);
1076 }
1077
1078 static gint
1079 query_unread_events (RTComEl *el)
1080 {
1081         sqlite3 *db;
1082         sqlite3_stmt *stmt;
1083         int ret;
1084         gint count = 0;
1085
1086         g_object_get (el, "db", &db, NULL);
1087
1088         if (sqlite3_prepare_v2 (db,
1089                                 "SELECT SUM(total_events)-SUM(read_events) FROM GroupCache;",
1090                                 -1,
1091                                 &stmt,
1092                                 NULL) != SQLITE_OK) {
1093                 g_error ("%s: can't compile SQL", G_STRFUNC);
1094                 return -1;
1095         }
1096
1097         while (SQLITE_BUSY == (ret = sqlite3_step (stmt)));
1098
1099         if (ret == SQLITE_ROW) {
1100                 count = sqlite3_column_int (stmt, 0);
1101         }
1102         else {
1103                 g_error ("%s: error while executing SQL", G_STRFUNC);
1104         }
1105
1106         sqlite3_finalize (stmt);
1107
1108         return count;
1109 }
1110
1111 static gboolean
1112 query_read_events (RTComEl *el, const gchar *service, gint *events, gint *conversations)
1113 {
1114         sqlite3 *db;
1115         sqlite3_stmt *stmt;
1116         int ret;
1117         gboolean result = TRUE;
1118
1119         g_object_get (el, "db", &db, NULL);
1120
1121         if (sqlite3_prepare_v2 (db,
1122                                 "SELECT SUM(total_events), COUNT(group_uid) FROM GroupCache, Services "
1123                                 "WHERE GroupCache.service_id=Services.id AND Services.name=?;",
1124                                 -1,
1125                                 &stmt,
1126                                 NULL) != SQLITE_OK) {
1127                 g_error ("%s: can't compile SQL", G_STRFUNC);
1128                 return FALSE;
1129         }
1130         if (sqlite3_bind_text (stmt, 1, service, -1, SQLITE_STATIC) != SQLITE_OK)  {
1131                 g_error ("Failed to bind %s to SQL stmt", service);
1132                 result = FALSE;
1133                 goto DONE;
1134         }
1135
1136         while (SQLITE_BUSY == (ret = sqlite3_step (stmt)));
1137
1138         if (ret == SQLITE_ROW) {
1139                 *events = sqlite3_column_int (stmt, 0);
1140                 *conversations = sqlite3_column_int (stmt, 1);
1141         }
1142         else {
1143                 g_error ("%s: error while executing SQL", G_STRFUNC);
1144                 result = FALSE;
1145                 goto DONE;
1146         }
1147
1148  DONE:
1149         sqlite3_finalize (stmt);
1150
1151         return result;
1152 }
1153
1154 static void
1155 am_ready (OssoABookAccountManager *manager,
1156           const GError            *error,
1157           gpointer                 user_data)
1158 {
1159         ELHomeApplet *self = EL_HOME_APPLET (user_data);
1160         ELHomeAppletPrivate *priv = self->priv;
1161
1162         if (!error &&
1163             priv->local_id &&
1164             !GTK_WIDGET_VISIBLE (priv->icon)) {
1165                 McAccount *account;
1166
1167                 account = osso_abook_account_manager_lookup_by_name (NULL,
1168                                                                      priv->local_id);
1169                 if (account) {
1170                         McProfile *profile = mc_profile_lookup (mc_account_compat_get_profile (account));
1171                         const gchar *icon_name = mc_profile_get_icon_name (profile);
1172                         if (icon_name) {
1173                                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
1174                                                               icon_name,
1175                                                               HILDON_ICON_SIZE_XSMALL);
1176                                 gtk_widget_show (priv->icon);
1177                         }
1178                 }
1179         }
1180 }
1181
1182 static void
1183 read_event (ELHomeApplet *self)
1184 {
1185         ELHomeAppletPrivate *priv = self->priv;
1186         RTComElIter *it = NULL;
1187         const gchar *icon_name = NULL;
1188         gchar *remote_id;
1189         gchar *local_id;
1190
1191         remote_id = g_strdup (priv->remote_id);
1192         local_id = g_strdup (priv->local_id);
1193
1194         clean_state (self);
1195
1196         it = make_query (priv->eventlogger, -1);
1197         show_event (self, it);
1198
1199         if (it) g_object_unref (it);
1200
1201         if (priv->event_id >= 0) {
1202                 gboolean new_account = g_strcmp0 (priv->local_id, local_id);
1203
1204                 if (g_strcmp0 (priv->remote_id, remote_id) ||
1205                     new_account ||
1206                     !priv->contact) {
1207                         clean_contact (self, TRUE);
1208                         start_aggregator (self);
1209                 }
1210                 else if (priv->contact) {
1211                         show_contact (self);
1212                 }
1213
1214                 if (new_account) {
1215                         if (g_strcmp0 (priv->local_id, "ring/tel/ring") == 0) {
1216                                 icon_name = "general_sms";
1217                         }
1218                         else {
1219                                 McAccount *account;
1220                                 OssoABookAccountManager *am = osso_abook_account_manager_get_default ();
1221                                 if (!osso_abook_waitable_is_ready (OSSO_ABOOK_WAITABLE (am), NULL)) {
1222                                         osso_abook_account_manager_call_when_ready  (am,
1223                                                                                      am_ready,
1224                                                                                      self,
1225                                                                                      NULL);
1226                                 }
1227                                 else {
1228                                         account = osso_abook_account_manager_lookup_by_name (NULL,
1229                                                                                              priv->local_id);
1230                                         if (account) {
1231                                                 McProfile *profile = mc_profile_lookup (mc_account_compat_get_profile (account));
1232                                                 icon_name = mc_profile_get_icon_name (profile);
1233                                         }
1234                                 }
1235                         }
1236
1237                         if (icon_name) {
1238                                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
1239                                                               icon_name,
1240                                                               HILDON_ICON_SIZE_XSMALL);
1241                                 gtk_widget_show (priv->icon);
1242                         }
1243                         else
1244                                 gtk_widget_hide (priv->icon);
1245                 }
1246         }
1247         else {
1248                 gchar *text;
1249                 gint n_sms_events = 0, n_sms_convs = 0;
1250                 gint n_chat_events = 0, n_chat_convs = 0;
1251                 const gchar *fmt = "%d <span size=\"small\">(%d)</span>";
1252
1253                 query_read_events (priv->eventlogger,
1254                                    "RTCOM_EL_SERVICE_SMS",
1255                                    &n_sms_events, &n_sms_convs);
1256                 query_read_events (priv->eventlogger,
1257                                    "RTCOM_EL_SERVICE_CHAT",
1258                                    &n_chat_events, &n_chat_convs);
1259
1260                 text = g_strdup_printf (fmt, n_sms_convs, n_sms_events);
1261                 gtk_label_set_markup (GTK_LABEL (priv->sms_total), text);
1262                 g_free (text);
1263
1264                 text = g_strdup_printf (fmt, n_chat_convs, n_chat_events);
1265                 gtk_label_set_markup (GTK_LABEL (priv->chat_total), text);
1266                 g_free (text);
1267
1268                 gtk_label_set_text (GTK_LABEL (priv->sender),
1269                                     dgettext ("rtcom-messaging-ui",
1270                                               "messaging_ap_conversations"));
1271
1272                 clean_contact (self, TRUE);
1273                 gtk_widget_hide (priv->icon);
1274         }
1275
1276         g_free (local_id);
1277         g_free (remote_id);
1278 }
1279
1280 static void
1281 remove_notification (ELHomeApplet *self)
1282 {
1283         ELHomeAppletPrivate *priv = self->priv;
1284
1285         DBusGConnection *conn;
1286         GError *error;
1287         DBusGProxy *proxy;
1288         GPtrArray *conv_structs;
1289         GType conv_structs_type;
1290         GValueArray *account_info;
1291         GValue value = {0, };
1292         DBusGProxyCall *call;
1293
1294         if (!(priv->remote_id && priv->local_id))
1295                 return;
1296
1297         conn = hd_home_plugin_item_get_dbus_g_connection (HD_HOME_PLUGIN_ITEM (self),
1298                                                           DBUS_BUS_SESSION,
1299                                                           &error);
1300         if (!conn) {
1301                 g_error ("Failed get dbus g connection %s", error->message);
1302                 g_error_free (error);
1303                 return;
1304         }
1305
1306         proxy = dbus_g_proxy_new_for_name (conn,
1307                                            NOTIFICATION_UI_DBUS_NAME,
1308                                            NOTIFICATION_UI_DBUS_PATH,
1309                                            NOTIFICATION_UI_DBUS_IFACE);
1310
1311         conv_structs = g_ptr_array_sized_new (1);
1312         account_info = g_value_array_new (2);
1313
1314         g_value_init (&value, G_TYPE_STRING);
1315         g_value_set_string (&value, priv->local_id);
1316         g_value_array_append (account_info, &value);
1317         g_value_unset (&value);
1318
1319         g_value_init (&value, G_TYPE_STRING);
1320         g_value_set_string (&value, priv->remote_id);
1321         g_value_array_append (account_info, &value);
1322         g_value_unset (&value);
1323
1324         g_ptr_array_add (conv_structs, account_info);
1325
1326         conv_structs_type = dbus_g_type_get_collection
1327                 ("GPtrArray",
1328                  dbus_g_type_get_struct ("GValueArray",
1329                                          G_TYPE_STRING,
1330                                          G_TYPE_STRING,
1331                                          G_TYPE_INVALID));
1332
1333         call = dbus_g_proxy_begin_call (proxy,
1334                                         "ClearConversationNotifications",
1335                                         NULL, NULL, NULL,
1336                                         conv_structs_type,
1337                                         conv_structs,
1338                                         G_TYPE_INVALID);
1339
1340         g_value_array_free (account_info);
1341         g_ptr_array_free (conv_structs, TRUE);
1342
1343         g_object_unref (proxy);
1344 }
1345
1346 static void
1347 mark_as_read (ELHomeApplet *self)
1348 {
1349         ELHomeAppletPrivate *priv = self->priv;
1350
1351         if (priv->event_id >= 0) {
1352                 rtcom_el_set_read_event (priv->eventlogger,
1353                                          priv->event_id,
1354                                          TRUE,
1355                                          NULL);
1356                 remove_notification (self);
1357         }
1358 }
1359
1360 static void
1361 launch_conversations (ELHomeApplet *self)
1362 {
1363         DBusConnection *conn;
1364         DBusMessage *message;
1365         DBusError error;
1366
1367         dbus_error_init (&error);
1368         conn = hd_home_plugin_item_get_dbus_connection (HD_HOME_PLUGIN_ITEM (self),
1369                                                         DBUS_BUS_SESSION,
1370                                                         &error);
1371         if (!conn) {
1372                 if (dbus_error_is_set (&error)) {
1373                         g_error ("Failed to get dbus connection %s", error.message);
1374                         dbus_error_free (&error);
1375                 }
1376                 return;
1377         }
1378
1379         message = dbus_message_new_method_call (CONVERSATIONS_UI_DBUS_NAME,
1380                                                 CONVERSATIONS_UI_DBUS_PATH,
1381                                                 CONVERSATIONS_UI_DBUS_IFACE,
1382                                                 "top_application");
1383         dbus_message_set_no_reply (message, TRUE);
1384
1385         if (dbus_connection_send (conn, message, NULL))
1386                 dbus_connection_flush (conn);
1387         dbus_message_unref (message);
1388
1389         dbus_connection_close (conn);
1390 }
1391
1392 static void
1393 open_conversation (ELHomeApplet *self)
1394 {
1395         ELHomeAppletPrivate *priv = self->priv;
1396         McAccount *account;
1397         const gchar *persistent_id = NULL;
1398
1399         if (!((priv->remote_id || priv->group_uid) && priv->local_id))
1400                 return;
1401
1402         account = osso_abook_account_manager_lookup_by_name (NULL,
1403                                                              priv->local_id);
1404         if (!account)
1405                 return;
1406
1407         if (priv->group_uid &&
1408             g_str_has_prefix (priv->group_uid, "group:")) {
1409                 persistent_id = strchr (priv->group_uid, '-');
1410                 if (persistent_id)
1411                         persistent_id++;
1412         }
1413
1414         if (persistent_id && persistent_id[0] != '\0') {
1415                 GHashTable *properties = tp_asv_new
1416                         (TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
1417                          TP_IFACE_CHANNEL_TYPE_TEXT,
1418                          TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT,
1419                          TP_HANDLE_TYPE_NONE,
1420                          RTCOM_TP_IFACE_CHANNEL_INTERFACE_PERSISTENT ".PersistentID",
1421                          G_TYPE_STRING, persistent_id,
1422                          NULL);
1423
1424                 mc_account_channelrequest_ht (account,
1425                                               properties,
1426                                               time (NULL),
1427                                               NULL,
1428                                               MC_ACCOUNT_CR_FLAG_USE_EXISTING,
1429                                               NULL, NULL, NULL, NULL);
1430
1431                 g_hash_table_unref (properties);
1432         }
1433         else if (priv->remote_id) {
1434                 McAccountChannelrequestData request;
1435
1436                 MC_ACCOUNT_CRD_INIT (&request);
1437                 MC_ACCOUNT_CRD_SET (&request, channel_type, TP_IFACE_QUARK_CHANNEL_TYPE_TEXT);
1438                 MC_ACCOUNT_CRD_SET (&request, target_handle_type, TP_HANDLE_TYPE_CONTACT);
1439                 MC_ACCOUNT_CRD_SET (&request, target_id, priv->remote_id);
1440
1441                 mc_account_channelrequest (account,
1442                                            &request,
1443                                            time (NULL),
1444                                            NULL,
1445                                            MC_ACCOUNT_CR_FLAG_USE_EXISTING,
1446                                            NULL, NULL, NULL, NULL);
1447         }
1448 }
1449
1450 static gboolean
1451 read_new_event (ELHomeApplet *self)
1452 {
1453         ELHomeAppletPrivate *priv = self->priv;
1454
1455         read_event (self);
1456
1457         if (priv->event_id >= 0)
1458                 priv->unread_count = query_unread_events (priv->eventlogger);
1459         else
1460                 priv->unread_count = 0;
1461
1462         update_unread_label (self);
1463
1464         priv->idle_id = 0;
1465
1466         return FALSE;
1467 }
1468
1469 static void
1470 add_new_idle (ELHomeApplet *self)
1471 {
1472         ELHomeAppletPrivate *priv = self->priv;
1473
1474         if (priv->idle_id)
1475                 g_source_remove (priv->idle_id);
1476         priv->idle_id = g_idle_add ((GSourceFunc)read_new_event,
1477                                     self);
1478 }
1479
1480 static void
1481 new_event_cb (RTComEl      *backend,
1482               gint          event_id,
1483               const gchar  *local_uid,
1484               const gchar  *remote_uid,
1485               const gchar  *remote_ebook_uid,
1486               const gchar  *group_uid,
1487               const gchar  *service,
1488               ELHomeApplet *self)
1489 {
1490         if (service && service[0] != '\0') {
1491                 const gchar** conv_service = conv_services;
1492                 do {
1493                         if (!g_strcmp0 (*conv_service, service)) {
1494                                 add_new_idle (self);
1495                                 return;
1496                         }
1497                 }
1498                 while(*++conv_service);
1499         }
1500         else
1501                 add_new_idle (self);
1502 }
1503
1504 static void
1505 all_deleted_cb (RTComEl      *backend,
1506                 const gchar  *service,
1507                 ELHomeApplet *self)
1508 {
1509         new_event_cb (backend,
1510                       0,
1511                       NULL,
1512                       NULL,
1513                       NULL,
1514                       NULL,
1515                       service,
1516                       self);
1517 }
1518
1519 static void
1520 refresh_hint_cb (RTComEl     *backend,
1521                  ELHomeApplet *self)
1522 {
1523         add_new_idle (self);
1524 }
1525
1526 static gboolean
1527 scroll_anim_cb (ELHomeApplet *self)
1528 {
1529         ELHomeAppletPrivate *priv = self->priv;
1530         gboolean to_continue;
1531
1532         priv->scroll_offset += SCROLL_STEP;
1533         gtk_widget_queue_draw_area (GTK_WIDGET (self),
1534                                     3*CONTENT_OFFSET_X,
1535                                     HEADER_HEIGHT + CONTENT_OFFSET_Y_TOP,
1536                                     MESSAGE_WIDTH,
1537                                     MESSAGE_HEIGHT);
1538
1539         to_continue = priv->scroll_offset <= priv->hidden_message_height;
1540         if (!to_continue) {
1541                 priv->scroll_anim_id = 0;
1542                 gtk_widget_hide (priv->cut_message);
1543         }
1544
1545         return to_continue;
1546 }
1547
1548 static gboolean
1549 button_press_event_cb (GtkWidget      *widget,
1550                        GdkEventButton *event,
1551                        ELHomeApplet   *self)
1552 {
1553         ELHomeAppletPrivate *priv = self->priv;
1554
1555         if (priv->event_id >= 0) {
1556                 if (event->y < CONTENT_OFFSET_Y_TOP + HEADER_HEIGHT) {
1557                         if (priv->aggregator &&
1558                             osso_abook_waitable_is_ready
1559                             (OSSO_ABOOK_WAITABLE (priv->aggregator), NULL))
1560                                 priv->active = SELECTED_HEADER;
1561                 }
1562                 else if (event->y > (BOX_HEIGHT - CONTENT_OFFSET_Y_BOTTOM - FOOTER_HEIGHT_PRESS) &&
1563                          event->x < FOOTER_WIDTH_PRESS)
1564                         priv->active = SELECTED_FOOTER;
1565                 else
1566                         priv->active = SELECTED_BODY;
1567         }
1568         else {
1569                 priv->active = SELECTED_BODY;
1570         }
1571
1572         gtk_widget_queue_draw (widget);
1573
1574         return TRUE;
1575 }
1576
1577 static GtkWidget*
1578 create_contact_starter_dialog (OssoABookAggregator *aggregator, const gchar *contact_id)
1579 {
1580         GtkWidget *dialog = NULL;
1581         GList *contacts = osso_abook_aggregator_lookup (aggregator, contact_id);
1582         if (contacts && contacts->data) {
1583                 GtkWidget *starter =
1584                         osso_abook_touch_contact_starter_new_with_contact
1585                         (NULL,
1586                          OSSO_ABOOK_CONTACT (contacts->data));
1587                 dialog = osso_abook_touch_contact_starter_dialog_new
1588                         (NULL,
1589                          OSSO_ABOOK_TOUCH_CONTACT_STARTER (starter));
1590                 gtk_widget_show_all (starter);
1591         }
1592
1593         g_list_free (contacts);
1594
1595         return dialog;
1596 }
1597
1598 static GtkWidget*
1599 create_temporary_contact_dialog (const gchar *remote_id,
1600                                  const gchar *account_id)
1601 {
1602         GtkWidget *dialog = NULL;
1603         const gchar *vcard = NULL;
1604         McAccount *account = NULL;
1605
1606         if (account_id) {
1607             vcard = osso_abook_account_manager_get_vcard_field (NULL, account_id);
1608             account = osso_abook_account_manager_lookup_by_name (NULL, account_id);
1609         }
1610
1611         if (vcard && account) {
1612                 EVCardAttribute *attribute = e_vcard_attribute_new (NULL, vcard);
1613
1614                 e_vcard_attribute_add_value (attribute, remote_id);
1615                 dialog = osso_abook_temporary_contact_dialog_new
1616                         (NULL,
1617                          NULL, /*EBook            *book,*/
1618                          attribute,
1619                          account);
1620                 g_signal_connect (dialog,
1621                                   "response",
1622                                   G_CALLBACK (gtk_widget_destroy),
1623                                   NULL);
1624                 e_vcard_attribute_free (attribute);
1625         }
1626
1627         return dialog;
1628 }
1629
1630 static gboolean
1631 button_release_event_cb (GtkWidget      *widget,
1632                          GdkEventButton *event,
1633                          ELHomeApplet   *self)
1634 {
1635         ELHomeAppletPrivate *priv = self->priv;
1636
1637         switch (priv->active) {
1638         case SELECTED_BODY:
1639                 if (priv->event_id >= 0) {
1640                         reset_scroll (self);
1641                         open_conversation (self);
1642                 }
1643                 else
1644                         launch_conversations (self);
1645                 break;
1646         case SELECTED_HEADER: {
1647                 GtkWidget *dialog = NULL;
1648
1649                 reset_scroll (self);
1650
1651                 if (priv->aggregator && priv->contact_id)
1652                         dialog = create_contact_starter_dialog
1653                                 (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
1654                                  priv->contact_id);
1655                 if (!dialog &&
1656                     priv->remote_id &&
1657                     priv->local_id)
1658                         dialog = create_temporary_contact_dialog (priv->remote_id,
1659                                                                   priv->local_id);
1660
1661                 if (dialog)
1662                         gtk_widget_show (dialog);
1663         }
1664                 break;
1665         case SELECTED_FOOTER:
1666                 if (priv->scroll_on_click) {
1667                         priv->scroll_on_click = FALSE;
1668                         priv->scroll_anim_id = g_timeout_add (SCROLL_PERIOD,
1669                                                               (GSourceFunc)scroll_anim_cb,
1670                                                               self);
1671                 }
1672                 else
1673                         mark_as_read (self);
1674                 break;
1675         default:;
1676         }
1677
1678         priv->active = SELECTED_NONE;
1679         gtk_widget_queue_draw (widget);
1680
1681         return TRUE;
1682 }
1683
1684 static gboolean
1685 leave_notify_event_cb (GtkWidget        *widget,
1686                        GdkEventCrossing *event,
1687                        ELHomeApplet     *self)
1688 {
1689         ELHomeAppletPrivate *priv = self->priv;
1690
1691         switch (priv->active) {
1692         case SELECTED_FOOTER:
1693                 stop_scroll_anim (priv);
1694                 /* fall down */
1695         case SELECTED_HEADER:
1696         case SELECTED_BODY:
1697                 gtk_widget_queue_draw (widget);
1698                 break;
1699         default:;
1700         }
1701
1702         priv->active = SELECTED_NONE;
1703         return FALSE;
1704 }
1705
1706 static gboolean
1707 init_eventlogger (ELHomeApplet *self)
1708 {
1709         ELHomeAppletPrivate *priv = self->priv;
1710
1711         priv->eventlogger = rtcom_el_new ();
1712
1713         /* check that db is initialized */
1714         gpointer *db = NULL;
1715         g_object_get (priv->eventlogger, "db", &db, NULL);
1716         if (!db) {
1717                 static int trial = 0;
1718
1719                 g_object_unref (priv->eventlogger);
1720                 priv->eventlogger = NULL;
1721
1722                 if (trial == 0) {
1723                         trial++;
1724                         priv->init_timer = g_timeout_add_seconds (5,
1725                                                                   (GSourceFunc)init_eventlogger,
1726                                                                   self);
1727                         return TRUE; /* return value doesn't matter */
1728                 }
1729                 else if (trial < 5) {
1730                         trial++;
1731                         return TRUE;
1732                 }
1733                 else {
1734                         g_error ("Failed to init eventlogger");
1735                         return FALSE;
1736                 }
1737         }
1738
1739         g_signal_connect (priv->eventlogger,
1740                           "new-event",
1741                           G_CALLBACK (new_event_cb),
1742                           self);
1743         g_signal_connect (priv->eventlogger,
1744                           "event-updated",
1745                           G_CALLBACK (new_event_cb),
1746                           self);
1747         g_signal_connect (priv->eventlogger,
1748                           "event-deleted",
1749                           G_CALLBACK (new_event_cb),
1750                           self);
1751         g_signal_connect (priv->eventlogger,
1752                           "all-deleted",
1753                           G_CALLBACK (all_deleted_cb),
1754                           self);
1755         g_signal_connect (priv->eventlogger,
1756                           "refresh-hint",
1757                           G_CALLBACK (refresh_hint_cb),
1758                           self);
1759
1760         add_new_idle (self);
1761
1762         priv->init_timer = 0;
1763
1764         return FALSE;
1765 }
1766
1767 static void
1768 el_home_applet_init (ELHomeApplet *self)
1769 {
1770         ELHomeAppletPrivate *priv;
1771         GtkWidget *event_box;
1772         GtkWidget *hbox, *vbox, *align, *footer;
1773         GtkWidget *w;
1774         GConfClient *gconf;
1775
1776         self->priv = EL_HOME_APPLET_GET_PRIVATE (self);
1777         priv = self->priv;
1778
1779         gtk_widget_set_app_paintable (GTK_WIDGET (self), TRUE);
1780
1781         priv->unread = gtk_label_new (NULL);
1782         gtk_misc_set_alignment (GTK_MISC (priv->unread),
1783                                 0.0f,
1784                                 0.5f);
1785         hildon_helper_set_logical_font (priv->unread, "SmallSystemFont");
1786
1787         priv->icon = gtk_image_new ();
1788         gtk_misc_set_alignment (GTK_MISC (priv->icon),
1789                                 0.5f,
1790                                 0.5f);
1791
1792         priv->sender = gtk_label_new (NULL);
1793         gtk_misc_set_alignment (GTK_MISC (priv->sender),
1794                                 0.5f,
1795                                 0.55f);
1796         gtk_label_set_ellipsize (GTK_LABEL (priv->sender),
1797                                  PANGO_ELLIPSIZE_END);
1798         gtk_widget_set_name (priv->sender, "hildon-shadow-label");
1799         hildon_helper_set_logical_font (priv->sender, "SystemFont");
1800         gtk_widget_set_size_request (priv->sender,
1801                                      C_WIDTH,
1802                                      HILDON_ICON_PIXEL_SIZE_THUMB);
1803
1804         /* construt empty table */
1805         priv->empty = gtk_fixed_new ();
1806
1807         w = gtk_image_new_from_icon_name ("general_sms", HILDON_ICON_SIZE_FINGER);
1808         gtk_fixed_put (GTK_FIXED (priv->empty), w,
1809                        4*HILDON_MARGIN_DOUBLE,
1810                        2*HILDON_MARGIN_DOUBLE);
1811
1812         w = gtk_image_new_from_icon_name ("general_chat", HILDON_ICON_SIZE_FINGER);
1813         gtk_fixed_put (GTK_FIXED (priv->empty), w,
1814                        4*HILDON_MARGIN_DOUBLE,
1815                        3*HILDON_MARGIN_DOUBLE + HILDON_ICON_PIXEL_SIZE_FINGER);
1816
1817         priv->sms_total = gtk_label_new (NULL);
1818         gtk_widget_set_name (priv->sms_total, "hildon-shadow-label");
1819         gtk_fixed_put (GTK_FIXED (priv->empty), priv->sms_total,
1820                        5*HILDON_MARGIN_DOUBLE  + HILDON_ICON_PIXEL_SIZE_FINGER,
1821                        2*HILDON_MARGIN_DOUBLE + HILDON_MARGIN_HALF);
1822
1823         priv->chat_total = gtk_label_new (NULL);
1824         gtk_widget_set_name (priv->chat_total, "hildon-shadow-label");
1825         gtk_fixed_put (GTK_FIXED (priv->empty), priv->chat_total,
1826                        5*HILDON_MARGIN_DOUBLE  + HILDON_ICON_PIXEL_SIZE_FINGER,
1827                        3*HILDON_MARGIN_DOUBLE + HILDON_MARGIN_HALF + HILDON_ICON_PIXEL_SIZE_FINGER);
1828
1829         gtk_widget_show_all (GTK_WIDGET (priv->empty));
1830         gtk_widget_hide (GTK_WIDGET (priv->empty));
1831         GTK_WIDGET_SET_FLAGS (priv->empty, GTK_NO_SHOW_ALL);
1832
1833         priv->received = gtk_label_new (NULL);
1834         gtk_misc_set_alignment (GTK_MISC (priv->received),
1835                                 1.0f,
1836                                 0.5f);
1837         hildon_helper_set_logical_font (priv->received, "SmallSystemFont");
1838         gtk_widget_set_name (priv->received, "hildon-shadow-label");
1839
1840
1841         priv->cut_message = gtk_label_new ("...");
1842         gtk_misc_set_alignment (GTK_MISC (priv->cut_message),
1843                                 0.5f,
1844                                 0.0f);
1845         hildon_helper_set_logical_font (priv->cut_message, "SmallSystemFont");
1846         gtk_widget_set_name (priv->cut_message, "hildon-shadow-label");
1847         GTK_WIDGET_SET_FLAGS (priv->cut_message, GTK_NO_SHOW_ALL);
1848
1849         hbox = gtk_hbox_new (FALSE, 0);
1850         gtk_box_pack_start (GTK_BOX (hbox), priv->sender, FALSE, FALSE, 0);
1851
1852         footer = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
1853         gtk_box_pack_start (GTK_BOX (footer), priv->unread, FALSE, FALSE, 0);
1854         gtk_box_pack_start (GTK_BOX (footer), priv->cut_message, TRUE, TRUE, 0);
1855         gtk_box_pack_end (GTK_BOX (footer), priv->icon, FALSE, FALSE, 0);
1856         gtk_box_pack_end (GTK_BOX (footer), priv->received, FALSE, FALSE, 0);
1857
1858         vbox = gtk_vbox_new (FALSE, 0);
1859         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1860         gtk_box_pack_start (GTK_BOX (vbox), priv->empty, TRUE, TRUE, 0);
1861         gtk_box_pack_end (GTK_BOX (vbox), footer, FALSE, FALSE, 0);
1862
1863         align = gtk_alignment_new (0.5f, 0.0f, 1.0f, 1.0f);
1864         gtk_alignment_set_padding (GTK_ALIGNMENT (align),
1865                                    0, 0, HILDON_MARGIN_DEFAULT, HILDON_MARGIN_DEFAULT);
1866
1867         gtk_container_set_border_width (GTK_CONTAINER (vbox), HILDON_MARGIN_HALF);
1868
1869         event_box = gtk_event_box_new ();
1870         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
1871         gtk_widget_set_size_request (event_box, BOX_WIDTH, BOX_HEIGHT);
1872
1873         gtk_container_add (GTK_CONTAINER (align), vbox);
1874         gtk_container_add (GTK_CONTAINER (event_box), align);
1875         gtk_container_add (GTK_CONTAINER (self), event_box);
1876
1877         g_signal_connect (event_box,
1878                           "button-press-event",
1879                           G_CALLBACK (button_press_event_cb),
1880                           self);
1881         g_signal_connect (event_box,
1882                           "button-release-event",
1883                           G_CALLBACK (button_release_event_cb),
1884                           self);
1885         g_signal_connect (event_box,
1886                           "leave-notify-event",
1887                           G_CALLBACK (leave_notify_event_cb),
1888                           self);
1889
1890         g_signal_connect (event_box,
1891                           "style-set",
1892                           G_CALLBACK (style_set_cb),
1893                           self);
1894         g_signal_connect (self,
1895                           "notify::is-on-current-desktop",
1896                           G_CALLBACK (notify_on_current_desktop),
1897                           self);
1898
1899         gtk_widget_show_all (GTK_WIDGET (event_box));
1900
1901
1902         osso_abook_init_with_name (PACKAGE, NULL);
1903
1904         gconf = gconf_client_get_default ();
1905         priv->time_fmt_24h = gconf_client_get_bool (gconf,
1906                                                     "/apps/clock/time-format",
1907                                                     NULL);
1908         g_object_unref (gconf);
1909
1910         init_eventlogger (self);
1911 }
1912
1913 static void
1914 el_home_applet_class_init (ELHomeAppletClass *klass)
1915 {
1916         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1917         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1918
1919         object_class->dispose = dispose;
1920         object_class->finalize = finalize;
1921         widget_class->expose_event = expose_event;
1922         widget_class->realize = el_home_applet_realize;
1923
1924         g_type_class_add_private (klass, sizeof (ELHomeAppletPrivate));
1925 }