Display received time
[conv-inbox] / src / el-home-applet.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright (C) 2009 Artem Garmash. All rights reserved.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Contact: Artem Garmash <artemgarmash@gmail.com>
20  *
21  */
22
23 #include "config.h"
24 #include "el-home-applet.h"
25
26 #include <hildon/hildon.h>
27 #include <rtcom-eventlogger/eventlogger.h>
28 #include <sqlite3.h>
29 #include <string.h>
30
31 #define EL_HOME_APPLET_GET_PRIVATE(obj) ( \
32         G_TYPE_INSTANCE_GET_PRIVATE (obj, \
33                 EL_TYPE_HOME_APPLET, ELHomeAppletPrivate))
34
35 #define BOX_WIDTH 352
36 #define BOX_HEIGHT 252
37
38 #define C_WIDTH (BOX_WIDTH - 2*HILDON_MARGIN_HALF)
39 #define C_HEIGHT (BOX_HEIGHT - 2*HILDON_MARGIN_HALF)
40 #define C_X HILDON_MARGIN_HALF
41 #define C_Y HILDON_MARGIN_HALF
42
43 #define HEADER_HEIGHT 48
44 #define MESSAGE_HEIGHT (C_HEIGHT - HEADER_HEIGHT)
45 #define MESSAGE_WIDTH (C_WIDTH - 2*HILDON_MARGIN_DEFAULT)
46
47 #define BOX_RADIOUS 10
48
49 struct _ELHomeAppletPrivate
50 {
51         RTComEl *eventlogger;
52
53         GtkWidget *sender;
54         GtkWidget *message;
55         GtkWidget *icon;
56         GtkWidget *unread;
57         GtkWidget *received;
58
59         gint       event_id;
60
61         gboolean   active;
62
63         guint unread_count;
64
65         const gchar *current_font;
66 };
67
68 HD_DEFINE_PLUGIN_MODULE (ELHomeApplet, el_home_applet, HD_TYPE_HOME_PLUGIN_ITEM);
69
70 const gchar* g_module_check_init(GModule *module);
71 const gchar*
72 g_module_check_init(GModule *module)
73 {
74         g_module_make_resident (module);
75         return NULL;
76 }
77
78 static void
79 el_home_applet_class_finalize (ELHomeAppletClass *klass)
80 {
81 }
82
83 static void
84 el_home_applet_realize (GtkWidget *widget)
85 {
86         GdkScreen *screen;
87
88         screen = gtk_widget_get_screen (widget);
89         gtk_widget_set_colormap (widget,
90                                  gdk_screen_get_rgba_colormap (screen));
91
92         gtk_widget_set_app_paintable (widget,
93                                       TRUE);
94
95         GTK_WIDGET_CLASS (el_home_applet_parent_class)->realize (widget);
96 }
97
98 /*
99  * Thanks http://cairographics.org/cookbook/roundedrectangles/
100  */
101 static void
102 rounded_rectangle (cairo_t *cr,
103                    double   x,
104                    double   y,
105                    double   w,
106                    double   h,
107                    double   r)
108 {
109         cairo_move_to (cr, x + r, y);
110         cairo_line_to (cr, x + w - r, y);
111         cairo_curve_to (cr, x + w, y,
112                         x + w, y,
113                         x + w, y + r);
114         cairo_line_to (cr, x + w, y + h - r);
115         cairo_curve_to (cr, x + w, y + h,
116                         x + w, y + h,
117                         x + w - r, y + h);
118         cairo_line_to (cr, x + r, y + h);
119         cairo_curve_to (cr, x, y + h,
120                         x, y + h,
121                         x, y + h - r);
122         cairo_line_to (cr, x, y + r);
123         cairo_curve_to (cr, x, y,
124                         x, y,
125                         x + r, y);
126 }
127
128 static gboolean
129 expose_event (GtkWidget *self, GdkEventExpose *event)
130 {
131         ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
132
133         cairo_t *cr;
134         GdkColor color;
135         float red, green, blue;
136
137         /* find theme active color */
138         gtk_style_lookup_color (self->style, "ActiveTextColor", &color);
139         red = color.red/(float)G_MAXUINT16;
140         green = color.green/(float)G_MAXUINT16;
141         blue = color.blue/(float)G_MAXUINT16;
142
143         cr = gdk_cairo_create (self->window);
144         gdk_cairo_region (cr, event->region);
145         cairo_clip (cr);
146
147         cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
148
149         cairo_set_source_rgba (cr, 0.4f, 0.4f, 0.4f, 0.1f);
150         cairo_set_line_width (cr, 3.0f);
151
152         rounded_rectangle (cr,
153                            C_X,
154                            C_Y,
155                            BOX_WIDTH - 2*C_X,
156                            BOX_HEIGHT - 2*C_Y,
157                            BOX_RADIOUS);
158
159         cairo_close_path(cr);
160         cairo_stroke (cr);
161
162         cairo_set_line_width (cr, 1.0f);
163
164         cairo_translate (cr, C_X, C_Y);
165         cairo_move_to (cr, 0, HEADER_HEIGHT);
166         cairo_line_to (cr, 0, BOX_RADIOUS);
167         cairo_curve_to (cr, 0, 0, 0, 0, BOX_RADIOUS, 0);
168         cairo_line_to (cr, C_WIDTH - BOX_RADIOUS, 0);
169         cairo_curve_to (cr, C_WIDTH, 0, C_WIDTH, 0, C_WIDTH, BOX_RADIOUS);
170         cairo_line_to (cr, C_WIDTH, HEADER_HEIGHT);
171         cairo_line_to (cr, 0, HEADER_HEIGHT);
172
173         cairo_close_path(cr);
174
175         cairo_set_source_rgba (cr, 0.2f, 0.2f, 0.2f, 0.8f);
176         cairo_fill_preserve (cr);
177         cairo_set_source_rgba (cr, red, green, blue, 1.0f);
178         cairo_stroke (cr);
179
180         cairo_move_to (cr, 0, HEADER_HEIGHT);
181         cairo_line_to (cr, 0, C_HEIGHT - BOX_RADIOUS);
182         cairo_curve_to (cr, 0, C_HEIGHT, 0, C_HEIGHT, BOX_RADIOUS, C_HEIGHT);
183         cairo_line_to (cr, C_WIDTH - BOX_RADIOUS, C_HEIGHT);
184         cairo_curve_to (cr, C_WIDTH, C_HEIGHT, C_WIDTH, C_HEIGHT, C_WIDTH, C_HEIGHT - BOX_RADIOUS);
185         cairo_line_to (cr, C_WIDTH, HEADER_HEIGHT);
186         cairo_line_to (cr, 0, HEADER_HEIGHT);
187         cairo_close_path(cr);
188
189         if (priv->active)
190                 cairo_set_source_rgba (cr, red, green, blue, 0.8f);
191         else
192                 cairo_set_source_rgba (cr, 0.4f, 0.4f, 0.4f, 0.8f);
193         cairo_fill (cr);
194
195         /* cairo_set_source_rgba (cr, red, green, blue, 1.0f); */
196         /* cairo_translate (cr, -C_X, -C_Y); */
197         /* rounded_rectangle (cr, */
198         /*                    C_X, */
199         /*                    C_Y, */
200         /*                    BOX_WIDTH - 2*C_X, */
201         /*                    BOX_HEIGHT - 2*C_Y, */
202         /*                    BOX_RADIOUS); */
203         /* cairo_close_path(cr); */
204         /* cairo_stroke (cr); */
205
206         cairo_destroy (cr);
207
208         return GTK_WIDGET_CLASS (el_home_applet_parent_class)->expose_event (self, event);
209 }
210
211 static void
212 dispose (GObject *self)
213 {
214         ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
215
216         if (priv->eventlogger){
217                 g_object_unref (priv->eventlogger);
218                 priv->eventlogger = NULL;
219         }
220
221         G_OBJECT_CLASS (el_home_applet_parent_class)->dispose (self);
222 }
223
224 static void
225 finalize (GObject *self)
226 {
227         G_OBJECT_CLASS (el_home_applet_parent_class)->finalize (self);
228 }
229
230 static gchar*
231 format_time (time_t t)
232 {
233         static const guint RESULT_SIZE = 32;
234
235         time_t now;
236         struct tm now_tm, t_tm;
237         const gchar *format = "%Y.%m.%d %T";
238         gchar *result = g_malloc0 (RESULT_SIZE);
239
240         now = time (NULL);
241         localtime_r (&now, &now_tm);
242         localtime_r (&t, &t_tm);
243
244         if ((now_tm.tm_year == t_tm.tm_year) &&
245             (now_tm.tm_mon  == t_tm.tm_mon) &&
246             (now_tm.tm_mday == t_tm.tm_mday))
247                 format = "%T";
248
249         strftime (result, RESULT_SIZE, format, &t_tm);
250
251         return result;
252 }
253
254 static void
255 show_event (ELHomeApplet *self, RTComElIter *it)
256 {
257         ELHomeAppletPrivate *priv = self->priv;
258
259         gchar *message = NULL;
260         gchar *remote = NULL;
261         gchar *received = NULL;
262         const gchar *icon_name = NULL;
263
264         if (it && rtcom_el_iter_first (it)){
265                 rtcom_el_iter_dup_string (it, "free-text", &message);
266                 if (message){
267                         const gchar *service;
268                         time_t received_t;
269
270                         rtcom_el_iter_get_int (it, "id", &priv->event_id);
271                         if (rtcom_el_iter_get_int (it, "start-time", (gint*)&received_t))
272                                 received = format_time (received_t);
273
274                         if(!rtcom_el_iter_dup_string (it, "remote-name", &remote))
275                                 rtcom_el_iter_dup_string (it, "remote-id", &remote);
276                         service = rtcom_el_iter_get_service (it);
277                         if (!g_strcmp0 (service, "RTCOM_EL_SERVICE_SMS"))
278                                 icon_name = "chat_unread_sms";
279                         else if (!g_strcmp0 (service, "RTCOM_EL_SERVICE_CHAT"))
280                                 icon_name = "chat_unread_chat";
281                 }
282         }
283         else{
284                 priv->event_id = -1;
285         }
286
287         gtk_label_set_text (GTK_LABEL (priv->message), message);
288         gtk_label_set_text (GTK_LABEL (priv->sender), remote);
289         gtk_label_set_text (GTK_LABEL (priv->received), received);
290
291         if (icon_name){
292                 const gchar *current_icon_name;
293                 gtk_image_get_icon_name (GTK_IMAGE (priv->icon),
294                                          &current_icon_name,
295                                          NULL);
296                 if (g_strcmp0 (current_icon_name, icon_name))
297                         gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
298                                                       icon_name,
299                                                       HILDON_ICON_SIZE_FINGER);
300                 gtk_widget_show (priv->icon);
301         }
302         else
303                 gtk_widget_hide (priv->icon);
304
305         g_free (message);
306         g_free (remote);
307 }
308
309 static RTComElIter*
310 make_query (RTComEl *el, gint event_id)
311 {
312         RTComElQuery *query = NULL;
313         RTComElIter *it = NULL;
314
315         static const gchar *services[] = {"RTCOM_EL_SERVICE_SMS",
316                                           "RTCOM_EL_SERVICE_CHAT",
317                                           NULL};
318         static const gchar *event_types[] = {"RTCOM_EL_EVENTTYPE_SMS_INBOUND",
319                                              "RTCOM_EL_EVENTTYPE_CHAT_INBOUND",
320                                              NULL};
321
322         query = rtcom_el_query_new (el);
323         rtcom_el_query_set_limit (query, 1);
324         if (event_id >= 0){
325                 rtcom_el_query_prepare (query,
326                                         "is-read", FALSE, RTCOM_EL_OP_EQUAL,
327                                         "id", event_id, RTCOM_EL_OP_EQUAL,
328                                         "service", services, RTCOM_EL_OP_IN_STRV,
329                                         "event-type", event_types, RTCOM_EL_OP_IN_STRV,
330                                         NULL);
331         }
332         else{
333                 rtcom_el_query_prepare (query,
334                                         "is-read", FALSE, RTCOM_EL_OP_EQUAL,
335                                         "service", services, RTCOM_EL_OP_IN_STRV,
336                                         "event-type", event_types, RTCOM_EL_OP_IN_STRV,
337                                         NULL);
338         }
339         it = rtcom_el_get_events (el, query);
340         g_object_unref(query);
341
342         return it;
343 }
344
345 static void
346 update_unread_label (ELHomeApplet *self)
347 {
348         ELHomeAppletPrivate *priv = self->priv;
349         gchar *text;
350
351         text = g_strdup_printf ("%d", priv->unread_count);
352         gtk_label_set_text (GTK_LABEL (priv->unread), text);
353         g_free (text);
354 }
355
356 static gint
357 query_unread_events (RTComEl *el)
358 {
359         sqlite3 *db;
360         sqlite3_stmt *stmt;
361         int ret;
362         gint count = 0;
363
364         g_object_get (el, "db", &db, NULL);
365
366         if (sqlite3_prepare_v2 (db,
367                                 "SELECT SUM(total_events)-SUM(read_events) FROM GroupCache;",
368                                 -1,
369                                 &stmt,
370                                 NULL) != SQLITE_OK){
371                 g_error ("%s: can't compile SQL", G_STRFUNC);
372                 return -1;
373         }
374
375         while (SQLITE_BUSY == (ret = sqlite3_step (stmt)));
376
377         if (ret == SQLITE_ROW){
378                 count = sqlite3_column_int (stmt, 0);
379         }
380         else{
381                 g_error ("%s: error while executing SQL", G_STRFUNC);
382         }
383
384         sqlite3_finalize (stmt);
385
386         return count;
387 }
388
389 static void
390 read_event (ELHomeApplet *self)
391 {
392         ELHomeAppletPrivate *priv = self->priv;
393         RTComElIter *it = NULL;
394
395         it = make_query (priv->eventlogger, -1);
396         show_event (self, it);
397         if (it) g_object_unref (it);
398 }
399
400 static void
401 mark_as_read (ELHomeApplet *self)
402 {
403         ELHomeAppletPrivate *priv = self->priv;
404
405         if (priv->event_id >= 0){
406                 rtcom_el_set_read_event (priv->eventlogger,
407                                          priv->event_id,
408                                          TRUE,
409                                          NULL);
410                 read_event (self);
411                 priv->unread_count--;
412                 update_unread_label (self);
413         }
414 }
415
416 static void
417 new_event_cb (RTComEl      *backend,
418               gint          event_id,
419               const gchar  *local_uid,
420               const gchar  *remote_uid,
421               const gchar  *remote_ebook_uid,
422               const gchar  *group_uid,
423               const gchar  *service,
424               ELHomeApplet *self)
425 {
426         ELHomeAppletPrivate *priv = self->priv;
427         RTComElIter *it = NULL;
428
429         it = make_query (priv->eventlogger, event_id);
430         if (it){
431                 if (rtcom_el_iter_first (it)){
432                         show_event (self, it);
433                         priv->unread_count++;
434                         update_unread_label (self);
435                 }
436                 g_object_unref (it);
437         }
438 }
439
440 static void
441 event_updated_cb (RTComEl      *backend,
442                   gint          event_id,
443                   const gchar  *local_uid,
444                   const gchar  *remote_uid,
445                   const gchar  *remote_ebook_uid,
446                   const gchar  *group_uid,
447                   const gchar  *service,
448                   ELHomeApplet *self)
449 {
450         ELHomeAppletPrivate *priv = self->priv;
451
452         if (event_id == priv->event_id)
453                 read_event (self);
454
455         priv->unread_count = query_unread_events (priv->eventlogger);
456         update_unread_label (self);
457 }
458
459 static gboolean
460 button_release_event_cb (GtkWidget      *widget,
461                          GdkEventButton *event,
462                          ELHomeApplet   *self)
463 {
464         ELHomeAppletPrivate *priv = self->priv;
465
466         if (priv->active){
467                 priv->active = FALSE;
468                 gtk_widget_queue_draw (widget);
469 #ifndef DEBUG_LAYOUT
470                 mark_as_read (self);
471 #endif
472         }
473
474         return TRUE;
475 }
476
477 static gboolean
478 button_press_event_cb (GtkWidget      *widget,
479                        GdkEventButton *event,
480                        ELHomeApplet   *self)
481 {
482         ELHomeAppletPrivate *priv = self->priv;
483
484         if (priv->event_id > 0){
485                 priv->active = TRUE;
486                 gtk_widget_queue_draw (widget);
487         }
488
489         return TRUE;
490 }
491
492 static gboolean
493 leave_notify_event_cb (GtkWidget        *widget,
494                        GdkEventCrossing *event,
495                        ELHomeApplet     *self)
496 {
497         ELHomeAppletPrivate *priv = self->priv;
498
499         if (priv->active){
500                 priv->active = FALSE;
501                 gtk_widget_queue_draw (widget);
502         }
503
504         return FALSE;
505 }
506
507 static void
508 el_home_applet_init (ELHomeApplet *self)
509 {
510         ELHomeAppletPrivate *priv;
511         GtkWidget *event_box;
512         GtkWidget *hbox, *vbox, *align;
513
514         self->priv = EL_HOME_APPLET_GET_PRIVATE (self);
515         priv = self->priv;
516
517         gtk_widget_set_app_paintable (GTK_WIDGET (self), TRUE);
518
519         priv->unread = gtk_label_new ("12");
520         hildon_helper_set_logical_color (priv->unread,
521                                          GTK_RC_FG,
522                                          GTK_STATE_NORMAL,
523                                          "ActiveTextColor");
524         gtk_misc_set_alignment (GTK_MISC (priv->unread),
525                                 1.0f,
526                                 0.5f);
527         gtk_widget_set_size_request (priv->unread,
528                                      -1,
529                                      HEADER_HEIGHT);
530
531         priv->icon = gtk_image_new_from_icon_name ("chat_unread_sms",
532                                                    HILDON_ICON_SIZE_FINGER);
533         gtk_misc_set_alignment (GTK_MISC (priv->icon),
534                                 0.5f,
535                                 0.5f);
536
537         priv->sender = gtk_label_new ("asdf asdf asdf asdf asdf");
538         gtk_misc_set_alignment (GTK_MISC (priv->sender),
539                                 0.5f,
540                                 0.5f);
541         gtk_label_set_ellipsize (GTK_LABEL (priv->sender),
542                                  PANGO_ELLIPSIZE_END);
543         gtk_widget_set_name (priv->sender, "hildon-shadow-label");
544         hildon_helper_set_logical_font (priv->sender, "SystemFont");
545
546         priv->message = g_object_new (GTK_TYPE_LABEL,
547                                       "label", "asdf asdf adsf asdf asdf asdf asdf asdf",
548                                       "wrap", TRUE,
549                                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
550                                       NULL);
551
552         gtk_misc_set_alignment (GTK_MISC (priv->message),
553                                 0.0f,
554                                 0.0f);
555         gtk_widget_set_size_request (priv->message,
556                                      MESSAGE_WIDTH,
557                                      MESSAGE_HEIGHT);
558         gtk_widget_set_name (priv->message, "hildon-shadow-label");
559
560         priv->received = gtk_label_new ("aewf aewf aewf awef");
561         gtk_misc_set_alignment (GTK_MISC (priv->received),
562                                 1.0f,
563                                 0.5f);
564         gtk_widget_set_size_request (priv->received,
565                                      MESSAGE_WIDTH,
566                                      -1);
567         hildon_helper_set_logical_font (priv->received, "SmallSystemFont");
568         gtk_widget_set_name (priv->received, "hildon-shadow-label");
569
570         hbox = gtk_hbox_new (FALSE, 0);
571         gtk_box_pack_start (GTK_BOX (hbox), priv->unread, FALSE, FALSE, 0);
572         gtk_box_pack_start (GTK_BOX (hbox), priv->icon, FALSE, FALSE, 0);
573         gtk_box_pack_start (GTK_BOX (hbox), priv->sender, TRUE, TRUE, 0);
574
575         vbox = gtk_vbox_new (FALSE, 0);
576         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
577         gtk_box_pack_start (GTK_BOX (vbox), priv->message, TRUE, TRUE, 0);
578         gtk_box_pack_start (GTK_BOX (vbox), priv->received, FALSE, FALSE, 0);
579
580         align = gtk_alignment_new (0.5f, 0.0f, 1.0f, 1.0f);
581         gtk_alignment_set_padding (GTK_ALIGNMENT (align),
582                                    0, 0, HILDON_MARGIN_DEFAULT, HILDON_MARGIN_DEFAULT);
583
584         gtk_container_set_border_width (GTK_CONTAINER (vbox), HILDON_MARGIN_HALF);
585
586         event_box = gtk_event_box_new ();
587         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
588         gtk_widget_set_size_request (event_box, BOX_WIDTH, BOX_HEIGHT);
589
590         gtk_container_add (GTK_CONTAINER (align), vbox);
591         gtk_container_add (GTK_CONTAINER (event_box), align);
592         gtk_container_add (GTK_CONTAINER (self), event_box);
593
594         g_signal_connect (event_box, "button-press-event",
595                 G_CALLBACK (button_press_event_cb), self);
596         g_signal_connect (event_box, "button-release-event",
597                 G_CALLBACK (button_release_event_cb), self);
598         g_signal_connect (event_box, "leave-notify-event",
599                 G_CALLBACK (leave_notify_event_cb), self);
600
601         gtk_widget_show_all (GTK_WIDGET (event_box));
602
603 #ifndef DEBUG_LAYOUT
604         priv->eventlogger = rtcom_el_new ();
605         g_signal_connect (priv->eventlogger,
606                           "new-event",
607                           G_CALLBACK (new_event_cb),
608                           self);
609         g_signal_connect (priv->eventlogger,
610                           "event-updated",
611                           G_CALLBACK (event_updated_cb),
612                           self);
613
614         read_event (self);
615         priv->unread_count = query_unread_events (priv->eventlogger);
616         update_unread_label (self);
617 #endif
618 }
619
620 static void
621 el_home_applet_class_init (ELHomeAppletClass *klass)
622 {
623         GObjectClass *object_class = G_OBJECT_CLASS (klass);
624         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
625
626         object_class->dispose = dispose;
627         object_class->finalize = finalize;
628         widget_class->expose_event = expose_event;
629         widget_class->realize = el_home_applet_realize;
630
631         g_type_class_add_private (klass, sizeof (ELHomeAppletPrivate));
632 }
633