1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 * Copyright (C) 2009 Artem Garmash. All rights reserved.
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.
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.
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
19 * Contact: Artem Garmash <artemgarmash@gmail.com>
24 #include "el-home-applet.h"
26 #include <hildon/hildon.h>
27 #include <rtcom-eventlogger/eventlogger.h>
31 #define EL_HOME_APPLET_GET_PRIVATE(obj) ( \
32 G_TYPE_INSTANCE_GET_PRIVATE (obj, \
33 EL_TYPE_HOME_APPLET, ELHomeAppletPrivate))
36 #define BOX_HEIGHT 266
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
43 #define HEADER_HEIGHT 48
44 #define MESSAGE_HEIGHT (C_HEIGHT - HEADER_HEIGHT)
45 #define MESSAGE_WIDTH (C_WIDTH - 2*HILDON_MARGIN_DEFAULT)
47 #define BOX_RADIOUS 10
49 #define SCROLL_PERIOD 100 /* ms */
50 #define SCROLL_STEP 1 /* pixel */
52 struct _ELHomeAppletPrivate
74 PangoFontDescription *font_desc;
78 cairo_surface_t *message_surface;
80 gboolean scroll_on_click;
82 gint hidden_message_height;
86 HD_DEFINE_PLUGIN_MODULE (ELHomeApplet, el_home_applet, HD_TYPE_HOME_PLUGIN_ITEM);
88 const gchar* g_module_check_init (GModule *module);
90 g_module_check_init (GModule *module)
92 g_module_make_resident (module);
97 el_home_applet_class_finalize (ELHomeAppletClass *klass)
102 el_home_applet_realize (GtkWidget *widget)
106 screen = gtk_widget_get_screen (widget);
107 gtk_widget_set_colormap (widget,
108 gdk_screen_get_rgba_colormap (screen));
110 gtk_widget_set_app_paintable (widget,
113 GTK_WIDGET_CLASS (el_home_applet_parent_class)->realize (widget);
117 * Thanks http://cairographics.org/cookbook/roundedrectangles/
120 rounded_rectangle (cairo_t *cr,
127 cairo_move_to (cr, x + r, y);
128 cairo_line_to (cr, x + w - r, y);
129 cairo_curve_to (cr, x + w, y,
132 cairo_line_to (cr, x + w, y + h - r);
133 cairo_curve_to (cr, x + w, y + h,
136 cairo_line_to (cr, x + r, y + h);
137 cairo_curve_to (cr, x, y + h,
140 cairo_line_to (cr, x, y + r);
141 cairo_curve_to (cr, x, y,
146 static cairo_surface_t*
147 draw_text (cairo_t *cr,
148 PangoFontDescription *desc,
154 PangoRectangle extent;
156 cairo_surface_t *gdk_surface, *result_surface;
159 /* Create a PangoLayout, set the font and text */
160 layout = pango_cairo_create_layout (cr);
161 pango_layout_set_text (layout,
164 pango_layout_set_font_description (layout, desc);
166 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
167 pango_layout_set_width (layout, PANGO_SCALE*width);
169 pango_layout_get_pixel_extents (layout, NULL, &extent);
170 *height = extent.height;
172 gdk_surface = cairo_get_target (cr);
173 result_surface = cairo_surface_create_similar
175 CAIRO_CONTENT_COLOR_ALPHA,
178 msg_cr = cairo_create (result_surface);
180 pango_cairo_update_layout (msg_cr, layout);
182 cairo_move_to (msg_cr, 1, 1);
183 cairo_set_source_rgba (msg_cr, 0.2, 0.2, 0.2, 0.8);
184 pango_cairo_show_layout (msg_cr, layout);
187 cairo_move_to (msg_cr, 0, 0);
188 cairo_set_source_rgba (msg_cr, 1.0, 1.0, 1.0, 1.0);
189 pango_cairo_show_layout (msg_cr, layout);
191 cairo_destroy (msg_cr);
192 g_object_unref (layout);
194 return result_surface;
198 stop_scroll_anim (ELHomeAppletPrivate *priv)
200 if (priv->scroll_anim_id > 0) {
201 g_source_remove (priv->scroll_anim_id);
202 priv->scroll_anim_id = 0;
203 priv->scroll_on_click = FALSE;
208 style_set_cb (GtkWidget *widget,
209 GtkStyle *previous_style,
212 ELHomeAppletPrivate *priv = self->priv;
214 GtkStyle *font_style;
216 font_style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
220 if (font_style && font_style->font_desc) {
222 pango_font_description_free (priv->font_desc);
223 priv->font_desc = pango_font_description_copy (font_style->font_desc);
226 if (gtk_style_lookup_color (widget->style,
229 priv->active_color.red = color.red/(float)G_MAXUINT16;
230 priv->active_color.green = color.green/(float)G_MAXUINT16;
231 priv->active_color.blue = color.blue/(float)G_MAXUINT16;
236 notify_on_current_desktop (GObject *object,
237 GParamSpec *unused G_GNUC_UNUSED,
240 ELHomeAppletPrivate *priv = self->priv;
243 g_object_get (object, "is-on-current-desktop", &on, NULL);
245 stop_scroll_anim (self->priv);
246 priv->scroll_on_click = priv->scroll_offset;
247 priv->scroll_offset = 0;
248 gtk_widget_queue_draw (GTK_WIDGET (self));
253 expose_event (GtkWidget *self, GdkEventExpose *event)
255 ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
259 message_height = C_HEIGHT - priv->received->allocation.height - HEADER_HEIGHT;
261 cr = gdk_cairo_create (self->window);
262 gdk_cairo_region (cr, event->region);
266 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
268 cairo_set_source_rgba (cr, 0.4f, 0.4f, 0.4f, 0.1f);
269 cairo_set_line_width (cr, 3.0f);
271 rounded_rectangle (cr,
278 cairo_close_path (cr);
282 cairo_set_line_width (cr, 1.0f);
284 cairo_translate (cr, C_X, C_Y);
285 cairo_move_to (cr, 0, HEADER_HEIGHT);
286 cairo_line_to (cr, 0, BOX_RADIOUS);
287 cairo_curve_to (cr, 0, 0, 0, 0, BOX_RADIOUS, 0);
288 cairo_line_to (cr, C_WIDTH - BOX_RADIOUS, 0);
289 cairo_curve_to (cr, C_WIDTH, 0, C_WIDTH, 0, C_WIDTH, BOX_RADIOUS);
290 cairo_line_to (cr, C_WIDTH, HEADER_HEIGHT);
291 cairo_line_to (cr, 0, HEADER_HEIGHT);
293 cairo_close_path (cr);
295 cairo_set_source_rgba (cr, 0.2f, 0.2f, 0.2f, 0.8f);
296 cairo_fill_preserve (cr);
297 cairo_set_source_rgba (cr,
298 priv->active_color.red,
299 priv->active_color.green,
300 priv->active_color.blue,
305 cairo_move_to (cr, 0, HEADER_HEIGHT);
306 cairo_line_to (cr, 0, C_HEIGHT - BOX_RADIOUS);
307 cairo_curve_to (cr, 0, C_HEIGHT, 0, C_HEIGHT, BOX_RADIOUS, C_HEIGHT);
308 cairo_line_to (cr, C_WIDTH - BOX_RADIOUS, C_HEIGHT);
309 cairo_curve_to (cr, C_WIDTH, C_HEIGHT, C_WIDTH, C_HEIGHT, C_WIDTH, C_HEIGHT - BOX_RADIOUS);
310 cairo_line_to (cr, C_WIDTH, HEADER_HEIGHT);
311 cairo_line_to (cr, 0, HEADER_HEIGHT);
312 cairo_close_path (cr);
314 /* draw body filling depending on (in)active state */
315 cairo_pattern_t *grad;
316 grad = cairo_pattern_create_linear (0, HEADER_HEIGHT,
320 cairo_pattern_add_color_stop_rgba (grad,
322 priv->active_color.red,
323 priv->active_color.green,
324 priv->active_color.blue,
326 cairo_pattern_add_color_stop_rgba (grad,
328 priv->active_color.red/2,
329 priv->active_color.green/2,
330 priv->active_color.blue/2,
334 cairo_pattern_add_color_stop_rgba (grad, 0.5f,
335 0.4f, 0.4f, 0.4f, 0.8f);
336 cairo_pattern_add_color_stop_rgba (grad, 1.0f,
337 0.2f, 0.2f, 0.2f, 0.8f);
339 cairo_set_source (cr, grad);
342 /* cairo_set_source_rgba (cr, red, green, blue, 1.0f); */
343 /* cairo_translate (cr, -C_X, -C_Y); */
344 /* rounded_rectangle (cr, */
347 /* BOX_WIDTH - 2*C_X, */
348 /* BOX_HEIGHT - 2*C_Y, */
350 /* cairo_close_path (cr); */
351 /* cairo_stroke (cr); */
354 if (!priv->message_surface) {
357 priv->message_surface = draw_text (cr,
363 priv->hidden_message_height = height - message_height;
364 priv->scroll_on_click = priv->hidden_message_height > 0;
374 cairo_set_source_surface (cr,
375 priv->message_surface,
377 HEADER_HEIGHT - priv->scroll_offset);
378 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
381 cairo_pattern_destroy (grad);
384 return GTK_WIDGET_CLASS (el_home_applet_parent_class)->expose_event (self, event);
388 dispose (GObject *self)
390 ELHomeAppletPrivate *priv = EL_HOME_APPLET(self)->priv;
392 stop_scroll_anim (priv);
394 g_source_remove (priv->idle_id);
397 if (priv->eventlogger) {
398 g_object_unref (priv->eventlogger);
399 priv->eventlogger = NULL;
403 g_free (priv->message);
404 priv->message = NULL;
406 if (priv->font_desc) {
407 pango_font_description_free (priv->font_desc);
408 priv->font_desc = NULL;
411 G_OBJECT_CLASS (el_home_applet_parent_class)->dispose (self);
415 finalize (GObject *self)
417 G_OBJECT_CLASS (el_home_applet_parent_class)->finalize (self);
421 format_time (time_t t)
423 static const guint RESULT_SIZE = 32;
426 struct tm now_tm, t_tm;
427 const gchar *format = "%Y.%m.%d %T";
428 gchar *result = g_malloc0 (RESULT_SIZE);
431 localtime_r (&now, &now_tm);
432 localtime_r (&t, &t_tm);
434 if ((now_tm.tm_year == t_tm.tm_year) &&
435 (now_tm.tm_mon == t_tm.tm_mon) &&
436 (now_tm.tm_mday == t_tm.tm_mday))
439 strftime (result, RESULT_SIZE, format, &t_tm);
445 show_event (ELHomeApplet *self, RTComElIter *it)
447 ELHomeAppletPrivate *priv = self->priv;
449 gchar *remote = NULL;
450 gchar *received = NULL;
451 const gchar *icon_name = NULL;
454 g_free (priv->message);
455 priv->message = NULL;
458 if (it && rtcom_el_iter_first (it)) {
459 rtcom_el_iter_dup_string (it, "free-text", &priv->message);
461 const gchar *service;
464 rtcom_el_iter_get_int (it, "id", &priv->event_id);
465 if (rtcom_el_iter_get_int (it, "start-time", (gint*)&received_t))
466 received = format_time (received_t);
468 if (!rtcom_el_iter_dup_string (it, "remote-name", &remote))
469 rtcom_el_iter_dup_string (it, "remote-id", &remote);
470 service = rtcom_el_iter_get_service (it);
471 if (!g_strcmp0 (service, "RTCOM_EL_SERVICE_SMS"))
472 icon_name = "chat_unread_sms";
473 else if (!g_strcmp0 (service, "RTCOM_EL_SERVICE_CHAT"))
474 icon_name = "chat_unread_chat";
482 gtk_widget_hide (priv->empty);
484 gtk_widget_show (priv->empty);
486 gtk_label_set_text (GTK_LABEL (priv->sender), remote);
487 gtk_label_set_text (GTK_LABEL (priv->received), received);
490 const gchar *current_icon_name;
491 gtk_image_get_icon_name (GTK_IMAGE (priv->icon),
494 if (g_strcmp0 (current_icon_name, icon_name))
495 gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
497 HILDON_ICON_SIZE_FINGER);
498 gtk_widget_show (priv->icon);
501 gtk_widget_hide (priv->icon);
505 stop_scroll_anim (priv);
506 priv->scroll_offset = 0;
507 if (priv->message_surface) {
508 cairo_surface_destroy (priv->message_surface);
509 priv->message_surface = NULL;
511 gtk_widget_queue_draw (GTK_WIDGET (self));
515 make_query (RTComEl *el, gint event_id)
517 RTComElQuery *query = NULL;
518 RTComElIter *it = NULL;
520 static const gchar *services[] = {"RTCOM_EL_SERVICE_SMS",
521 "RTCOM_EL_SERVICE_CHAT",
523 static const gchar *event_types[] = {"RTCOM_EL_EVENTTYPE_SMS_INBOUND",
524 "RTCOM_EL_EVENTTYPE_CHAT_INBOUND",
527 query = rtcom_el_query_new (el);
528 rtcom_el_query_set_limit (query, 1);
530 rtcom_el_query_prepare (query,
531 "is-read", FALSE, RTCOM_EL_OP_EQUAL,
532 "id", event_id, RTCOM_EL_OP_EQUAL,
533 "service", services, RTCOM_EL_OP_IN_STRV,
534 "event-type", event_types, RTCOM_EL_OP_IN_STRV,
538 rtcom_el_query_prepare (query,
539 "is-read", FALSE, RTCOM_EL_OP_EQUAL,
540 "service", services, RTCOM_EL_OP_IN_STRV,
541 "event-type", event_types, RTCOM_EL_OP_IN_STRV,
544 it = rtcom_el_get_events (el, query);
545 g_object_unref (query);
551 update_unread_label (ELHomeApplet *self)
553 ELHomeAppletPrivate *priv = self->priv;
556 if (priv->unread_count > 0) {
557 text = g_strdup_printf ("%d", priv->unread_count);
558 gtk_label_set_text (GTK_LABEL (priv->unread), text);
562 gtk_label_set_text (GTK_LABEL (priv->unread), NULL);
566 query_unread_events (RTComEl *el)
573 g_object_get (el, "db", &db, NULL);
575 if (sqlite3_prepare_v2 (db,
576 "SELECT SUM(total_events)-SUM(read_events) FROM GroupCache;",
579 NULL) != SQLITE_OK) {
580 g_error ("%s: can't compile SQL", G_STRFUNC);
584 while (SQLITE_BUSY == (ret = sqlite3_step (stmt)));
586 if (ret == SQLITE_ROW) {
587 count = sqlite3_column_int (stmt, 0);
590 g_error ("%s: error while executing SQL", G_STRFUNC);
593 sqlite3_finalize (stmt);
599 read_event (ELHomeApplet *self)
601 ELHomeAppletPrivate *priv = self->priv;
602 RTComElIter *it = NULL;
604 it = make_query (priv->eventlogger, -1);
605 show_event (self, it);
606 if (it) g_object_unref (it);
610 mark_as_read (ELHomeApplet *self)
612 ELHomeAppletPrivate *priv = self->priv;
614 if (priv->event_id >= 0) {
615 rtcom_el_set_read_event (priv->eventlogger,
620 priv->unread_count--;
621 update_unread_label (self);
626 read_new_event (ELHomeApplet *self)
628 ELHomeAppletPrivate *priv = self->priv;
631 priv->unread_count = query_unread_events (priv->eventlogger);
632 update_unread_label (self);
640 add_new_idle (ELHomeApplet *self)
642 ELHomeAppletPrivate *priv = self->priv;
645 g_source_remove (priv->idle_id);
646 priv->idle_id = g_idle_add ((GSourceFunc)read_new_event,
651 new_event_cb (RTComEl *backend,
653 const gchar *local_uid,
654 const gchar *remote_uid,
655 const gchar *remote_ebook_uid,
656 const gchar *group_uid,
657 const gchar *service,
664 scroll_anim_cb (ELHomeApplet *self)
666 ELHomeAppletPrivate *priv = self->priv;
667 gboolean to_continue;
669 priv->scroll_offset += SCROLL_STEP;
670 gtk_widget_queue_draw_area (GTK_WIDGET (self),
674 C_HEIGHT - priv->received->allocation.height - HEADER_HEIGHT);
676 to_continue = priv->scroll_offset <= priv->hidden_message_height;
678 priv->scroll_anim_id = 0;
684 button_press_event_cb (GtkWidget *widget,
685 GdkEventButton *event,
688 ELHomeAppletPrivate *priv = self->priv;
690 if (priv->event_id > 0) {
692 gtk_widget_queue_draw (widget);
699 button_release_event_cb (GtkWidget *widget,
700 GdkEventButton *event,
703 ELHomeAppletPrivate *priv = self->priv;
706 priv->active = FALSE;
707 stop_scroll_anim (priv);
708 if (priv->scroll_on_click) {
709 priv->scroll_on_click = FALSE;
710 priv->scroll_anim_id = g_timeout_add (SCROLL_PERIOD,
711 (GSourceFunc)scroll_anim_cb,
720 gtk_widget_queue_draw (widget);
727 leave_notify_event_cb (GtkWidget *widget,
728 GdkEventCrossing *event,
731 ELHomeAppletPrivate *priv = self->priv;
734 priv->active = FALSE;
735 stop_scroll_anim (priv);
736 gtk_widget_queue_draw (widget);
743 el_home_applet_init (ELHomeApplet *self)
745 ELHomeAppletPrivate *priv;
746 GtkWidget *event_box;
747 GtkWidget *hbox, *vbox, *align;
749 self->priv = EL_HOME_APPLET_GET_PRIVATE (self);
752 gtk_widget_set_app_paintable (GTK_WIDGET (self), TRUE);
754 priv->unread = gtk_label_new ("12");
755 hildon_helper_set_logical_color (priv->unread,
759 gtk_misc_set_alignment (GTK_MISC (priv->unread),
762 gtk_widget_set_size_request (priv->unread,
766 priv->icon = gtk_image_new_from_icon_name ("chat_unread_sms",
767 HILDON_ICON_SIZE_FINGER);
768 gtk_misc_set_alignment (GTK_MISC (priv->icon),
772 priv->sender = gtk_label_new ("asdf asdf asdf asdf asdf");
773 gtk_misc_set_alignment (GTK_MISC (priv->sender),
776 gtk_label_set_ellipsize (GTK_LABEL (priv->sender),
777 PANGO_ELLIPSIZE_END);
778 gtk_widget_set_name (priv->sender, "hildon-shadow-label");
779 hildon_helper_set_logical_font (priv->sender, "SystemFont");
781 priv->message = g_strdup ("One two three four five six seven eight nine ten");
784 priv->empty = gtk_label_new ("No new messages");
785 gtk_widget_set_name (priv->empty, "hildon-shadow-label");
786 GTK_WIDGET_SET_FLAGS (priv->empty, GTK_NO_SHOW_ALL);
788 priv->received = gtk_label_new ("aewf aewf aewf awef");
789 gtk_misc_set_alignment (GTK_MISC (priv->received),
792 gtk_widget_set_size_request (priv->received,
795 hildon_helper_set_logical_font (priv->received, "SmallSystemFont");
796 gtk_widget_set_name (priv->received, "hildon-shadow-label");
798 hbox = gtk_hbox_new (FALSE, 0);
799 gtk_box_pack_start (GTK_BOX (hbox), priv->unread, FALSE, FALSE, 0);
800 gtk_box_pack_start (GTK_BOX (hbox), priv->icon, FALSE, FALSE, 0);
801 gtk_box_pack_start (GTK_BOX (hbox), priv->sender, TRUE, TRUE, 0);
803 vbox = gtk_vbox_new (FALSE, 0);
804 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
805 gtk_box_pack_start (GTK_BOX (vbox), priv->empty, TRUE, TRUE, 0);
806 gtk_box_pack_end (GTK_BOX (vbox), priv->received, FALSE, FALSE, 0);
808 align = gtk_alignment_new (0.5f, 0.0f, 1.0f, 1.0f);
809 gtk_alignment_set_padding (GTK_ALIGNMENT (align),
810 0, 0, HILDON_MARGIN_DEFAULT, HILDON_MARGIN_DEFAULT);
812 gtk_container_set_border_width (GTK_CONTAINER (vbox), HILDON_MARGIN_HALF);
814 event_box = gtk_event_box_new ();
815 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
816 gtk_widget_set_size_request (event_box, BOX_WIDTH, BOX_HEIGHT);
818 gtk_container_add (GTK_CONTAINER (align), vbox);
819 gtk_container_add (GTK_CONTAINER (event_box), align);
820 gtk_container_add (GTK_CONTAINER (self), event_box);
822 g_signal_connect (event_box,
823 "button-press-event",
824 G_CALLBACK (button_press_event_cb),
826 g_signal_connect (event_box,
827 "button-release-event",
828 G_CALLBACK (button_release_event_cb),
830 g_signal_connect (event_box,
831 "leave-notify-event",
832 G_CALLBACK (leave_notify_event_cb),
835 g_signal_connect (event_box,
837 G_CALLBACK (style_set_cb),
839 g_signal_connect (self,
840 "notify::is-on-current-desktop",
841 G_CALLBACK (notify_on_current_desktop),
844 gtk_widget_show_all (GTK_WIDGET (event_box));
847 priv->eventlogger = rtcom_el_new ();
848 g_signal_connect (priv->eventlogger,
850 G_CALLBACK (new_event_cb),
852 g_signal_connect (priv->eventlogger,
854 G_CALLBACK (new_event_cb),
857 read_new_event (self);
862 el_home_applet_class_init (ELHomeAppletClass *klass)
864 GObjectClass *object_class = G_OBJECT_CLASS (klass);
865 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
867 object_class->dispose = dispose;
868 object_class->finalize = finalize;
869 widget_class->expose_event = expose_event;
870 widget_class->realize = el_home_applet_realize;
872 g_type_class_add_private (klass, sizeof (ELHomeAppletPrivate));