imported version 0.1-2
[birthday] / src / birthday.c
index e4881f1..022974b 100644 (file)
@@ -38,6 +38,8 @@
 #include <libebook/e-book.h>
 #include <libosso-abook/osso-abook.h>
 
+GtkWidget *tree_view;
+
 enum
 {
        COLUMN_AVATAR = 0,
@@ -47,62 +49,115 @@ enum
        NUM_COLS
 };
 
+static void
+sort_by_name_clicked (GtkButton *button,
+                      GtkLabel *label)
+{
+       GtkTreeModel *model;
+
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));
+       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
+                                             COLUMN_FULLNAME, GTK_SORT_ASCENDING);
+}
+
+static void
+sort_by_date_clicked (GtkButton *button,
+                      GtkLabel *label)
+{
+       GtkTreeModel *model;
+
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));
+       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
+                                             COLUMN_NEXT_BIRTHDAY, GTK_SORT_ASCENDING);
+
+}
+
+static void
+on_search_close_clicked (GtkButton *button,
+                        gpointer data)
+{
+       GtkWidget *search = GTK_WIDGET (data);
+
+       /* hide search bar */
+       gtk_widget_hide (search);
+
+       /* FIXME: refilter treeview */
+}
+
+
+static gboolean
+on_key_press_event (GtkWidget *widget,
+                   GdkEventKey *event,
+                   gpointer data)
+{
+       GtkWidget *search = GTK_WIDGET (data);
+
+       if ((event->keyval > GDK_space) && (event->keyval <= GDK_stricteq) && !GTK_WIDGET_VISIBLE (search)) {
+               /* show search bar */
+               gtk_widget_show (search);
+
+               /* FIXME: focus on entry */
+       }
+
+       return FALSE;
+}
+
 static unsigned int
 calc_age (EContactDate *bdate)
 {
-    struct tm tm_bday;
-    struct tm *tm_age;
-    time_t t_age;
-    int age = 0;
-
-    tm_bday.tm_sec = 0;
-    tm_bday.tm_min = 0;
-    tm_bday.tm_hour = 0;
-    tm_bday.tm_mday = bdate->day;
-    tm_bday.tm_mon = bdate->month - 1;
-    tm_bday.tm_year = bdate->year - 1900;
-
-    t_age = time (NULL) - mktime (&tm_bday);
-    tm_age = gmtime (&t_age);
-    age = tm_age->tm_year - 70;
-
-    if (age < 0)
-       age = 0;
-
-    return age;
+       struct tm tm_bday;
+       struct tm *tm_age;
+       time_t t_age;
+       int age = 0;
+
+       tm_bday.tm_sec = 0;
+       tm_bday.tm_min = 0;
+       tm_bday.tm_hour = 0;
+       tm_bday.tm_mday = bdate->day;
+       tm_bday.tm_mon = bdate->month - 1;
+       tm_bday.tm_year = bdate->year - 1900;
+
+       t_age = time (NULL) - mktime (&tm_bday);
+       tm_age = gmtime (&t_age);
+       age = tm_age->tm_year - 70;
+
+       if (age < 0)
+               age = 0;
+
+       return age;
 }
 
 static unsigned int
 calc_next_bday (EContactDate *bdate)
 {
-    struct tm tm_current_bday, tm_next_bday;
-    struct tm *tm_current_date;
-    time_t t_current_date, t_current_bday, t_next_bday, t_next_bday_in;
-
-    t_current_date = time (NULL);
-    tm_current_date = localtime (&t_current_date);
-
-    tm_current_bday.tm_sec = 0;
-    tm_current_bday.tm_min = 0;
-    tm_current_bday.tm_hour = 0;
-    tm_current_bday.tm_mday = bdate->day;
-    tm_current_bday.tm_mon = bdate->month - 1;
-    tm_current_bday.tm_year = tm_current_date->tm_year;
-    t_current_bday = mktime (&tm_current_bday);
-
-    if (t_current_date > t_current_bday) {
-       tm_next_bday.tm_sec = 0;
-       tm_next_bday.tm_min = 0;
-       tm_next_bday.tm_hour = 0;
-       tm_next_bday.tm_mday = bdate->day;
-       tm_next_bday.tm_mon = bdate->month - 1;
-       tm_next_bday.tm_year = tm_current_date->tm_year + 1;
-       t_next_bday = mktime (&tm_next_bday);
-    } else {
-       t_next_bday = t_current_bday;
-    }
-    t_next_bday_in = t_next_bday - t_current_date;
-    return (t_next_bday_in / 86400) + 1;
+       struct tm tm_current_bday, tm_next_bday;
+       struct tm *tm_current_date;
+       time_t t_current_date, t_current_bday, t_next_bday;
+
+       t_current_date = time (NULL);
+       tm_current_date = localtime (&t_current_date);
+
+       tm_current_bday.tm_sec = 0;
+       tm_current_bday.tm_min = 0;
+       tm_current_bday.tm_hour = 0;
+       tm_current_bday.tm_mday = bdate->day;
+       tm_current_bday.tm_mon = bdate->month - 1;
+       tm_current_bday.tm_year = tm_current_date->tm_year;
+       t_current_bday = mktime (&tm_current_bday);
+
+       if (t_current_date > t_current_bday) {
+               tm_next_bday.tm_sec = 0;
+               tm_next_bday.tm_min = 0;
+               tm_next_bday.tm_hour = 0;
+               tm_next_bday.tm_mday = bdate->day;
+               tm_next_bday.tm_mon = bdate->month - 1;
+               tm_next_bday.tm_year = tm_current_date->tm_year + 1;
+               t_next_bday = mktime (&tm_next_bday);
+       } else {
+               t_next_bday = t_current_bday;
+       }
+
+       return (t_next_bday - t_current_date) / 86400;
 }
 
 static gchar *
@@ -130,7 +185,7 @@ get_text_color_by_name (const gchar *name)
 }
 
 static GtkListStore *
-create_bday_liststore(GList *contacts)
+create_bday_liststore (GList *contacts)
 {
        GtkListStore *store;
        GtkTreeIter iter;
@@ -148,34 +203,65 @@ create_bday_liststore(GList *contacts)
                                   G_TYPE_INT);         /* COLUMN_NEXT_BIRTHDAY */
 
        for (contact = contacts; contact != NULL; contact = contact->next) {
-               EContactDate *bdate;
+               EContactDate *bdate = NULL;
 
                bdate = e_contact_get (E_CONTACT (contact->data), E_CONTACT_BIRTH_DATE);
                if (bdate) {
-                       EContactPhoto *photo;
+                       EContactPhoto *photo = NULL;
                        GError *error = NULL;
-                       GdkPixbuf *avatar;
-                       gchar *avatar_filename = NULL;
+                       GdkPixbuf *avatar = NULL;
                        gchar *fullname = NULL;
                        guint age = 0, next_birthday = 0;
                        gchar *display_column = NULL;
                        gchar *next_birthday_text = NULL;
 
                        photo = e_contact_get (E_CONTACT (contact->data), E_CONTACT_PHOTO);
-
                        if (photo) {
-                               avatar_filename = g_filename_from_uri (photo->data.uri, NULL, NULL);
-                               if (avatar_filename) {
-                                       avatar = gdk_pixbuf_new_from_file_at_size (avatar_filename, 48, 48, &error);
-                                       g_free (avatar_filename);
-                                       avatar_filename = NULL;
+                               if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
+                                       GdkPixbufLoader *loader;
+
+                                       loader = gdk_pixbuf_loader_new ();
+                                       if (gdk_pixbuf_loader_write (loader, (guchar *) photo->data.inlined.data, photo->data.inlined.length, NULL))
+                                               avatar = gdk_pixbuf_loader_get_pixbuf (loader);
+
+                               } else {
+                                       gchar *avatar_filename = NULL;
+
+                                       avatar_filename = g_filename_from_uri (photo->data.uri, NULL, NULL);
+                                       if (avatar_filename) {
+                                               avatar = gdk_pixbuf_new_from_file (avatar_filename, &error);
+                                               g_free (avatar_filename);
+                                       }
+                               }
+
+                               if (avatar) {
+                                       gint height = gdk_pixbuf_get_height (avatar);
+                                       if (height != 48) {
+                                               gint new_height = 48;
+                                               gint new_width = new_height * gdk_pixbuf_get_width (avatar) / height;
+                                               avatar = gdk_pixbuf_scale_simple (avatar, new_width, new_height, GDK_INTERP_BILINEAR);
+                                       }
                                }
-                               
+                               e_contact_photo_free (photo);
+                               photo = NULL;
                        } else {
                                avatar = gdk_pixbuf_new_from_file ("/usr/share/icons/hicolor/48x48/hildon/general_default_avatar.png", &error);
                        }
 
                        fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_FULL_NAME);
+                       if (!fullname) {
+                               fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_GIVEN_NAME);
+                               if (!fullname) {
+                                       fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_FAMILY_NAME);
+                                       if (!fullname) {
+                                               fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_NICKNAME);
+                                               if (!fullname) {
+                                                       fullname = e_contact_get (E_CONTACT (contact->data), E_CONTACT_ORG);
+                                               }
+                                       }
+                               }
+                       }
+
                        age = calc_age(bdate);
                        next_birthday = calc_next_bday(bdate);
                        next_birthday_text = g_strdup_printf(ngettext ("next birthday in %d day", "next birthday in %d days", next_birthday), next_birthday);
@@ -190,10 +276,21 @@ create_bday_liststore(GList *contacts)
                                            COLUMN_NEXT_BIRTHDAY, next_birthday,
                                            -1);
 
+                       if (display_column)
+                               g_free (display_column);
+                       display_column = NULL;
+
+                       if (fullname)
+                               g_free (fullname);
+                       fullname = NULL;
+
                        if (next_birthday_text)
                                g_free (next_birthday_text);
                        next_birthday_text = NULL;
+
+                       e_contact_date_free (bdate);
                }
+               bdate = NULL;
        }
 
        if (text_font)
@@ -206,10 +303,66 @@ create_bday_liststore(GList *contacts)
 }
 
 static GtkWidget *
-create_main_window(GtkListStore *store)
+create_search_bar ()
+{
+       GtkWidget *hbox, *entry, *button;
+       GtkEntryCompletion *completion;
+
+       /* search hbox */
+       hbox = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
+
+       /* search entry */
+       entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
+       hildon_gtk_entry_set_input_mode (GTK_ENTRY (entry), HILDON_GTK_INPUT_MODE_FULL);
+       gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
+
+       completion = gtk_entry_completion_new ();
+       gtk_entry_completion_set_inline_completion (completion, TRUE);
+       gtk_entry_completion_set_popup_completion (completion, FALSE);
+       gtk_entry_set_completion (GTK_ENTRY (entry), completion);
+
+       /* clear button */
+       button = GTK_WIDGET (gtk_tool_button_new (gtk_image_new_from_icon_name
+                            ("general_close", (GtkIconSize) HILDON_ICON_PIXEL_SIZE_FINGER), "Clear"));
+       gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, TRUE, 0);
+
+       /* search signals */
+//     g_signal_connect (entry, "changed", G_CALLBACK (onSearchEntryChanged), this);
+       g_signal_connect (button, "clicked", G_CALLBACK (on_search_close_clicked), hbox);
+
+       return hbox;
+}
+
+static void
+create_main_menu (GtkWidget *window)
+{
+       HildonAppMenu *menu;
+       GtkWidget *filter;
+
+       menu = HILDON_APP_MENU (hildon_app_menu_new ());
+       hildon_window_set_app_menu (HILDON_WINDOW (window), menu);
+
+       filter = hildon_gtk_radio_button_new (HILDON_SIZE_FINGER_HEIGHT , NULL);
+       gtk_button_set_label (GTK_BUTTON (filter), _("Name"));
+       gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
+       g_signal_connect_after (filter, "clicked", G_CALLBACK (sort_by_name_clicked), NULL);
+       hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
+
+       filter = hildon_gtk_radio_button_new_from_widget (HILDON_SIZE_FINGER_HEIGHT , GTK_RADIO_BUTTON (filter));
+       gtk_button_set_label (GTK_BUTTON (filter), _("Date"));
+       gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (filter), TRUE);
+       g_signal_connect_after (filter, "clicked", G_CALLBACK (sort_by_date_clicked), NULL);
+       hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
+
+       gtk_widget_show_all (GTK_WIDGET (menu));
+}
+
+static GtkWidget *
+create_main_window (GtkListStore *store)
 {
        HildonProgram *program = NULL;
-       GtkWidget *window, *main_vbox, *alignment, *label, *pannable, *tree_view;
+       GtkWidget *window, *main_vbox, *alignment, *label, *pannable, *search;
        GtkTreeViewColumn *column;
        GtkCellRenderer *renderer;
 
@@ -240,22 +393,20 @@ create_main_window(GtkListStore *store)
        /* alignment for pannable area */
        alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
        gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
-                                  0, 0, HILDON_MARGIN_DEFAULT, HILDON_MARGIN_DEFAULT);
+                                  0, 0, HILDON_MARGIN_DEFAULT, 0);
        gtk_box_pack_start (GTK_BOX (main_vbox), alignment, TRUE, TRUE, 0);
 
        /* pannable for tree view */
        pannable = hildon_pannable_area_new ();
+       g_object_set (G_OBJECT (pannable), "mov-mode", HILDON_MOVEMENT_MODE_VERT, NULL);
        gtk_container_add (GTK_CONTAINER (alignment), pannable);
 
        /* tree view */
        tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
-       gtk_container_add(GTK_CONTAINER(pannable), tree_view);
-       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
-                                             COLUMN_NEXT_BIRTHDAY, GTK_SORT_ASCENDING);
-
+       gtk_container_add (GTK_CONTAINER (pannable), tree_view);
        /* display column */
        column = gtk_tree_view_column_new ();
-       gtk_tree_view_column_set_fixed_width (column, 704);
+       gtk_tree_view_column_set_fixed_width (column, 709);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
        renderer = gtk_cell_renderer_text_new ();
        gtk_tree_view_column_pack_start (column, renderer, TRUE);
@@ -268,7 +419,7 @@ create_main_window(GtkListStore *store)
 
        /* avatar column */
        column = gtk_tree_view_column_new ();
-       gtk_tree_view_column_set_fixed_width (column, 64);
+       gtk_tree_view_column_set_fixed_width (column, 48);
        gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
        renderer = gtk_cell_renderer_pixbuf_new ();
        gtk_tree_view_column_pack_end (column, renderer, FALSE);
@@ -277,13 +428,17 @@ create_main_window(GtkListStore *store)
                                             NULL);
        gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
 
-
+       /* search bar */
+       search = create_search_bar();
+       gtk_box_pack_end (GTK_BOX (main_vbox), search, FALSE, FALSE, 0);
 
        /* window signals */
        g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
+//     g_signal_connect (G_OBJECT (window), "key-press-event", G_CALLBACK (on_key_press_event), search);
 
-       gtk_widget_show_all (GTK_WIDGET(window));
-       gtk_widget_hide (GTK_WIDGET(label));
+       gtk_widget_show_all (GTK_WIDGET (window));
+       gtk_widget_hide (GTK_WIDGET (label));
+       gtk_widget_hide (GTK_WIDGET (search));
        return window;
 }
 
@@ -339,10 +494,16 @@ int main (int argc, char **argv)
                goto exit;
        }
 
-       store = create_bday_liststore(contacts);
-       window = create_main_window(store);
+       store = create_bday_liststore (contacts);
+       window = create_main_window (store);
+       g_object_unref (G_OBJECT (store));
+       create_main_menu (window);
+
+       /* sort list by next birthdays */
+       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
+                                             COLUMN_NEXT_BIRTHDAY, GTK_SORT_ASCENDING);
 
-       gtk_main();
+       gtk_main ();
 
 exit:
        osso_deinitialize (osso_context);