* Fixes NB#104896, do not show purged attachments in "Save attachment" dialog
[modest] / src / hildon2 / modest-maemo-utils.c
index 12a1817..6b37582 100644 (file)
@@ -31,6 +31,7 @@
 #define DBUS_API_SUBJECT_TO_CHANGE
 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
 
+#include <sys/utsname.h>
 #include <dbus/dbus.h>
 #include <dbus/dbus-glib-lowlevel.h>
 #include <glib.h>
@@ -52,6 +53,7 @@
 #include "modest-text-utils.h"
 #include "modest-platform.h"
 #include "modest-ui-constants.h"
+#include <hildon/hildon-picker-dialog.h>
 
 /*
  * For getting and tracking the Bluetooth name
@@ -68,6 +70,9 @@
 #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"
+
 
 static osso_context_t *__osso_context = NULL; /* urgh global */
 
@@ -76,7 +81,7 @@ modest_maemo_utils_get_osso_context (void)
 {
        if (!__osso_context) 
                g_warning ("%s: __osso_context == NULL", __FUNCTION__);
-       
+
        return __osso_context;
 }
 
@@ -87,111 +92,18 @@ modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
        __osso_context = osso_context;
 }
 
-static void
-update_device_name_from_msg (DBusMessage *message)
+void
+modest_maemo_utils_get_device_name (void)
 {
-       DBusError error;
-       DBusMessageIter iter;
-
-       dbus_error_init (&error);
+       struct utsname name;
 
-       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;
-               }
-               dbus_message_iter_get_basic (&iter, &device_name);
+       if (uname (&name) == 0) {
                modest_conf_set_string (modest_runtime_get_conf(),
-                                       MODEST_CONF_DEVICE_NAME, device_name,
+                                       MODEST_CONF_DEVICE_NAME, name.nodename,
                                        NULL);
        }
 }
 
-
-static void
-on_device_name_received (DBusPendingCall *call, void *user_data)
-{
-       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;
-       }
-
-       update_device_name_from_msg (message);
-       dbus_message_unref (message);
-}
-
-
-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);
-
-       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
-
-
-static void
-get_device_name_from_dbus ()
-{
-       static DBusConnection *conn = NULL;
-       DBusMessage *request;
-       DBusError error;
-       DBusPendingCall *call = NULL;
-       
-       dbus_error_init (&error);
-       if (!conn) {
-               conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
-               if (!conn) {
-                       g_printerr ("modest: cannot get on the dbus: %s: %s\n",
-                                   error.name, error.message);
-                       dbus_error_free (&error);
-                       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");
-}
-
-
-void
-modest_maemo_utils_get_device_name (void)
-{
-       get_device_name_from_dbus ();
-}
-
 void
 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
 {
@@ -203,8 +115,7 @@ modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
        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,
+       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);
@@ -239,30 +150,38 @@ modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win,
 }
 
 FILE*
-modest_maemo_open_mcc_mapping_file (void)
+modest_maemo_open_mcc_mapping_file (gboolean *translated)
 {
-       FILE* result;
-       
+       FILE* result = NULL;
        const gchar* path;
-       const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
+       gchar *path1 = g_strdup_printf ("%s.%s", MODEST_OPERATOR_WIZARD_MCC_MAPPING, getenv("LANG"));
        const gchar* path2 = MODEST_MCC_MAPPING;
-       
-       if (access(path1, R_OK) == 0) 
+
+       if (translated)
+               *translated = TRUE;
+
+       if (access (path1, R_OK) == 0) {
                path = path1;
-       else if (access(path2, R_OK) == 0)
+       } else if (access (MODEST_OPERATOR_WIZARD_MCC_MAPPING, R_OK) == 0) {
+               path = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
+               if (translated)
+                       *translated = FALSE;
+       } else if (access (path2, R_OK) == 0) {
                path = path2;
-       else {
+       } else {
                g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
                           __FUNCTION__, path1, path2);
-               return NULL;
+               goto end;
        }
-       
+
        result = fopen (path, "r");
        if (!result) {
                g_warning ("%s: error opening mapping file '%s': %s",
                           __FUNCTION__, path, strerror(errno));
-               return NULL;
+               goto end;
        }
+ end:
+       g_free (path1);
        return result;
 }
 
@@ -290,39 +209,155 @@ modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
        return new_menu;
 }
 
+/**
+ * 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 *group,
-                                       const gchar *label_text,
+modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
+                                       GtkSizeGroup *value_size_group,
+                                       const gchar *title,
+                                       gboolean use_markup,
                                        GtkWidget *control)
 {
+       return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
+                                                                  value_size_group,
+                                                                  title,
+                                                                  use_markup,
+                                                                  control,
+                                                                  HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+}
+
+/**
+ * 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,
+                                                      gboolean use_markup,
+                                                      GtkWidget *control,
+                                                      HildonSizeType size_type)
+{
        GtkWidget *label;
        GtkWidget *box;
   
-       label = gtk_label_new (label_text);
+       if (use_markup) {
+               label = gtk_label_new (NULL);
+               gtk_label_set_markup (GTK_LABEL (label), title);
+       } else {
+               label = gtk_label_new (title);
+       }
+
+       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+       hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
        gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
        gtk_widget_show (label);
-       box = gtk_hbox_new (FALSE, MODEST_MARGIN_NONE);
+       box = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
        gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
        gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, MODEST_MARGIN_HALF);
-       if (group)
-               gtk_size_group_add_widget (group, label);
+       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);
 
-       hildon_gtk_widget_set_theme_size (control, HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+       g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
 
        return box;
 }
 
+/**
+ * modest_maemo_utils_captioned_set_label:
+ * @captioned: a #GtkWidget built as captioned
+ * @new_label: a string
+ * @use_markup: a #gboolean
+ *
+ * set a new label for the captioned
+ */
 void
-modest_maemo_utils_create_picker_layout (GtkSizeGroup *sizegroup, 
-                                        const gchar *label, 
-                                        GtkWidget *picker)
+modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
+                                       const gchar *new_label,
+                                       gboolean use_markup)
 {
-       hildon_button_set_title (HILDON_BUTTON (picker), label);
-       if (sizegroup)
-               hildon_button_add_title_size_group (HILDON_BUTTON (picker), sizegroup);
-       hildon_button_set_alignment (HILDON_BUTTON (picker), 0.0, 0.5, 1.0, 0.0);
-       hildon_button_set_title_alignment (HILDON_BUTTON (picker), 0.0, 0.5);
-       hildon_button_set_value_alignment (HILDON_BUTTON (picker), 0.0, 0.5);
+       GtkWidget *label;
+
+       g_return_if_fail (GTK_IS_WIDGET (captioned));
+
+       label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
+       g_return_if_fail (GTK_IS_LABEL (label));
+
+       if (use_markup) {
+               gtk_label_set_markup (GTK_LABEL (label), new_label);
+       } else {
+               gtk_label_set_text (GTK_LABEL (label), new_label);
+       }
+}
+
+/**
+ * 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)
+{
+       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_value_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);
+}
+
+void
+modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
+                                      const gchar *title, 
+                                      GtkWidget *button)
+{
+       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);
+       }
+       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);
 }
 
 GtkWidget *
@@ -341,3 +376,85 @@ modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *content
 
        return box;
 }
+
+static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
+{
+       return TRUE;
+}
+
+gboolean
+modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list)
+{
+       GtkTreeModel *model;
+       TnyIterator *iterator;
+       GtkWidget *selector;
+       GtkCellRenderer *renderer;
+       GtkWidget *dialog;
+       gint response;
+       gboolean result = TRUE;
+
+       model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
+       for (iterator = tny_list_create_iterator (att_list);
+            !tny_iterator_is_done (iterator);
+            tny_iterator_next (iterator)) {
+               GtkTreeIter iter;
+               TnyMimePart *part;
+
+               part = (TnyMimePart *) tny_iterator_get_current (iterator);
+
+               /* Embbeded messages are not offered to be
+                  saved. Purged attachments are ignored as well */
+               if (!TNY_IS_MSG (part) && !tny_mime_part_is_purged (part)) {
+                       gchar *label;
+                       gchar *filename = NULL;
+
+                       filename = g_strdup (tny_mime_part_get_filename (part));
+                       label = g_strconcat (filename, NULL);
+                       gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+                       gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, label, 1, part, -1);
+                       g_free (label);
+                       g_object_unref (part);
+               }
+       }
+
+       selector = GTK_WIDGET (hildon_touch_selector_new ());
+       renderer = gtk_cell_renderer_text_new ();
+       hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, renderer,
+                                            "text", 0, NULL);
+       hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector), 
+                                                        HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
+
+       dialog = hildon_picker_dialog_new (window);
+       gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ti_select_attachment_title"));
+       hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
+       hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
+
+       response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+       if (response == GTK_RESPONSE_OK) {
+               GList *selected_rows, *node;
+
+               tny_list_remove_matches (att_list, match_all, NULL);
+               selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
+               for (node = selected_rows; node != NULL; node = g_list_next (node)) {
+                       GtkTreePath *path;
+                       GObject *selected;
+                       GtkTreeIter iter;
+
+                       path = (GtkTreePath *) node->data;
+                       gtk_tree_model_get_iter (model, &iter, path);
+                       gtk_tree_model_get (model, &iter, 1, &selected, -1);
+                       tny_list_append (att_list, selected);
+               }
+               if (tny_list_get_length (att_list) == 0)
+                       result = FALSE;
+       } else {
+               result = FALSE;
+       }
+
+       gtk_widget_destroy (dialog);
+
+       g_object_unref (model);
+
+       return result;
+}