Added posibility to call or send SMS to selected contact.
[birthday] / src / birthday.c
1 /*
2  *  Birthday application for Maemo.
3  *  Copyright (C) 2010 Roman Moravcik
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
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <time.h>
29
30 #include <libosso.h>
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34
35 #include <gtk/gtk.h>
36 #include <hildon/hildon.h>
37
38 #include <libebook/e-book.h>
39 #include <libosso-abook/osso-abook.h>
40
41 #include <mce/dbus-names.h>
42 #include <mce/mode-names.h>
43
44 #define MCE_MATCH_RULE "type='signal',interface='" MCE_SIGNAL_IF "',member='" MCE_DEVICE_ORIENTATION_SIG "'"
45
46 #define _HL(str) dgettext("hildon-libs",str)
47
48 enum
49 {
50         COLUMN_AVATAR = 0,
51         COLUMN_DISPLAY,
52         COLUMN_FULLNAME,
53         COLUMN_NEXT_BIRTHDAY,
54         COLUMN_ABOOK_CONTACT,
55         NUM_COLS
56 };
57
58 /* Application UI data struct */
59 typedef struct _BirthdayData BirthdayData;
60 struct _BirthdayData {
61         GtkWidget *window;
62         GtkWidget *label;
63         GtkWidget *view;
64         GtkWidget *search;
65
66         GtkTreeViewColumn *display_column;
67
68         GtkWidget *tree_view;
69         GtkTreeModel *sorted;
70         GtkTreeModel *filter;
71
72         gchar *searched_name;
73         gboolean found;
74
75         guint n_contacts;
76 };
77
78 static void set_portrait_mode (BirthdayData *priv, gboolean enable);
79
80 static gboolean
81 is_portrait_mode (osso_context_t *osso_context)
82 {
83         osso_rpc_t ret;
84         gboolean result = FALSE;
85
86         if (osso_rpc_run_system (osso_context, MCE_SERVICE, MCE_REQUEST_PATH,
87                                  MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET,
88                                  &ret, DBUS_TYPE_INVALID) == OSSO_OK) {
89
90                 if (strcmp (ret.value.s, MCE_ORIENTATION_PORTRAIT) == 0) {
91                         result = TRUE;
92                 }
93                 osso_rpc_free_val (&ret);
94         } else {
95                 g_critical ("ERROR: Call do DBus failed\n");
96         }
97         return result;
98 }
99
100 static DBusHandlerResult
101 dbus_handle_mce_message (DBusConnection *connection,
102                          DBusMessage *message,
103                          gpointer data)
104 {
105         DBusMessageIter iter;
106         const gchar *orientation = NULL;
107         BirthdayData *priv;
108
109         g_return_val_if_fail (data, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
110         priv = (BirthdayData *) data;
111
112         if (dbus_message_is_signal (message, MCE_SIGNAL_IF, MCE_DEVICE_ORIENTATION_SIG)) {
113                 if (dbus_message_iter_init (message, &iter)) {
114                         dbus_message_iter_get_basic(&iter, &orientation);
115                         if (orientation) {
116                                 if (!strcmp (orientation, MCE_ORIENTATION_PORTRAIT))
117                                         set_portrait_mode (priv, TRUE);
118                                 else
119                                         set_portrait_mode (priv, FALSE);
120                         }
121                 }
122         }
123         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
124 }
125
126 static gboolean
127 birthday_filered_view_visible_func (GtkTreeModel *model,
128                                     GtkTreeIter *iter,
129                                     gpointer data)
130 {
131         BirthdayData *priv;
132         gchar *fullname = NULL, *ascii_searched_name = NULL, *ascii_fullname = NULL;
133         gboolean found = FALSE;
134
135         g_return_val_if_fail (data, FALSE);
136         priv = (BirthdayData *) data;
137
138         if (priv->searched_name == NULL) {
139                 priv->found = TRUE;
140                 return TRUE;
141         }
142
143         ascii_searched_name = g_ascii_strdown (priv->searched_name, strlen (priv->searched_name));
144
145         gtk_tree_model_get (model, iter, COLUMN_FULLNAME, &fullname, -1);
146         if (fullname) {
147                 ascii_fullname = g_ascii_strdown (fullname,  strlen (fullname));
148                 g_free (fullname);
149         }
150
151         if (g_strstr_len (ascii_fullname, strlen (ascii_fullname), ascii_searched_name) != NULL)
152                 found = TRUE;
153
154         if (ascii_searched_name)
155                 g_free (ascii_searched_name);
156
157         if (ascii_fullname)
158                 g_free (ascii_fullname);
159
160         if (found)
161                 priv->found = TRUE;
162
163         return found;
164 }
165
166 static void
167 sort_by_name_clicked (GtkButton *button,
168                       gpointer data)
169 {
170         BirthdayData *priv;
171
172         g_return_if_fail (data);
173         priv = (BirthdayData *) data;
174
175         if (priv->sorted) {
176                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->sorted),
177                                                       COLUMN_FULLNAME, GTK_SORT_ASCENDING);
178         }
179 }
180
181 static void
182 sort_by_date_clicked (GtkButton *button,
183                       gpointer data)
184 {
185         BirthdayData *priv;
186
187         g_return_if_fail (data);
188         priv = (BirthdayData *) data;
189
190         if (priv->sorted) {
191                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->sorted),
192                                                       COLUMN_NEXT_BIRTHDAY, GTK_SORT_ASCENDING);
193         }
194 }
195
196 static void
197 search_menu_clicked (GtkButton *button,
198                      gpointer data)
199 {
200         BirthdayData *priv;
201         GtkWidget *entry;
202
203         g_return_if_fail (data);
204         priv = (BirthdayData *) data;
205
206         /* show search bar */
207         gtk_widget_show (priv->search);
208
209         /* focus on search entry */
210         entry  = g_object_get_data (G_OBJECT (priv->search), "entry");
211         gtk_entry_set_text (GTK_ENTRY (entry), "");
212         gtk_widget_grab_focus (GTK_WIDGET (entry));
213
214         /* refilter tree view */
215         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
216 }
217
218 static void
219 on_search_entry_changed (GtkEditable *editable,
220                          gpointer data)
221 {
222         GtkTreeSelection *selection;
223         BirthdayData *priv;
224
225         g_return_if_fail (data);
226         priv = (BirthdayData *) data;
227
228         priv->found = FALSE;
229
230         if (priv->searched_name)
231                 g_free (priv->searched_name);
232         priv->searched_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (editable)));
233
234         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
235
236         /* ugly hack, set back mode to selection none to not generate "changed"
237            signal during re-filtering  */
238         gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
239
240         /* refilter tree view */
241         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
242
243         if (priv->found) {
244                 /* hide label */
245                 gtk_widget_hide (priv->label);
246
247                 /* show tree view */
248                 gtk_widget_show (priv->view);
249         } else {
250                 /* hide label */
251                 gtk_widget_show (priv->label);
252                 gtk_label_set_text (GTK_LABEL (priv->label), _("No search results"));
253
254                 /* show tree view */
255                 gtk_widget_hide (priv->view);
256         }
257
258         /* ugly, but working way how to scroll to the first row */
259         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->tree_view),
260                                       gtk_tree_path_new_from_string ("0"), NULL, FALSE, 0, 0);
261
262         /* ugly hack, set back mode to single selection */
263         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
264
265         /* unselect selected rows */
266         gtk_tree_selection_unselect_all (selection);
267 }
268
269 static void
270 on_search_close_clicked (GtkButton *button,
271                          gpointer data)
272 {
273         GtkTreeSelection *selection;
274         BirthdayData *priv;
275
276         g_return_if_fail (data);
277         priv = (BirthdayData *) data;
278
279         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
280
281         /* ugly hack, set back mode to selection none to not generate "changed"
282            signal during re-filtering  */
283         gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
284
285         /* hide search bar */
286         gtk_widget_hide (priv->search);
287
288         /* hide label */
289         gtk_widget_hide (priv->label);
290
291         /* show tree view */
292         gtk_widget_show (priv->view);
293
294         /* clear searched name */
295         if (priv->searched_name)
296                 g_free (priv->searched_name);
297         priv->searched_name = NULL;
298
299         /* refilter tree view */
300         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
301
302         /* ugly, but working way how to scroll to the first row */
303         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->tree_view),
304                                       gtk_tree_path_new_from_string ("0"), NULL, FALSE, 0, 0);
305
306         /* ugly hack, set back mode to single selection */
307         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
308
309         /* unselect selected rows */
310         gtk_tree_selection_unselect_all (selection);
311 }
312
313
314 static gboolean
315 on_key_press_event (GtkWidget *widget,
316                     GdkEventKey *event,
317                     gpointer data)
318 {
319         BirthdayData *priv;
320
321         g_return_val_if_fail (data, TRUE);
322         priv = (BirthdayData *) data;
323
324         if (priv->n_contacts == 0)
325                 return FALSE;
326
327         if ((event->keyval > GDK_space) && (event->keyval <= GDK_stricteq) && !GTK_WIDGET_VISIBLE (priv->search)) {
328                 GtkWidget *entry;
329
330                 /* show search bar */
331                 gtk_widget_show (priv->search);
332
333                 /* focus on search entry */
334                 entry  = g_object_get_data (G_OBJECT (priv->search), "entry");
335                 gtk_entry_set_text (GTK_ENTRY (entry), "");
336                 gtk_widget_grab_focus (GTK_WIDGET (entry));
337
338                 /* refilter tree view */
339                 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
340         }
341
342         return FALSE;
343 }
344
345 static void
346 on_selection_changed (GtkTreeSelection *selection,
347                       gpointer data)
348 {
349         BirthdayData *priv;
350         GtkTreeModel *model;
351         GtkTreeIter iter;
352
353         g_return_if_fail (data);
354         priv = (BirthdayData *) data;
355
356         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
357                 OssoABookContact *abook_contact = NULL;
358
359                 /* unselect selected rows */
360                 gtk_tree_selection_unselect_all (selection);
361
362                 gtk_tree_model_get (model, &iter, COLUMN_ABOOK_CONTACT, &abook_contact, -1);
363
364                 if (abook_contact) {
365                         GtkWidget *starter, *dialog;
366                         OssoABookContactDetailStore *store;
367                         OssoABookContactAction actions[7] = {OSSO_ABOOK_CONTACT_ACTION_TEL,
368                                                              OSSO_ABOOK_CONTACT_ACTION_SMS,
369                                                              OSSO_ABOOK_CONTACT_ACTION_CHATTO,
370                                                              OSSO_ABOOK_CONTACT_ACTION_VOIPTO,
371                                                              OSSO_ABOOK_CONTACT_ACTION_VOIPTO_AUDIO,
372                                                              OSSO_ABOOK_CONTACT_ACTION_VOIPTO_VIDEO,
373                                                              OSSO_ABOOK_CONTACT_ACTION_MAILTO};
374
375                         store = osso_abook_contact_detail_store_new (abook_contact,
376                                                                      OSSO_ABOOK_CONTACT_DETAIL_EMAIL |
377                                                                      OSSO_ABOOK_CONTACT_DETAIL_PHONE |
378                                                                      OSSO_ABOOK_CONTACT_DETAIL_IM_VOICE |
379                                                                      OSSO_ABOOK_CONTACT_DETAIL_IM_VIDEO |
380                                                                      OSSO_ABOOK_CONTACT_DETAIL_IM_CHAT |
381                                                                      OSSO_ABOOK_CONTACT_DETAIL_SMS);
382
383                         starter = osso_abook_touch_contact_starter_new_with_store (store,
384                                                                                    (OssoABookContactAction *) &actions,
385                                                                                    sizeof (actions));
386
387                         dialog = osso_abook_touch_contact_starter_dialog_new (GTK_WINDOW (priv->window),
388                                                                               OSSO_ABOOK_TOUCH_CONTACT_STARTER (starter));
389                         gtk_widget_show_all (dialog);
390                         gtk_dialog_run (GTK_DIALOG (dialog));
391                 }
392         }
393 }
394
395 static unsigned int
396 calc_age (EContactDate *bdate, time_t current_date)
397 {
398         struct tm *current_date_tm;
399         struct tm bday_tm;
400         int age = 0;
401
402         current_date_tm = gmtime (&current_date);
403
404         bday_tm.tm_sec = 0;
405         bday_tm.tm_min = 0;
406         bday_tm.tm_hour = 0;
407         bday_tm.tm_mday = bdate->day;
408         bday_tm.tm_mon = bdate->month - 1;
409         bday_tm.tm_year = current_date_tm->tm_year;
410         bday_tm.tm_isdst = current_date_tm->tm_isdst;
411
412         if (mktime (&bday_tm) > current_date) {
413                 age = (current_date_tm->tm_year + 1900) - bdate->year - 1;
414         } else {
415                 age = (current_date_tm->tm_year + 1900) - bdate->year;
416         }
417
418         if (age < 0)
419                 age = 0;
420
421         return age;
422 }
423
424 static unsigned int
425 calc_next_bday (EContactDate *bdate, time_t current_date)
426 {
427         struct tm current_bday_tm, next_bday_tm;
428         struct tm *current_date_tm;
429         time_t current_bday, next_bday;
430
431         current_date_tm = gmtime (&current_date);
432
433         current_bday_tm.tm_sec = 0;
434         current_bday_tm.tm_min = 0;
435         current_bday_tm.tm_hour = 0;
436         current_bday_tm.tm_mday = bdate->day;
437         current_bday_tm.tm_mon = bdate->month - 1;
438         current_bday_tm.tm_year = current_date_tm->tm_year;
439         current_bday_tm.tm_isdst = current_date_tm->tm_isdst;
440         current_bday = mktime (&current_bday_tm);
441
442         if (current_date > current_bday) {
443                 next_bday_tm.tm_sec = 0;
444                 next_bday_tm.tm_min = 0;
445                 next_bday_tm.tm_hour = 0;
446                 next_bday_tm.tm_mday = bdate->day;
447                 next_bday_tm.tm_mon = bdate->month - 1;
448                 next_bday_tm.tm_year = current_date_tm->tm_year + 1;
449                 next_bday_tm.tm_isdst = current_date_tm->tm_isdst;
450                 next_bday = mktime (&next_bday_tm);
451         } else {
452                 next_bday = current_bday;
453         }
454
455         return (next_bday - current_date) / 86400;
456 }
457
458 static gchar *
459 get_text_font_by_name (const gchar *name)
460 {
461         GtkSettings *settings;
462         GtkStyle *style;
463
464         settings = gtk_settings_get_default ();
465         style = gtk_rc_get_style_by_paths (settings, name, NULL, G_TYPE_NONE);
466         return pango_font_description_to_string (style->font_desc);
467 }
468
469 static gchar *
470 get_text_color_by_name (const gchar *name)
471 {
472         GtkSettings *settings;
473         GtkStyle *style;
474         GdkColor color;
475
476         settings = gtk_settings_get_default ();
477         style = gtk_rc_get_style_by_paths (settings, "GtkButton", "osso-logical-colors", G_OBJECT_TYPE(gtk_button_new()));
478         gtk_style_lookup_color (style, name, &color);
479         return gdk_color_to_string (&color);
480 }
481
482 static void
483 set_portrait_mode (BirthdayData *priv,
484                    gboolean enable)
485 {
486         g_return_if_fail (priv);
487
488         if (enable) {
489                 gtk_tree_view_column_set_fixed_width (priv->display_column, 389);
490                 hildon_gtk_window_set_portrait_flags (GTK_WINDOW (priv->window),
491                                                       HILDON_PORTRAIT_MODE_REQUEST);
492         } else {
493                 gtk_tree_view_column_set_fixed_width (priv->display_column, 709);
494                 hildon_gtk_window_set_portrait_flags (GTK_WINDOW (priv->window),
495                                                       ~HILDON_PORTRAIT_MODE_REQUEST);
496         }
497 }
498
499 static GtkListStore *
500 create_bday_liststore (BirthdayData *priv, GList *contacts)
501 {
502         GtkListStore *store;
503         GtkTreeIter iter;
504         GList *contact;
505         gchar *text_font = NULL;
506         gchar *text_color = NULL;
507         guint n_contacts = 0;
508         time_t current_date;
509         struct tm *current_date_tm;
510
511         g_return_val_if_fail (priv, NULL);
512
513         text_font = get_text_font_by_name ("SmallSystemFont");
514         text_color = get_text_color_by_name ("SecondaryTextColor");
515
516         current_date = time (NULL);
517
518         /* set hour, minute, second to 0 */
519         current_date_tm = gmtime (&current_date);
520         current_date_tm->tm_sec = 0;
521         current_date_tm->tm_min = 0;
522         current_date_tm->tm_hour = 0;
523         current_date = mktime (current_date_tm);
524
525         store = gtk_list_store_new(NUM_COLS,
526                                    GDK_TYPE_PIXBUF,     /* COLUMN_AVATAR */
527                                    G_TYPE_STRING,       /* COLUMN_DISPLAY */
528                                    G_TYPE_STRING,       /* COLUMN_FULLNAME */
529                                    G_TYPE_INT,          /* COLUMN_NEXT_BIRTHDAY */
530                                    G_TYPE_POINTER);     /* COLUMN_ABOOK_CONTACT */
531
532         for (contact = contacts; contact != NULL; contact = contact->next) {
533                 EContactDate *bdate = NULL;
534
535                 bdate = e_contact_get (E_CONTACT (contact->data), E_CONTACT_BIRTH_DATE);
536                 if (bdate) {
537                         EContactPhoto *photo = NULL;
538                         GError *error = NULL;
539                         GdkPixbuf *avatar = NULL;
540                         gchar *fullname = NULL;
541                         guint age = 0, next_birthday = 0;
542                         gchar *display_column = NULL;
543                         gchar *next_birthday_text = NULL;
544                         struct tm birthday_tm;
545                         gchar birthday_text[11];
546                         OssoABookContact *abook_contact;
547
548                         photo = e_contact_get (E_CONTACT (contact->data), E_CONTACT_PHOTO);
549                         if (photo) {
550                                 if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
551                                         GdkPixbufLoader *loader;
552
553                                         loader = gdk_pixbuf_loader_new ();
554                                         if (gdk_pixbuf_loader_write (loader, (guchar *) photo->data.inlined.data, photo->data.inlined.length, NULL))
555                                                 avatar = gdk_pixbuf_loader_get_pixbuf (loader);
556
557                                 } else {
558                                         gchar *avatar_filename = NULL;
559
560                                         avatar_filename = g_filename_from_uri (photo->data.uri, NULL, NULL);
561                                         if (avatar_filename) {
562                                                 avatar = gdk_pixbuf_new_from_file (avatar_filename, &error);
563                                                 g_free (avatar_filename);
564                                         }
565                                 }
566
567                                 if (avatar) {
568                                         gint height = gdk_pixbuf_get_height (avatar);
569                                         if (height != 48) {
570                                                 gint new_height = 48;
571                                                 gint new_width = new_height * gdk_pixbuf_get_width (avatar) / height;
572                                                 avatar = gdk_pixbuf_scale_simple (avatar, new_width, new_height, GDK_INTERP_BILINEAR);
573                                         }
574                                 }
575                                 e_contact_photo_free (photo);
576                                 photo = NULL;
577                         } else {
578                                 avatar = gdk_pixbuf_new_from_file ("/usr/share/icons/hicolor/48x48/hildon/general_default_avatar.png", &error);
579                         }
580
581                         fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_FULL_NAME);
582                         if (!fullname) {
583                                 fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_GIVEN_NAME);
584                                 if (!fullname) {
585                                         fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_FAMILY_NAME);
586                                         if (!fullname) {
587                                                 fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_NICKNAME);
588                                                 if (!fullname) {
589                                                         fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_ORG);
590                                                 }
591                                         }
592                                 }
593                         }
594
595                         birthday_tm.tm_sec = 0;
596                         birthday_tm.tm_min = 0;
597                         birthday_tm.tm_hour = 0;
598                         birthday_tm.tm_mday = bdate->day;
599                         birthday_tm.tm_mon = bdate->month - 1;
600                         birthday_tm.tm_year = bdate->year - 1900;
601                         strftime (birthday_text, 11, _HL("wdgt_va_date"), &birthday_tm);
602
603                         age = calc_age(bdate, current_date);
604                         next_birthday = calc_next_bday(bdate, current_date);
605
606                         if (next_birthday == 0)
607                                 next_birthday_text = g_strdup_printf (_("has birthday today"));
608                         else
609                                 next_birthday_text = g_strdup_printf (ngettext ("will have birthday tomorrow",
610                                                                                 "will have birthday in %d days", next_birthday),
611                                                                       next_birthday);
612
613                         display_column = g_strdup_printf ("%s <span font_desc=\"%s\" foreground=\"%s\"><sup>(%d)</sup>\n%s, %s</span>",
614                                                          fullname, text_font, text_color, age, birthday_text, next_birthday_text);
615
616                         abook_contact = osso_abook_contact_new_from_template (E_CONTACT (contact->data));
617
618                         gtk_list_store_append (store, &iter);
619                         gtk_list_store_set (store, &iter,
620                                             COLUMN_AVATAR, avatar,
621                                             COLUMN_DISPLAY, display_column,
622                                             COLUMN_FULLNAME, fullname,
623                                             COLUMN_NEXT_BIRTHDAY, next_birthday,
624                                             COLUMN_ABOOK_CONTACT, abook_contact,
625                                             -1);
626                         n_contacts++;
627
628                         if (display_column)
629                                 g_free (display_column);
630                         display_column = NULL;
631
632                         if (fullname)
633                                 g_free (fullname);
634                         fullname = NULL;
635
636                         if (next_birthday_text)
637                                 g_free (next_birthday_text);
638                         next_birthday_text = NULL;
639
640                         e_contact_date_free (bdate);
641                 }
642                 bdate = NULL;
643         }
644
645         if (text_font)
646                 g_free (text_font);
647
648         if (text_color)
649                 g_free (text_color);
650
651         priv->n_contacts = n_contacts;
652         return store;
653 }
654
655 static void
656 create_search_bar (BirthdayData *priv)
657 {
658         GtkWidget *entry, *button;
659         GtkEntryCompletion *completion;
660
661         g_return_if_fail (priv);
662
663         /* search hbox */
664         priv->search = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
665
666         /* search entry */
667         entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
668         hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry), HILDON_GTK_INPUT_MODE_FULL);
669         gtk_box_pack_start (GTK_BOX (priv->search), entry, TRUE, TRUE, 0);
670
671         completion = gtk_entry_completion_new ();
672         gtk_entry_completion_set_inline_completion (completion, TRUE);
673         gtk_entry_completion_set_popup_completion (completion, FALSE);
674         gtk_entry_set_completion (GTK_ENTRY (entry), completion);
675
676         /* clear button */
677         button = GTK_WIDGET (gtk_tool_button_new (gtk_image_new_from_icon_name
678                              ("general_close", (GtkIconSize) HILDON_ICON_PIXEL_SIZE_FINGER), "Clear"));
679         gtk_box_pack_end (GTK_BOX (priv->search), button, FALSE, TRUE, 0);
680
681         /* search signals */
682         g_signal_connect (entry, "changed", G_CALLBACK (on_search_entry_changed), priv);
683         g_signal_connect (button, "clicked", G_CALLBACK (on_search_close_clicked), priv);
684
685         g_object_set_data (G_OBJECT (priv->search), "entry", entry);
686 }
687
688 static void
689 create_main_menu (BirthdayData *priv)
690 {
691         HildonAppMenu *menu;
692         GtkWidget *filter, *item;
693
694         g_return_if_fail (priv);
695
696         menu = HILDON_APP_MENU (hildon_app_menu_new ());
697         hildon_window_set_app_menu (HILDON_WINDOW (priv->window), menu);
698
699         filter = hildon_gtk_radio_button_new (HILDON_SIZE_FINGER_HEIGHT , NULL);
700         gtk_button_set_label (GTK_BUTTON (filter), _("Name"));
701         gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
702         g_signal_connect_after (filter, "clicked", G_CALLBACK (sort_by_name_clicked), priv);
703         hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
704
705         filter = hildon_gtk_radio_button_new_from_widget (HILDON_SIZE_FINGER_HEIGHT , GTK_RADIO_BUTTON (filter));
706         gtk_button_set_label (GTK_BUTTON (filter), _("Date"));
707         gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
708         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (filter), TRUE);
709         g_signal_connect_after (filter, "clicked", G_CALLBACK (sort_by_date_clicked), priv);
710         hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
711
712         item = hildon_gtk_button_new (HILDON_SIZE_AUTO);
713         gtk_button_set_label (GTK_BUTTON (item), _("Search"));
714         hildon_app_menu_append (menu, GTK_BUTTON (item));
715         g_signal_connect (item, "clicked", G_CALLBACK (search_menu_clicked), priv);
716
717         if (priv->n_contacts > 0)
718                 gtk_widget_show_all (GTK_WIDGET (menu));
719 }
720
721 static void
722 create_main_window (BirthdayData *priv, GtkListStore *store)
723 {
724         HildonProgram *program = NULL;
725         GtkWidget *main_vbox, *alignment, *pannable, *tree_view;
726         GtkTreeModel *filter;
727         GtkTreeViewColumn *column;
728         GtkCellRenderer *renderer;
729         GtkTreeSelection *selection;
730
731         g_return_if_fail (priv);
732
733         program = hildon_program_get_instance ();
734         g_set_application_name (_("Birthday"));
735
736         /* main window */
737         priv->window = hildon_stackable_window_new ();
738         hildon_program_add_window (program, HILDON_WINDOW (priv->window));
739
740         /* create main menu */
741         create_main_menu (priv);
742
743         /* aligment */
744         alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
745         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
746                                    HILDON_MARGIN_HALF, 0, HILDON_MARGIN_DEFAULT, HILDON_MARGIN_DEFAULT);
747         gtk_container_add (GTK_CONTAINER (priv->window), alignment);
748
749         /* main vbox */
750         main_vbox = gtk_vbox_new (FALSE, 0);
751         gtk_container_add (GTK_CONTAINER (alignment), main_vbox);
752
753         /* no_search_result label */
754         priv->label = gtk_label_new (_("No contacts with birthday"));
755         hildon_helper_set_logical_color (priv->label, GTK_RC_FG,
756                                          GTK_STATE_NORMAL, "SecondaryTextColor");
757         hildon_helper_set_logical_font (priv->label, "LargeSystemFont");
758         gtk_box_pack_start (GTK_BOX (main_vbox), priv->label, TRUE, TRUE, 0);
759
760         /* alignment for pannable area */
761         priv->view = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
762         gtk_alignment_set_padding (GTK_ALIGNMENT (priv->view),
763                                    0, 0, HILDON_MARGIN_DEFAULT, 0);
764         gtk_box_pack_start (GTK_BOX (main_vbox), priv->view, TRUE, TRUE, 0);
765
766         /* pannable for tree view */
767         pannable = hildon_pannable_area_new ();
768         g_object_set (G_OBJECT (pannable), "mov-mode", HILDON_MOVEMENT_MODE_VERT, NULL);
769         gtk_container_add (GTK_CONTAINER (priv->view), pannable);
770
771         /* sort list by next birthdays */
772         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
773                                               COLUMN_NEXT_BIRTHDAY, GTK_SORT_ASCENDING);
774         priv->sorted = GTK_TREE_MODEL (store);
775
776         /* filtered view */
777         filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (store), NULL);
778         gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter),
779                                                 birthday_filered_view_visible_func,
780                                                 priv,
781                                                 NULL);
782         gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter));
783         priv->filter = GTK_TREE_MODEL (filter);
784
785         /* tree view */
786         priv->tree_view = hildon_gtk_tree_view_new_with_model (HILDON_UI_MODE_EDIT, filter);
787         gtk_container_add (GTK_CONTAINER (pannable), priv->tree_view);
788
789         /* display column */
790         column = gtk_tree_view_column_new ();
791         gtk_tree_view_column_set_fixed_width (column, 709);
792         gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
793         renderer = gtk_cell_renderer_text_new ();
794         gtk_tree_view_column_pack_start (column, renderer, TRUE);
795         gtk_tree_view_column_set_attributes (column, renderer,
796                                              "markup", COLUMN_DISPLAY,
797                                              NULL);
798         g_object_set (G_OBJECT (renderer), "xpad", 10, NULL);
799         gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
800         priv->display_column = column;
801
802         /* avatar column */
803         column = gtk_tree_view_column_new ();
804         gtk_tree_view_column_set_fixed_width (column, 48);
805         gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
806         renderer = gtk_cell_renderer_pixbuf_new ();
807         gtk_tree_view_column_pack_end (column, renderer, FALSE);
808         gtk_tree_view_column_set_attributes (column, renderer,
809                                              "pixbuf", COLUMN_AVATAR,
810                                              NULL);
811         gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
812
813         /* search bar */
814         create_search_bar(priv);
815         gtk_box_pack_end (GTK_BOX (main_vbox), priv->search, FALSE, FALSE, 0);
816
817         gtk_widget_show_all (GTK_WIDGET (priv->window));
818         gtk_widget_hide (GTK_WIDGET (priv->search));
819
820         if (priv->n_contacts > 0) {
821                 gtk_widget_hide (GTK_WIDGET (priv->label));
822                 gtk_widget_show (GTK_WIDGET (priv->view));
823         } else {
824                 gtk_widget_show (GTK_WIDGET (priv->label));
825                 gtk_widget_hide (GTK_WIDGET (priv->view));
826         }
827
828         /* enable portrait mode support */
829         hildon_gtk_window_set_portrait_flags (GTK_WINDOW (priv->window),
830                                               HILDON_PORTRAIT_MODE_SUPPORT);
831
832         /* tree view signals */
833         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
834         gtk_tree_selection_unselect_all (selection);
835         g_signal_connect (selection, "changed", G_CALLBACK (on_selection_changed), priv);
836
837         /* window signals */
838         g_signal_connect (G_OBJECT (priv->window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
839         g_signal_connect (G_OBJECT (priv->window), "key-press-event", G_CALLBACK (on_key_press_event), priv);
840 }
841
842 static GList *
843 get_all_contacts (EBook *ebook)
844 {
845         GError *error = NULL;
846         EBookQuery *query;
847         GList *contacts;
848
849         ebook = e_book_new_system_addressbook (&error);
850         if (!ebook) {
851                 g_warning ("Error opening system address book: %s", error->message);
852                 g_error_free (error);
853                 return NULL;
854         }
855
856         if (!e_book_open (ebook, TRUE, &error)) {
857                 g_warning ("Error opening system address book: %s", error->message);
858                 g_error_free (error);
859                 return NULL;
860         }
861
862         query = e_book_query_any_field_contains ("");
863
864         if (!e_book_get_contacts (ebook, query, &contacts, &error)) {
865                 g_warning ("Error getting contacts: %s", error->message);
866                 g_error_free (error);
867                 return NULL;
868         }
869
870         return contacts;
871 }
872
873 int main (int argc, char **argv)
874 {
875         BirthdayData *data;
876         osso_context_t *osso_context;
877         EBook *ebook;
878         GtkWidget *window;
879         GtkListStore *store;
880         GList *contacts;
881
882         hildon_gtk_init (&argc, &argv);
883
884         /* create application data */
885         data = g_new0 (BirthdayData, 1);
886         data->searched_name = NULL;
887         data->found = TRUE;
888         data->n_contacts = 0;
889
890         /* initialize localization */
891         setlocale(LC_ALL, "");
892         bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
893         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
894         textdomain(GETTEXT_PACKAGE);
895
896         /* initialize osso */
897         osso_context = osso_initialize ("birthday", "0.1", TRUE, NULL);
898         if (osso_context == NULL) {
899                 g_critical ("Error initializing osso");
900                 return 1;
901         }
902
903         /* init abook */
904         if (!osso_abook_init (&argc, &argv, osso_context)) {
905                 g_critical ("Error initializing libosso-abook");
906                 goto exit;
907         }
908
909         contacts = get_all_contacts (ebook);
910         store = create_bday_liststore (data, contacts);
911
912         /* create main widow */
913         create_main_window (data, store);
914
915         /* get the system dbus connection */
916         DBusConnection *dbus_connection = osso_get_sys_dbus_connection (osso_context);
917
918         /* add the callback, which should be called, once the device is rotated */
919         dbus_bus_add_match (dbus_connection, MCE_MATCH_RULE, NULL);
920         dbus_connection_add_filter (dbus_connection, dbus_handle_mce_message, data, NULL);
921
922         /* check if device is not already in portrait orientation */
923         if (is_portrait_mode (osso_context))
924                 set_portrait_mode (data, TRUE);
925
926         gtk_main ();
927
928 exit:
929         osso_deinitialize (osso_context);
930         return 0;
931 }