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