In wizard, if country picker change, only refresh the plugins entries in
[modest] / src / hildon2 / modest-maemo-utils.c
index 2d81c68..353b72f 100644 (file)
 #include "modest-text-utils.h"
 #include "modest-platform.h"
 #include "modest-ui-constants.h"
+#include <hildon/hildon-picker-dialog.h>
+#ifdef MODEST_USE_IPHB
+#include <iphbd/libiphb.h>
+#endif
 
-/*
- * For getting and tracking the Bluetooth name
- */
-#define BTNAME_SERVICE                  "org.bluez"
-#define BTNAME_REQUEST_IF               "org.bluez.Adapter"
-#define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
-#define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
-#define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
-
-#define BTNAME_REQ_GET                  "GetName"
-#define BTNAME_SIG_CHANGED              "NameChanged"
-
-#define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
-                          "',member='" BTNAME_SIG_CHANGED "'"
+/* Label child of a captioned */
+#define CAPTIONED_LABEL_CHILD "captioned-label"
 
+#ifdef MODEST_PLATFORM_MAEMO
+#define INTERNAL_MMC_USB_MODE  "/system/osso/af/internal-mmc-used-over-usb"
+#endif
 
 static osso_context_t *__osso_context = NULL; /* urgh global */
 
@@ -75,79 +70,113 @@ osso_context_t *
 modest_maemo_utils_get_osso_context (void)
 {
        if (!__osso_context) 
-               g_warning ("%s: __osso_context == NULL", __FUNCTION__);
-       
-       return __osso_context;
-}
+               __osso_context = osso_initialize(PACKAGE,PACKAGE_VERSION,
+                                                FALSE, NULL);
 
-void
-modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
-{
-       g_return_if_fail (osso_context);
-       __osso_context = osso_context;
+       return __osso_context;
 }
 
 static void
-update_device_name_from_msg (DBusMessage *message)
+get_properties_cb (DBusMessage *message)
 {
-       DBusError error;
        DBusMessageIter iter;
+       DBusMessageIter dict_iter;
+       DBusMessageIter dict_entry_iter;
+       DBusError err;
+       gchar *bt_name = NULL;
+       int key_type, array_type, msg_type;
+
+       dbus_error_init(&err);
+       if (dbus_set_error_from_message (&err, message)) {
+               g_warning ("%s: %s", __FUNCTION__, err.message);
+       }
 
-       dbus_error_init (&error);
-
-       if (dbus_set_error_from_message (&error, message)) {
-               g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
-               dbus_error_free (&error);
-       } else {
-               const gchar *device_name;
-               if (!dbus_message_iter_init (message, &iter)) {
-                       g_printerr ("modest: message did not have argument\n");
-                       return;
+       /* Get msg type */
+       dbus_message_iter_init (message, &iter);
+       msg_type = dbus_message_iter_get_arg_type (&iter);
+       dbus_message_iter_recurse (&iter, &dict_iter);
+
+       while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY) {
+
+               dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
+
+               while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING) {
+                       DBusMessageIter dict_entry_content_iter;
+                       char *key;
+                       char *value;
+                       int dict_entry_type;
+                       int dict_entry_content_type;
+
+                       dbus_message_iter_get_basic (&dict_entry_iter, &key);
+                       dbus_message_iter_next (&dict_entry_iter);
+                       dict_entry_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
+                       dbus_message_iter_recurse (&dict_entry_iter, &dict_entry_content_iter);
+                       dict_entry_content_type = dbus_message_iter_get_arg_type (&dict_entry_content_iter);
+
+                       if (dict_entry_content_type == DBUS_TYPE_STRING) {
+                               dbus_message_iter_get_basic ( &dict_entry_content_iter, &value );
+
+                               if (strcmp (key, "Name") == 0 ) {
+                                       g_debug ("-------------Name %s", value);
+                                       bt_name = value;
+                                       break;
+                               }
+                       }
+                       dbus_message_iter_next (&dict_entry_iter);
                }
-               dbus_message_iter_get_basic (&iter, &device_name);
+
+               if (key_type != DBUS_TYPE_INVALID)
+                       break;
+
+               dbus_message_iter_next (&dict_iter);
+       }
+
+       /* Save device name */
+       if (bt_name) {
+               g_debug ("Setting the device name %s", bt_name);
                modest_conf_set_string (modest_runtime_get_conf(),
-                                       MODEST_CONF_DEVICE_NAME, device_name,
+                                       MODEST_CONF_DEVICE_NAME, bt_name,
                                        NULL);
        }
 }
 
-
 static void
-on_device_name_received (DBusPendingCall *call, void *user_data)
+get_default_adapter_cb (DBusConnection *conn,
+                       DBusMessage *message)
 {
-       DBusMessage *message;
-       
-       g_return_if_fail (dbus_pending_call_get_completed (call));
-       
-       message = dbus_pending_call_steal_reply (call);
-       if (!message) {
-               g_printerr ("modest: no reply on device name query\n");
-               return;
-       }
+       DBusMessageIter iter;
+       gchar* path = NULL;
+       DBusError error;
+       DBusMessage *msg, *adapterMsg;
 
-       update_device_name_from_msg (message);
-       dbus_message_unref (message);
-}
+       dbus_message_iter_init (message, &iter);
+       dbus_message_iter_get_basic (&iter, &path);
 
+       if (!path)
+               return;
 
-static DBusHandlerResult
-handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
-{
-       if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
-               update_device_name_from_msg (msg);
+       adapterMsg = dbus_message_new_method_call ("org.bluez", path,
+                                                  "org.bluez.Adapter",
+                                                  "GetProperties");
 
-       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       dbus_error_init (&error);
+       msg = dbus_connection_send_with_reply_and_block (conn, adapterMsg, -1, &error);
+       if (msg) {
+               g_debug ("Getting the properties");
+               get_properties_cb (msg);
+               dbus_message_unref (msg);
+       }
+       dbus_message_unref (adapterMsg);
 }
 
-
-static void
-get_device_name_from_dbus ()
+void
+modest_maemo_utils_get_device_name (void)
 {
        static DBusConnection *conn = NULL;
        DBusMessage *request;
        DBusError error;
-       DBusPendingCall *call = NULL;
-       
+       DBusMessage *msg;
+
        dbus_error_init (&error);
        if (!conn) {
                conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
@@ -158,262 +187,188 @@ get_device_name_from_dbus ()
                        return;
                }
        }
-       
-       request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
-                                               BTNAME_REQUEST_IF, BTNAME_REQ_GET);
-       if (!request) {
-               /* should we free the connection? */
-               g_printerr ("modest: dbus_message_new_method_call failed\n");
-               return;
-       }
-       dbus_message_set_auto_start (request, TRUE);
-       if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
-               dbus_pending_call_set_notify (call, on_device_name_received,
-                                             NULL, NULL);
-               dbus_pending_call_unref (call);
-       }
-       dbus_message_unref (request);
-       
-       dbus_connection_setup_with_g_main (conn, NULL);
-       dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
-       if (dbus_error_is_set(&error)) {
-               g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
-               dbus_error_free (&error);
-       }
 
-       if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
-               g_printerr ("modest: dbus_connection_add_filter failed\n");
-}
+       /* Get the default adapter */
+       request = dbus_message_new_method_call ("org.bluez", "/" ,
+                                               "org.bluez.Manager",
+                                               "DefaultAdapter");
 
+       msg = dbus_connection_send_with_reply_and_block (conn, request, -1, &error);
+       if (msg) {
+               g_debug ("Getting the default adapter");
+               get_default_adapter_cb (conn, msg);
+               dbus_message_unref (msg);
+       }
 
-void
-modest_maemo_utils_get_device_name (void)
-{
-       get_device_name_from_dbus ();
+       dbus_message_unref (request);
+       if (dbus_error_is_set (&error))
+               dbus_error_free (&error);
 }
 
 void
 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
 {
-       gchar *images_folder;
        GtkFileFilter *file_filter;
        GList *image_mimetypes_list;
        GList *node;
+       gchar *conf_folder;
 
        g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
 
-       /* Set the default folder to images folder */
-       images_folder = g_build_filename (g_get_home_dir (), 
-                                         MODEST_MAEMO_UTILS_MYDOCS_FOLDER,
-                                         MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
-       gtk_file_chooser_set_current_folder (chooser, images_folder);
-       g_free (images_folder);
+       conf_folder = modest_conf_get_string (modest_runtime_get_conf (),
+                                             MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL);
+       if (conf_folder && conf_folder[0] != '\0') {
+               gtk_file_chooser_set_current_folder_uri (chooser, conf_folder);
+       } else {
+               gchar *images_folder;
+               /* Set the default folder to images folder */
+               images_folder = (gchar *) g_strdup(g_get_user_special_dir (G_USER_DIRECTORY_PICTURES));
+               if (!images_folder) {
+                       /* fallback */
+                       images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
+                                                         MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
+               }
+               gtk_file_chooser_set_current_folder (chooser, images_folder);
+               g_free (images_folder);
+       }
+       g_free (conf_folder);
 
        /* Set the images mime filter */
        file_filter = gtk_file_filter_new ();
-#ifdef MODEST_HAVE_HILDON0_WIDGETS
-       image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
-#else
        image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
-#endif
        for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
                gtk_file_filter_add_mime_type (file_filter, node->data);
        }
        gtk_file_chooser_set_filter (chooser, file_filter);
-#ifdef MODEST_HAVE_HILDON0_WIDGETS
-       osso_mime_types_list_free (image_mimetypes_list);
-#else
        hildon_mime_types_list_free (image_mimetypes_list);
-#endif
 
 }
 
-void
-modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
-                                     gboolean thumbable)
+#ifdef MODEST_PLATFORM_MAEMO
+gboolean
+modest_maemo_utils_in_usb_mode ()
 {
-       g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
-#ifdef MODEST_HAVE_HILDON1_WIDGETS             
-       hildon_helper_set_thumb_scrollbar (win, thumbable);
-#endif /* MODEST_HAVE_HILDON1_WIDGETS */
+       return modest_conf_get_bool (modest_runtime_get_conf (), INTERNAL_MMC_USB_MODE, NULL);
 }
+#endif
+
+#ifdef MODEST_USE_IPHB
 
-FILE*
-modest_maemo_open_mcc_mapping_file (void)
+typedef struct _ModestHeartbeatSource {
+       GSource source;
+       iphb_t iphb;
+       GPollFD poll;
+       gint interval;
+} ModestHeartbeatSource;
+
+static gboolean modest_heartbeat_prepare (GSource* source, gint *timeout)
 {
-       FILE* result;
-       
-       const gchar* path;
-       const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
-       const gchar* path2 = MODEST_MCC_MAPPING;
-       
-       if (access(path1, R_OK) == 0) 
-               path = path1;
-       else if (access(path2, R_OK) == 0)
-               path = path2;
-       else {
-               g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
-                          __FUNCTION__, path1, path2);
-               return NULL;
-       }
-       
-       result = fopen (path, "r");
-       if (!result) {
-               g_warning ("%s: error opening mapping file '%s': %s",
-                          __FUNCTION__, path, strerror(errno));
-               return NULL;
-       }
-       return result;
+    *timeout = -1;
+    return FALSE;
 }
 
-GtkWidget *
-modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
-                                               const gchar *item_name)
+static gboolean 
+modest_heartbeat_check(GSource* source)
 {
-       GtkWidget *new_menu;
-       GtkWidget *menubar;
-       GList *children, *iter;
+       return ((ModestHeartbeatSource *) source)->poll.revents != 0;
+}
 
-       menubar = gtk_ui_manager_get_widget (manager, item_name);
-       new_menu = gtk_menu_new ();
+static gboolean modest_heartbeat_dispatch (GSource *source, GSourceFunc callback, gpointer userdata)
+{
+    if (callback(userdata))
+    {
+           ModestHeartbeatSource *hb_source = (ModestHeartbeatSource *) source;
 
-       children = gtk_container_get_children (GTK_CONTAINER (menubar));
-       for (iter = children; iter != NULL; iter = g_list_next (iter)) {
-               GtkWidget *menu;
+           g_source_remove_poll (source, &(hb_source->poll));
 
-               menu = GTK_WIDGET (iter->data);
-               gtk_widget_reparent (menu, new_menu);
-       }
-       
-       g_list_free (children);
+           int min = MAX(hb_source->interval - 30, 5);
+           iphb_wait(hb_source->iphb, min, min + 60, 0);
 
-       return new_menu;
-}
+           hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
+           hb_source->poll.events = G_IO_IN;
+           hb_source->poll.revents = 0;
 
-/**
- * modest_maemo_utils_create_captioned:
- * @title_size_group: a #GtkSizeGroup
- * @value_size_group: a #GtkSizeGroup
- * @title: a string
- * @control: a #GtkWidget
- *
- * this creates a widget (a #GtkHBox) with a control, and a label
- * (@string) captioning it. It also uses the proper size groups for title
- * and control.
- *
- * Returns: a widget containing the control and a proper label.
- */
-GtkWidget *
-modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
-                                       GtkSizeGroup *value_size_group,
-                                       const gchar *title,
-                                       GtkWidget *control)
-{
-       return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
-                                                                  value_size_group,
-                                                                  title,
-                                                                  control,
-                                                                  HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
-}
+           g_source_add_poll(source, &(hb_source->poll));
 
-/**
- * modest_maemo_utils_create_captioned_with_size_type:
- * @title_size_group: a #GtkSizeGroup
- * @value_size_group: a #GtkSizeGroup
- * @title: a string
- * @control: a #GtkWidget
- * @size_type: a #HildonSizeType
- *
- * this creates a widget (a #GtkHBox) with a control, and a label
- * (@string) captioning it. It also uses the proper size groups for title
- * and control.
- *
- * Returns: a widget containing the control and a proper label.
- */
-GtkWidget *
-modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
-                                                      GtkSizeGroup *value_size_group,
-                                                      const gchar *title,
-                                                      GtkWidget *control,
-                                                      HildonSizeType size_type)
-{
-       GtkWidget *label;
-       GtkWidget *box;
-  
-       label = gtk_label_new (title);
-       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
-       gtk_widget_show (label);
-       box = gtk_hbox_new (TRUE, MODEST_MARGIN_HALF);
-       gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, MODEST_MARGIN_HALF);
-       gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, MODEST_MARGIN_HALF);
-       if (title_size_group)
-               gtk_size_group_add_widget (title_size_group, label);
-       if (value_size_group)
-               gtk_size_group_add_widget (value_size_group, control);
-
-       hildon_gtk_widget_set_theme_size (control, size_type);
-
-       return box;
+           return TRUE;
+    } else {
+           return FALSE;
+    }
 }
 
-/**
- * modest_maemo_utils_set_hbutton_layout:
- * @title_sizegroup: a #GtkSizeGroup, or %NULL
- * @value_sizegroup: a #GtkSizeGroup, or %NULL
- * @title: a string
- * @button: a #HildonButton
- *
- * Configures the alignment and layout of @button. If @title_sizegroup is provided,
- * the title will be aligned to the left using it. If @value_sizegroup is provided,
- * the value will be aligned to the left using it. It also sets the title
- * of the button.
- *
- * The alignment is left for the title and for the value.
- */
-void
-modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
-                                      GtkSizeGroup *value_sizegroup,
-                                      const gchar *title, 
-                                      GtkWidget *button)
+static void 
+modest_heartbeat_finalize (GSource* source)
 {
-       hildon_button_set_title (HILDON_BUTTON (button), title);
-       if (title_sizegroup)
-               hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
-       if (value_sizegroup)
-               hildon_button_add_title_size_group (HILDON_BUTTON (button), value_sizegroup);
-       hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
-       hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
-       hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
+       ModestHeartbeatSource* hb_source = (ModestHeartbeatSource *) source;
+       hb_source->iphb = iphb_close(hb_source->iphb);
 }
 
-void
-modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
-                                      const gchar *title, 
-                                      GtkWidget *button)
+GSourceFuncs modest_heartbeat_funcs =
+{
+  modest_heartbeat_prepare,
+  modest_heartbeat_check,
+  modest_heartbeat_dispatch,
+  modest_heartbeat_finalize,
+  NULL,
+  NULL
+};
+
+static GSource *
+modest_heartbeat_source_new (void)
 {
-       hildon_button_set_title (HILDON_BUTTON (button), title);
-       if (sizegroup) {
-               hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
-               hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
+       GSource *source;
+       ModestHeartbeatSource *hb_source;
+       iphb_t iphb;
+       int hb_interval;
+
+       source = NULL;
+       hb_interval = 0;
+
+       iphb = iphb_open (&hb_interval);
+
+       if (iphb != 0) {
+               int min;
+               source = g_source_new (&modest_heartbeat_funcs, sizeof (ModestHeartbeatSource));
+               hb_source = (ModestHeartbeatSource *) source;
+               g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
+               hb_source->iphb = iphb;
+               hb_source->interval = hb_interval;
+
+               min = MAX(hb_interval - 30, 5);
+               iphb_wait(hb_source->iphb, min, min + 60, 0);
+
+               hb_source->poll.fd = iphb_get_fd(hb_source->iphb);
+               hb_source->poll.events = G_IO_IN;
+               hb_source->poll.revents = 0;
+
+               g_source_add_poll(source, &(hb_source->poll));
+       } else {
+               source = g_idle_source_new ();
        }
-       hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
-       hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
-       hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
+
+       return source;
 }
 
-GtkWidget *
-modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
+guint
+modest_heartbeat_add (GSourceFunc function,
+                     gpointer userdata)
 {
-       GtkWidget *label;
-       GtkWidget *box;
+       GSource *source;
+       guint id;
 
-       label = gtk_label_new (label_text);
-       gtk_widget_show (label);
+       source = modest_heartbeat_source_new ();
+       g_source_set_callback (source, function, userdata, NULL);
+       id = g_source_attach (source, NULL);
+       g_source_unref (source);
 
-       box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
-       gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
-       gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
-       gtk_widget_show (box);
+       return id;
+}
 
-       return box;
+#else
+guint
+modest_heartbeat_add (GSourceFunc function,
+                     gpointer userdata)
+{
+       return g_idle_add (function, userdata);
 }
+#endif