Changes to disable cut/copy with images (fixes NB#80355).
authorJose Dapena Paz <jdapena@igalia.com>
Mon, 11 Feb 2008 07:27:52 +0000 (07:27 +0000)
committerJose Dapena Paz <jdapena@igalia.com>
Mon, 11 Feb 2008 07:27:52 +0000 (07:27 +0000)
* src/modest-text-utils.[ch], src/modest-ui-dimming-rules.c:
        * Refactored a method to check if a GtkTextBuffer has a valid
          cut/copy selection (currently it means it's not empty and does
          not contain images inside).
* src/modest-ui-actions.c:
        * Don't cut/copy if the selection is not valid.
* src/maemo/modest-msg-edit-window.c:
        * Disable default cut/copy handlers if no selection is valid.

pmo-trunk-r4155

src/maemo/modest-msg-edit-window.c
src/modest-text-utils.c
src/modest-text-utils.h
src/modest-ui-actions.c
src/modest-ui-dimming-rules.c

index 14303eb..639ecb4 100644 (file)
@@ -641,6 +641,30 @@ text_buffer_mark_set (GtkTextBuffer *buffer,
        gtk_text_buffer_end_user_action (buffer);
 }
 
+static void
+cut_clipboard_check (GtkTextView *text_view,
+                    gpointer userdata)
+{
+       GtkTextBuffer *buffer;
+       
+       buffer = gtk_text_view_get_buffer (text_view);
+       if (!modest_text_utils_buffer_selection_is_valid (buffer)) {
+               g_signal_stop_emission_by_name ((gpointer )text_view, "cut-clipboard");
+       }
+}
+
+static void
+copy_clipboard_check (GtkTextView *text_view,
+                    gpointer userdata)
+{
+       GtkTextBuffer *buffer;
+       
+       buffer = gtk_text_view_get_buffer (text_view);
+       if (!modest_text_utils_buffer_selection_is_valid (buffer)) {
+               g_signal_stop_emission_by_name ((gpointer )text_view, "copy-clipboard");
+       }
+}
+
 void vadj_changed (GtkAdjustment *adj,
                   ModestMsgEditWindow *window)
 {
@@ -720,6 +744,9 @@ connect_signals (ModestMsgEditWindow *obj)
                g_signal_connect (G_OBJECT (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD)), "owner-change",
                                  G_CALLBACK (modest_msg_edit_window_clipboard_owner_change), obj);
 
+       g_signal_connect (G_OBJECT (priv->msg_body), "cut-clipboard", G_CALLBACK (cut_clipboard_check), NULL);
+       g_signal_connect (G_OBJECT (priv->msg_body), "copy-clipboard", G_CALLBACK (copy_clipboard_check), NULL);
+
 }
 
 static void
index 561ca9f..6bbd800 100644 (file)
@@ -1651,3 +1651,37 @@ modest_text_utils_label_get_selection (GtkLabel *label)
                return g_strdup ("");
        }
 }
+
+static gboolean
+_forward_search_image_char (gunichar ch,
+                           gpointer userdata)
+{
+       return (ch == 0xFFFC);
+}
+
+gboolean
+modest_text_utils_buffer_selection_is_valid (GtkTextBuffer *buffer)
+{
+       gboolean result;
+       GtkTextIter start, end;
+
+       g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
+
+       result = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (buffer));
+
+       /* check there are no images in selection */
+       if (result) {
+               gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
+               if (gtk_text_iter_get_char (&start)== 0xFFFC)
+                       result = FALSE;
+               else {
+                       gtk_text_iter_backward_char (&end);
+                       if (gtk_text_iter_forward_find_char (&start, _forward_search_image_char,
+                                                            NULL, &end))
+                               result = FALSE;
+               }
+                                   
+       }
+
+       return result;
+}
index 3b27aa9..3a9c975 100644 (file)
@@ -435,5 +435,16 @@ gchar *      modest_text_utils_label_get_selection (GtkLabel *label);
 gboolean     modest_text_utils_is_forbidden_char (const gchar character,
                                                  ModestTextUtilsForbiddenCharType type);
 
+/**
+ * modest_text_utils_buffer_selection_is_valid:
+ * @buffer: a #GtkTextBuffer
+ *
+ * Checks if @buffer contains a valid selection for cut/copy. This means it's
+ * not empty, and no images are in the selection.
+ *
+ * Returns: %TRUE if there's a valid selection, false otherwise.
+ */
+gboolean     modest_text_utils_buffer_selection_is_valid (GtkTextBuffer *buffer);
+
 
 #endif /* __MODEST_TEXT_UTILS_H__ */
index cc8f566..9024860 100644 (file)
@@ -3271,9 +3271,11 @@ modest_ui_actions_on_cut (GtkAction *action,
                GtkTextBuffer *buffer;
 
                buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
-               gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
-               gtk_clipboard_set_can_store (clipboard, NULL, 0);
-               gtk_clipboard_store (clipboard);
+               if (modest_text_utils_buffer_selection_is_valid (buffer)) {
+                       gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
+                       gtk_clipboard_set_can_store (clipboard, NULL, 0);
+                       gtk_clipboard_store (clipboard);
+               }
        } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
                TnyList *header_list = modest_header_view_get_selected_headers (
                                MODEST_HEADER_VIEW (focused_widget));
@@ -3330,9 +3332,11 @@ modest_ui_actions_on_copy (GtkAction *action,
        } else if (GTK_IS_TEXT_VIEW (focused_widget)) {
                GtkTextBuffer *buffer;
                buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused_widget));
-               gtk_text_buffer_copy_clipboard (buffer, clipboard);
-               gtk_clipboard_set_can_store (clipboard, NULL, 0);
-               gtk_clipboard_store (clipboard);
+               if (modest_text_utils_buffer_selection_is_valid (buffer)) {
+                       gtk_text_buffer_copy_clipboard (buffer, clipboard);
+                       gtk_clipboard_set_can_store (clipboard, NULL, 0);
+                       gtk_clipboard_store (clipboard);
+               }
        } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
                TnyList *header_list = modest_header_view_get_selected_headers (
                                MODEST_HEADER_VIEW (focused_widget));
index aaef460..7ab6ff0 100644 (file)
@@ -2287,13 +2287,6 @@ _clipboard_is_empty (ModestWindow *win)
 }
 
 static gboolean
-_forward_search_image_char (gunichar ch,
-                           gpointer userdata)
-{
-       return (ch == 0xFFFC);
-}
-
-static gboolean
 _invalid_clipboard_selected (ModestWindow *win,
                             ModestDimmingRule *rule) 
 {
@@ -2310,23 +2303,8 @@ _invalid_clipboard_selected (ModestWindow *win,
                gboolean has_selection = FALSE;
                if (GTK_IS_TEXT_VIEW (focused)) {
                        GtkTextBuffer *buffer = NULL;
-                       GtkTextIter start, end;
                        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
-                       has_selection = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (buffer));
-
-                       /* check there are no images in selection */
-                       if (has_selection) {
-                               gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
-                               if (gtk_text_iter_get_char (&start)== 0xFFFC)
-                                       has_selection = FALSE;
-                               else {
-                                       gtk_text_iter_backward_char (&end);
-                                       if (gtk_text_iter_forward_find_char (&start, _forward_search_image_char,
-                                                                         NULL, &end))
-                                               has_selection = FALSE;
-                               }
-                                   
-                       }
+                       has_selection = modest_text_utils_buffer_selection_is_valid (buffer);
                } else if (GTK_IS_EDITABLE (focused)) {
                        has_selection = gtk_editable_get_selection_bounds (GTK_EDITABLE (focused), NULL, NULL);
                }