Added posibility to call or send SMS to selected contact.
[birthday] / src / birthday.c
index 00c200c..52c472a 100644 (file)
 #include <libebook/e-book.h>
 #include <libosso-abook/osso-abook.h>
 
+#include <mce/dbus-names.h>
+#include <mce/mode-names.h>
+
+#define MCE_MATCH_RULE "type='signal',interface='" MCE_SIGNAL_IF "',member='" MCE_DEVICE_ORIENTATION_SIG "'"
+
 #define _HL(str) dgettext("hildon-libs",str)
 
 enum
@@ -46,6 +51,7 @@ enum
        COLUMN_DISPLAY,
        COLUMN_FULLNAME,
        COLUMN_NEXT_BIRTHDAY,
+       COLUMN_ABOOK_CONTACT,
        NUM_COLS
 };
 
@@ -57,6 +63,8 @@ struct _BirthdayData {
        GtkWidget *view;
        GtkWidget *search;
 
+       GtkTreeViewColumn *display_column;
+
        GtkWidget *tree_view;
        GtkTreeModel *sorted;
        GtkTreeModel *filter;
@@ -67,6 +75,54 @@ struct _BirthdayData {
        guint n_contacts;
 };
 
+static void set_portrait_mode (BirthdayData *priv, gboolean enable);
+
+static gboolean
+is_portrait_mode (osso_context_t *osso_context)
+{
+       osso_rpc_t ret;
+       gboolean result = FALSE;
+
+       if (osso_rpc_run_system (osso_context, MCE_SERVICE, MCE_REQUEST_PATH,
+                                MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET,
+                                &ret, DBUS_TYPE_INVALID) == OSSO_OK) {
+
+               if (strcmp (ret.value.s, MCE_ORIENTATION_PORTRAIT) == 0) {
+                       result = TRUE;
+                }
+                osso_rpc_free_val (&ret);
+       } else {
+               g_critical ("ERROR: Call do DBus failed\n");
+       }
+       return result;
+}
+
+static DBusHandlerResult
+dbus_handle_mce_message (DBusConnection *connection,
+                        DBusMessage *message,
+                        gpointer data)
+{
+       DBusMessageIter iter;
+       const gchar *orientation = NULL;
+       BirthdayData *priv;
+
+       g_return_val_if_fail (data, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+       priv = (BirthdayData *) data;
+
+       if (dbus_message_is_signal (message, MCE_SIGNAL_IF, MCE_DEVICE_ORIENTATION_SIG)) {
+               if (dbus_message_iter_init (message, &iter)) {
+                       dbus_message_iter_get_basic(&iter, &orientation);
+                       if (orientation) {
+                               if (!strcmp (orientation, MCE_ORIENTATION_PORTRAIT))
+                                       set_portrait_mode (priv, TRUE);
+                               else
+                                       set_portrait_mode (priv, FALSE);
+                       }
+               }
+       }
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
 static gboolean
 birthday_filered_view_visible_func (GtkTreeModel *model,
                                    GtkTreeIter *iter,
@@ -163,6 +219,7 @@ static void
 on_search_entry_changed (GtkEditable *editable,
                         gpointer data)
 {
+       GtkTreeSelection *selection;
        BirthdayData *priv;
 
        g_return_if_fail (data);
@@ -174,6 +231,12 @@ on_search_entry_changed (GtkEditable *editable,
                g_free (priv->searched_name);
        priv->searched_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (editable)));
 
+       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
+
+       /* ugly hack, set back mode to selection none to not generate "changed"
+          signal during re-filtering  */
+       gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
+
        /* refilter tree view */
        gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
 
@@ -195,17 +258,30 @@ on_search_entry_changed (GtkEditable *editable,
        /* ugly, but working way how to scroll to the first row */
        gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->tree_view),
                                      gtk_tree_path_new_from_string ("0"), NULL, FALSE, 0, 0);
+
+       /* ugly hack, set back mode to single selection */
+       gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
+
+       /* unselect selected rows */
+       gtk_tree_selection_unselect_all (selection);
 }
 
 static void
 on_search_close_clicked (GtkButton *button,
                         gpointer data)
 {
+       GtkTreeSelection *selection;
        BirthdayData *priv;
 
        g_return_if_fail (data);
        priv = (BirthdayData *) data;
 
+       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
+
+       /* ugly hack, set back mode to selection none to not generate "changed"
+          signal during re-filtering  */
+       gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
+
        /* hide search bar */
        gtk_widget_hide (priv->search);
 
@@ -226,6 +302,12 @@ on_search_close_clicked (GtkButton *button,
        /* ugly, but working way how to scroll to the first row */
        gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->tree_view),
                                      gtk_tree_path_new_from_string ("0"), NULL, FALSE, 0, 0);
+
+       /* ugly hack, set back mode to single selection */
+       gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
+
+       /* unselect selected rows */
+       gtk_tree_selection_unselect_all (selection);
 }
 
 
@@ -260,6 +342,56 @@ on_key_press_event (GtkWidget *widget,
        return FALSE;
 }
 
+static void
+on_selection_changed (GtkTreeSelection *selection,
+                     gpointer data)
+{
+       BirthdayData *priv;
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+
+       g_return_if_fail (data);
+       priv = (BirthdayData *) data;
+
+       if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
+               OssoABookContact *abook_contact = NULL;
+
+               /* unselect selected rows */
+               gtk_tree_selection_unselect_all (selection);
+
+               gtk_tree_model_get (model, &iter, COLUMN_ABOOK_CONTACT, &abook_contact, -1);
+
+               if (abook_contact) {
+                       GtkWidget *starter, *dialog;
+                       OssoABookContactDetailStore *store;
+                       OssoABookContactAction actions[7] = {OSSO_ABOOK_CONTACT_ACTION_TEL,
+                                                            OSSO_ABOOK_CONTACT_ACTION_SMS,
+                                                            OSSO_ABOOK_CONTACT_ACTION_CHATTO,
+                                                            OSSO_ABOOK_CONTACT_ACTION_VOIPTO,
+                                                            OSSO_ABOOK_CONTACT_ACTION_VOIPTO_AUDIO,
+                                                            OSSO_ABOOK_CONTACT_ACTION_VOIPTO_VIDEO,
+                                                            OSSO_ABOOK_CONTACT_ACTION_MAILTO};
+
+                       store = osso_abook_contact_detail_store_new (abook_contact,
+                                                                    OSSO_ABOOK_CONTACT_DETAIL_EMAIL |
+                                                                    OSSO_ABOOK_CONTACT_DETAIL_PHONE |
+                                                                    OSSO_ABOOK_CONTACT_DETAIL_IM_VOICE |
+                                                                    OSSO_ABOOK_CONTACT_DETAIL_IM_VIDEO |
+                                                                    OSSO_ABOOK_CONTACT_DETAIL_IM_CHAT |
+                                                                    OSSO_ABOOK_CONTACT_DETAIL_SMS);
+
+                       starter = osso_abook_touch_contact_starter_new_with_store (store,
+                                                                                  (OssoABookContactAction *) &actions,
+                                                                                  sizeof (actions));
+
+                       dialog = osso_abook_touch_contact_starter_dialog_new (GTK_WINDOW (priv->window),
+                                                                             OSSO_ABOOK_TOUCH_CONTACT_STARTER (starter));
+                       gtk_widget_show_all (dialog);
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+               }
+       }
+}
+
 static unsigned int
 calc_age (EContactDate *bdate, time_t current_date)
 {
@@ -275,6 +407,7 @@ calc_age (EContactDate *bdate, time_t current_date)
        bday_tm.tm_mday = bdate->day;
        bday_tm.tm_mon = bdate->month - 1;
        bday_tm.tm_year = current_date_tm->tm_year;
+       bday_tm.tm_isdst = current_date_tm->tm_isdst;
 
        if (mktime (&bday_tm) > current_date) {
                age = (current_date_tm->tm_year + 1900) - bdate->year - 1;
@@ -303,6 +436,7 @@ calc_next_bday (EContactDate *bdate, time_t current_date)
        current_bday_tm.tm_mday = bdate->day;
        current_bday_tm.tm_mon = bdate->month - 1;
        current_bday_tm.tm_year = current_date_tm->tm_year;
+       current_bday_tm.tm_isdst = current_date_tm->tm_isdst;
        current_bday = mktime (&current_bday_tm);
 
        if (current_date > current_bday) {
@@ -312,6 +446,7 @@ calc_next_bday (EContactDate *bdate, time_t current_date)
                next_bday_tm.tm_mday = bdate->day;
                next_bday_tm.tm_mon = bdate->month - 1;
                next_bday_tm.tm_year = current_date_tm->tm_year + 1;
+               next_bday_tm.tm_isdst = current_date_tm->tm_isdst;
                next_bday = mktime (&next_bday_tm);
        } else {
                next_bday = current_bday;
@@ -344,6 +479,23 @@ get_text_color_by_name (const gchar *name)
        return gdk_color_to_string (&color);
 }
 
+static void
+set_portrait_mode (BirthdayData *priv,
+                  gboolean enable)
+{
+       g_return_if_fail (priv);
+
+       if (enable) {
+               gtk_tree_view_column_set_fixed_width (priv->display_column, 389);
+               hildon_gtk_window_set_portrait_flags (GTK_WINDOW (priv->window),
+                                                     HILDON_PORTRAIT_MODE_REQUEST);
+       } else {
+               gtk_tree_view_column_set_fixed_width (priv->display_column, 709);
+               hildon_gtk_window_set_portrait_flags (GTK_WINDOW (priv->window),
+                                                     ~HILDON_PORTRAIT_MODE_REQUEST);
+       }
+}
+
 static GtkListStore *
 create_bday_liststore (BirthdayData *priv, GList *contacts)
 {
@@ -374,7 +526,8 @@ create_bday_liststore (BirthdayData *priv, GList *contacts)
                                   GDK_TYPE_PIXBUF,     /* COLUMN_AVATAR */
                                   G_TYPE_STRING,       /* COLUMN_DISPLAY */
                                   G_TYPE_STRING,       /* COLUMN_FULLNAME */
-                                  G_TYPE_INT);         /* COLUMN_NEXT_BIRTHDAY */
+                                  G_TYPE_INT,          /* COLUMN_NEXT_BIRTHDAY */
+                                  G_TYPE_POINTER);     /* COLUMN_ABOOK_CONTACT */
 
        for (contact = contacts; contact != NULL; contact = contact->next) {
                EContactDate *bdate = NULL;
@@ -390,6 +543,7 @@ create_bday_liststore (BirthdayData *priv, GList *contacts)
                        gchar *next_birthday_text = NULL;
                        struct tm birthday_tm;
                        gchar birthday_text[11];
+                       OssoABookContact *abook_contact;
 
                        photo = e_contact_get (E_CONTACT (contact->data), E_CONTACT_PHOTO);
                        if (photo) {
@@ -448,16 +602,26 @@ create_bday_liststore (BirthdayData *priv, GList *contacts)
 
                        age = calc_age(bdate, current_date);
                        next_birthday = calc_next_bday(bdate, current_date);
-                       next_birthday_text = g_strdup_printf(ngettext ("next birthday in %d day", "next birthday in %d days", next_birthday), next_birthday);
-                       display_column = g_strdup_printf("%s <span font_desc=\"%s\" foreground=\"%s\"><sup>(%d)</sup>\n%s, %s</span>",
+
+                       if (next_birthday == 0)
+                               next_birthday_text = g_strdup_printf (_("has birthday today"));
+                       else
+                               next_birthday_text = g_strdup_printf (ngettext ("will have birthday tomorrow",
+                                                                               "will have birthday in %d days", next_birthday),
+                                                                     next_birthday);
+
+                       display_column = g_strdup_printf ("%s <span font_desc=\"%s\" foreground=\"%s\"><sup>(%d)</sup>\n%s, %s</span>",
                                                         fullname, text_font, text_color, age, birthday_text, next_birthday_text);
 
+                       abook_contact = osso_abook_contact_new_from_template (E_CONTACT (contact->data));
+
                        gtk_list_store_append (store, &iter);
                        gtk_list_store_set (store, &iter,
                                            COLUMN_AVATAR, avatar,
                                            COLUMN_DISPLAY, display_column,
                                            COLUMN_FULLNAME, fullname,
                                            COLUMN_NEXT_BIRTHDAY, next_birthday,
+                                           COLUMN_ABOOK_CONTACT, abook_contact,
                                            -1);
                        n_contacts++;
 
@@ -562,6 +726,7 @@ create_main_window (BirthdayData *priv, GtkListStore *store)
        GtkTreeModel *filter;
        GtkTreeViewColumn *column;
        GtkCellRenderer *renderer;
+       GtkTreeSelection *selection;
 
        g_return_if_fail (priv);
 
@@ -618,7 +783,7 @@ create_main_window (BirthdayData *priv, GtkListStore *store)
        priv->filter = GTK_TREE_MODEL (filter);
 
        /* tree view */
-       priv->tree_view = hildon_gtk_tree_view_new_with_model (HILDON_UI_MODE_NORMAL, filter);
+       priv->tree_view = hildon_gtk_tree_view_new_with_model (HILDON_UI_MODE_EDIT, filter);
        gtk_container_add (GTK_CONTAINER (pannable), priv->tree_view);
 
        /* display column */
@@ -631,8 +796,8 @@ create_main_window (BirthdayData *priv, GtkListStore *store)
                                             "markup", COLUMN_DISPLAY,
                                             NULL);
        g_object_set (G_OBJECT (renderer), "xpad", 10, NULL);
-
        gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
+       priv->display_column = column;
 
        /* avatar column */
        column = gtk_tree_view_column_new ();
@@ -649,10 +814,6 @@ create_main_window (BirthdayData *priv, GtkListStore *store)
        create_search_bar(priv);
        gtk_box_pack_end (GTK_BOX (main_vbox), priv->search, FALSE, FALSE, 0);
 
-       /* window signals */
-       g_signal_connect (G_OBJECT (priv->window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
-       g_signal_connect (G_OBJECT (priv->window), "key-press-event", G_CALLBACK (on_key_press_event), priv);
-
        gtk_widget_show_all (GTK_WIDGET (priv->window));
        gtk_widget_hide (GTK_WIDGET (priv->search));
 
@@ -663,6 +824,19 @@ create_main_window (BirthdayData *priv, GtkListStore *store)
                gtk_widget_show (GTK_WIDGET (priv->label));
                gtk_widget_hide (GTK_WIDGET (priv->view));
        }
+
+       /* enable portrait mode support */
+       hildon_gtk_window_set_portrait_flags (GTK_WINDOW (priv->window),
+                                             HILDON_PORTRAIT_MODE_SUPPORT);
+
+       /* tree view signals */
+       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
+       gtk_tree_selection_unselect_all (selection);
+       g_signal_connect (selection, "changed", G_CALLBACK (on_selection_changed), priv);
+
+       /* window signals */
+       g_signal_connect (G_OBJECT (priv->window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
+       g_signal_connect (G_OBJECT (priv->window), "key-press-event", G_CALLBACK (on_key_press_event), priv);
 }
 
 static GList *
@@ -738,6 +912,17 @@ int main (int argc, char **argv)
        /* create main widow */
        create_main_window (data, store);
 
+       /* get the system dbus connection */
+       DBusConnection *dbus_connection = osso_get_sys_dbus_connection (osso_context);
+
+       /* add the callback, which should be called, once the device is rotated */
+       dbus_bus_add_match (dbus_connection, MCE_MATCH_RULE, NULL);
+       dbus_connection_add_filter (dbus_connection, dbus_handle_mce_message, data, NULL);
+
+       /* check if device is not already in portrait orientation */
+       if (is_portrait_mode (osso_context))
+               set_portrait_mode (data, TRUE);
+
        gtk_main ();
 
 exit: