[evolution/webkit-composer: 68/210] Port plugins do EEditor.



commit 85ac2e51bc20e2885500e4f559676af29259b364
Author: Dan Vrátil <dvratil redhat com>
Date:   Tue Aug 28 18:15:35 2012 +0200

    Port plugins do EEditor.

 plugins/email-custom-header/email-custom-header.c |   10 +-
 plugins/external-editor/external-editor.c         |  149 +++++++++++++--------
 plugins/face/face.c                               |   12 +-
 plugins/templates/templates.c                     |    6 +-
 4 files changed, 106 insertions(+), 71 deletions(-)
---
diff --git a/plugins/email-custom-header/email-custom-header.c 
b/plugins/email-custom-header/email-custom-header.c
index 78364fa..8c63002 100644
--- a/plugins/email-custom-header/email-custom-header.c
+++ b/plugins/email-custom-header/email-custom-header.c
@@ -500,8 +500,10 @@ action_email_custom_header_cb (GtkAction *action,
        GdkWindow *window;
        CustomHeaderOptionsDialog *dialog = NULL;
        EmailCustomHeaderWindow *new_email_custom_header_window = NULL;
+       EEditor *editor;
 
-       ui_manager = gtkhtml_editor_get_ui_manager (GTKHTML_EDITOR (composer));
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (composer));
+       ui_manager = e_editor_get_ui_manager (editor);
        menuitem = gtk_ui_manager_get_widget (ui_manager, "/main-menu/insert-menu/insert-menu-top/Custom 
Header");
 
        new_email_custom_header_window = g_object_get_data ((GObject *) composer, "compowindow");
@@ -543,13 +545,13 @@ gboolean
 e_plugin_ui_init (GtkUIManager *ui_manager,
                   EMsgComposer *composer)
 {
-       GtkhtmlEditor *editor;
+       EEditor *editor;
 
-       editor = GTKHTML_EDITOR (composer);
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (composer));
 
        /* Add actions to the "composer" action group. */
        gtk_action_group_add_actions (
-               gtkhtml_editor_get_action_group (editor, "composer"),
+               e_editor_get_action_group (editor, "composer"),
                entries, G_N_ELEMENTS (entries), composer);
 
        return TRUE;
diff --git a/plugins/external-editor/external-editor.c b/plugins/external-editor/external-editor.c
index d31b0d8..ef096a3 100644
--- a/plugins/external-editor/external-editor.c
+++ b/plugins/external-editor/external-editor.c
@@ -52,6 +52,7 @@ static void   ee_editor_command_changed
                                                (GtkWidget *textbox);
 static void    ee_editor_immediate_launch_changed
                                                (GtkWidget *checkbox);
+static void    async_external_editor           (EMsgComposer *composer);
 static gboolean        editor_running                  (void);
 static gboolean        key_press_cb                    (GtkWidget *widget,
                                                 GdkEventKey *event,
@@ -148,29 +149,30 @@ static void
 enable_disable_composer (EMsgComposer *composer,
                          gboolean enable)
 {
-       GtkhtmlEditor *editor;
+       EEditor *editor;
        GtkAction *action;
        GtkActionGroup *action_group;
 
        g_return_if_fail (E_IS_MSG_COMPOSER (composer));
 
-       editor = GTKHTML_EDITOR (composer);
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (composer));
 
-       if (enable)
-               gtkhtml_editor_run_command (editor, "editable-on");
-       else
-               gtkhtml_editor_run_command (editor, "editable-off");
+       if (enable) {
+               webkit_web_view_set_editable (WEBKIT_WEB_VIEW (editor), TRUE);
+       } else {
+               webkit_web_view_set_editable (WEBKIT_WEB_VIEW (editor), FALSE);
+       }
 
-       action = GTKHTML_EDITOR_ACTION_EDIT_MENU (composer);
+       action = E_EDITOR_ACTION_EDIT_MENU (editor);
        gtk_action_set_sensitive (action, enable);
 
-       action = GTKHTML_EDITOR_ACTION_FORMAT_MENU (composer);
+       action = E_EDITOR_ACTION_FORMAT_MENU (editor);
        gtk_action_set_sensitive (action, enable);
 
-       action = GTKHTML_EDITOR_ACTION_INSERT_MENU (composer);
+       action = E_EDITOR_ACTION_INSERT_MENU (editor);
        gtk_action_set_sensitive (action, enable);
 
-       action_group = gtkhtml_editor_get_action_group (editor, "composer");
+       action_group = e_editor_get_action_group (editor, "composer");
        gtk_action_group_set_sensitive (action_group, enable);
 }
 
@@ -247,30 +249,72 @@ numlines (const gchar *text,
        return lineno;
 }
 
-static gboolean external_editor_running = FALSE;
-static GMutex external_editor_running_lock;
+static gint
+get_caret_position (EEditorWidget *widget)
+{
+       WebKitDOMDocument *document;
+       WebKitDOMDOMWindow *window;
+       WebKitDOMDOMSelection *selection;
+       WebKitDOMRange *range;
+       gint range_count;
+       WebKitDOMNodeList *nodes;
+       gulong ii, length;
+
+       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (widget));
+       window = webkit_dom_document_get_default_view (document);
+       selection = webkit_dom_dom_window_get_selection (window);
+       if (webkit_dom_dom_selection_get_range_count (selection) < 1) {
+               return 0;
+       }
 
-static gpointer
-external_editor_thread (gpointer user_data)
+       range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL);
+       range_count = 0;
+       nodes = webkit_dom_node_get_child_nodes (
+                       webkit_dom_node_get_parent_node (
+                               webkit_dom_dom_selection_get_anchor_node (
+                                       selection)));
+       length = webkit_dom_node_list_get_length (nodes);
+       for (ii = 0; ii < length; ii++) {
+               WebKitDOMNode *node;
+
+               node = webkit_dom_node_list_item (nodes, ii);
+               if (webkit_dom_node_is_same_node (
+                       node, webkit_dom_dom_selection_get_anchor_node (selection))) {
+
+                       break;
+               } else if (webkit_dom_node_get_node_type (node) == 3) {
+                       gchar *text = webkit_dom_node_get_text_content (node);
+                       range_count += strlen (text);
+                       g_free (text);
+               }
+       }
+
+       return webkit_dom_range_get_start_offset (range, NULL) + range_count;
+}
+
+void
+async_external_editor (EMsgComposer *composer)
 {
-       EMsgComposer *composer = user_data;
        gchar *filename = NULL;
        gint status = 0;
        GSettings *settings;
        gchar *editor_cmd_line = NULL, *editor_cmd = NULL, *content;
        gint fd, position = -1, offset = -1;
+       EEditor *editor;
+       EEditorWidget *editor_widget;
+
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (composer));
+       editor_widget = e_editor_get_editor_widget (editor);
 
        /* prefix temp files with evo so .*vimrc can be setup to recognize them */
        fd = g_file_open_tmp ("evoXXXXXX", &filename, NULL);
        if (fd > 0) {
-               gsize length = 0;
-
                close (fd);
                d (printf ("\n\aTemporary-file Name is : [%s] \n\a", filename));
 
                /* Push the text (if there is one) from the composer to the file */
-               content = gtkhtml_editor_get_text_plain (GTKHTML_EDITOR (composer), &length);
-               g_file_set_contents (filename, content, length, NULL);
+               content = e_editor_widget_get_text_plain (editor_widget);
+               g_file_set_contents (filename, content, strlen (content), NULL);
        } else {
                struct run_error_dialog_data *data;
 
@@ -282,14 +326,13 @@ external_editor_thread (gpointer user_data)
 
                /* run_error_dialog also calls enable_composer */
                g_idle_add ((GSourceFunc) run_error_dialog, data);
-
-               goto finished;
+               return;
        }
 
        settings = g_settings_new ("org.gnome.evolution.plugin.external-editor");
        editor_cmd = g_settings_get_string (settings, "command");
        if (!editor_cmd) {
-               if (!(editor_cmd = g_strdup (g_getenv ("EDITOR"))))
+               if (!(editor_cmd = g_strdup (g_getenv ("EDITOR"))) )
                        /* Make gedit the default external editor,
                         * if the default schemas are not installed
                         * and no $EDITOR is set. */
@@ -297,11 +340,8 @@ external_editor_thread (gpointer user_data)
        }
        g_object_unref (settings);
 
-       if (g_strrstr (editor_cmd, "vim") != NULL
-           && gtk_html_get_cursor_pos (
-                       gtkhtml_editor_get_html (
-                       GTKHTML_EDITOR (composer)), &position, &offset)
-                               && position >= 0 && offset >= 0) {
+       if (g_strrstr (editor_cmd, "vim") != NULL &&
+           ((position = get_caret_position (editor_widget)) > 0)) {
                gchar *tmp = editor_cmd;
                gint lineno;
                gboolean set_nofork;
@@ -342,7 +382,7 @@ external_editor_thread (gpointer user_data)
                g_free (filename);
                g_free (editor_cmd_line);
                g_free (editor_cmd);
-               goto finished;
+               return;
        }
        g_free (editor_cmd_line);
        g_free (editor_cmd);
@@ -354,7 +394,7 @@ external_editor_thread (gpointer user_data)
 #endif
                d (printf ("\n\nsome problem here with external editor\n\n"));
                g_idle_add ((GSourceFunc) enable_composer, composer);
-               goto finished;
+               return;
        } else {
                gchar *buf;
 
@@ -365,9 +405,8 @@ external_editor_thread (gpointer user_data)
                        htmltext = camel_text_to_html (
                                buf, CAMEL_MIME_FILTER_TOHTML_PRE, 0);
 
-                       array = g_array_sized_new (
-                               TRUE, TRUE,
-                               sizeof (gpointer), 2 * sizeof (gpointer));
+                       array = g_array_sized_new (TRUE, TRUE,
+                                                  sizeof (gpointer), 2 * sizeof (gpointer));
                        array = g_array_append_val (array, composer);
                        array = g_array_append_val (array, htmltext);
 
@@ -378,13 +417,6 @@ external_editor_thread (gpointer user_data)
                        g_free (filename);
                }
        }
-
- finished:
-       g_mutex_lock (&external_editor_running_lock);
-       external_editor_running = FALSE;
-       g_mutex_unlock (&external_editor_running_lock);
-
-       return NULL;
 }
 
 static void launch_editor (GtkAction *action, EMsgComposer *composer)
@@ -398,13 +430,8 @@ static void launch_editor (GtkAction *action, EMsgComposer *composer)
 
        disable_composer (composer);
 
-       g_mutex_lock (&external_editor_running_lock);
-       external_editor_running = TRUE;
-       g_mutex_unlock (&external_editor_running_lock);
-
-       editor_thread = g_thread_new (
-               NULL, external_editor_thread, composer);
-       g_thread_unref (editor_thread);
+       editor_thread = g_thread_create (
+               (GThreadFunc) async_external_editor, composer, FALSE, NULL);
 }
 
 static GtkActionEntry entries[] = {
@@ -448,14 +475,21 @@ key_press_cb (GtkWidget *widget,
        return TRUE;
 }
 
+static void
+editor_running_thread_func (GThread *thread,
+                            gpointer running)
+{
+       if (thread == editor_thread)
+               *(gboolean*)running = TRUE;
+}
+
+/* Racy? */
 static gboolean
 editor_running (void)
 {
-       gboolean running;
+       gboolean running = FALSE;
 
-       g_mutex_lock (&external_editor_running_lock);
-       running = external_editor_running;
-       g_mutex_unlock (&external_editor_running_lock);
+       g_thread_foreach ((GFunc) editor_running_thread_func, &running);
 
        return running;
 }
@@ -478,24 +512,23 @@ gboolean
 e_plugin_ui_init (GtkUIManager *manager,
                   EMsgComposer *composer)
 {
-       GtkhtmlEditor *editor;
-       EWebViewGtkHTML *web_view;
+       EEditor *editor;
+       EEditorWidget *editor_widget;
 
-       editor = GTKHTML_EDITOR (composer);
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (composer));
+       editor_widget = e_editor_get_editor_widget (editor);
 
        /* Add actions to the "composer" action group. */
        gtk_action_group_add_actions (
-               gtkhtml_editor_get_action_group (editor, "composer"),
+               e_editor_get_action_group (editor, "composer"),
                entries, G_N_ELEMENTS (entries), composer);
 
-       web_view = e_msg_composer_get_web_view (composer);
-
        g_signal_connect (
-               web_view, "key_press_event",
+               editor_widget, "key_press_event",
                G_CALLBACK (key_press_cb), composer);
 
        g_signal_connect (
-               web_view, "delete-event",
+               editor_widget, "delete-event",
                G_CALLBACK (delete_cb), composer);
 
        return TRUE;
diff --git a/plugins/face/face.c b/plugins/face/face.c
index 06a6bc6..dd352f7 100644
--- a/plugins/face/face.c
+++ b/plugins/face/face.c
@@ -424,7 +424,7 @@ gboolean
 e_plugin_ui_init (GtkUIManager *ui_manager,
                   EMsgComposer *composer)
 {
-       GtkhtmlEditor *editor;
+       EEditor *editor;
 
        static GtkToggleActionEntry entries[] = {
                { "face-plugin",
@@ -445,11 +445,11 @@ e_plugin_ui_init (GtkUIManager *ui_manager,
                g_free (face);
        }
 
-       editor = GTKHTML_EDITOR (composer);
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (composer));
 
        /* Add actions to the "composer" action group. */
        gtk_action_group_add_toggle_actions (
-               gtkhtml_editor_get_action_group (editor, "composer"),
+               e_editor_get_action_group (editor, "composer"),
                entries, G_N_ELEMENTS (entries), composer);
 
        return TRUE;
@@ -465,11 +465,11 @@ void
 face_handle_send (EPlugin *ep,
                   EMEventTargetComposer *target)
 {
-       GtkhtmlEditor *editor;
+       EEditor *editor;
        GtkAction *action;
 
-       editor = GTKHTML_EDITOR (target->composer);
-       action = gtkhtml_editor_get_action (editor, "face-plugin");
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (target->composer));
+       action = e_editor_get_action (editor, "face-plugin");
 
        g_return_if_fail (action != NULL);
 
diff --git a/plugins/templates/templates.c b/plugins/templates/templates.c
index 95d0060..8a75cff 100644
--- a/plugins/templates/templates.c
+++ b/plugins/templates/templates.c
@@ -1334,13 +1334,13 @@ gboolean
 init_composer_actions (GtkUIManager *ui_manager,
                        EMsgComposer *composer)
 {
-       GtkhtmlEditor *editor;
+       EEditor *editor;
 
-       editor = GTKHTML_EDITOR (composer);
+       editor = e_editor_window_get_editor (E_EDITOR_WINDOW (composer));
 
        /* Add actions to the "composer" action group. */
        gtk_action_group_add_actions (
-               gtkhtml_editor_get_action_group (editor, "composer"),
+               e_editor_get_action_group (editor, "composer"),
                composer_entries, G_N_ELEMENTS (composer_entries), composer);
 
        return TRUE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]