managesieve: don't reset status while appending text
[claws.git] / src / plugins / managesieve / sieve_editor.c
index 332ca9f233914841dfe310c62083e19d6ff16e04..dc89f7fb2998b23f7ec59ecd86848818ca5dc1b1 100644 (file)
@@ -54,6 +54,7 @@ gboolean sieve_editor_search_string_backward(void *obj,
        const gchar *str, gboolean case_sens);
 static void sieve_editor_save_cb(GtkAction *action, SieveEditorPage *page);
 static void sieve_editor_check_cb(GtkAction *action, SieveEditorPage *page);
+static void sieve_editor_changed_cb(GtkTextBuffer *, SieveEditorPage *page);
 static void sieve_editor_revert_cb(GtkAction *action, SieveEditorPage *page);
 static void sieve_editor_close_cb(GtkAction *action, SieveEditorPage *page);
 static void sieve_editor_undo_cb(GtkAction *action, SieveEditorPage *page);
@@ -113,14 +114,19 @@ void sieve_editor_append_text(SieveEditorPage *page, gchar *text, gint len)
 {
        GtkTextBuffer *buffer;
        GtkTextIter iter;
-       gboolean was_modified = page->modified;
 
-       undo_block(page->undostruct);
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->text));
+
+       g_signal_handlers_block_by_func(G_OBJECT(buffer),
+                        G_CALLBACK(sieve_editor_changed_cb), page);
+
+       undo_block(page->undostruct);
        gtk_text_buffer_get_end_iter(buffer, &iter);
        gtk_text_buffer_insert(buffer, &iter, text, len);
        undo_unblock(page->undostruct);
-       sieve_editor_set_modified(page, was_modified);
+
+       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
+                        G_CALLBACK(sieve_editor_changed_cb), page);
 }
 
 static gint sieve_editor_get_text(SieveEditorPage *page, gchar **text)
@@ -266,9 +272,12 @@ static void sieve_editor_search(SieveEditorPage *page)
 
 /* Actions */
 
-static void got_data_reverting(SieveSession *session, gchar *contents,
+static void got_data_reverting(SieveSession *session, gboolean abort,
+               gchar *contents,
                SieveEditorPage *page)
 {
+       if (abort)
+               return;
        if (contents == NULL) {
                /* end of data */
                undo_unblock(page->undostruct);
@@ -324,9 +333,11 @@ static void sieve_editor_revert_cb(GtkAction *action, SieveEditorPage *page)
                sieve_editor_revert(page);
 }
 
-static void got_data_saved(SieveSession *session, SieveResult *result,
-               SieveEditorPage *page)
+static void got_data_saved(SieveSession *session, gboolean abort,
+               SieveResult *result, SieveEditorPage *page)
 {
+       if (abort)
+               return;
        if (result->has_status && result->success) {
                sieve_editor_set_modified(page, FALSE);
                if (page->closing) {
@@ -362,9 +373,11 @@ static void sieve_editor_find_cb(GtkAction *action, SieveEditorPage *page)
        sieve_editor_search(page);
 }
 
-static void got_data_checked(SieveSession *session, SieveResult *result,
-               SieveEditorPage *page)
+static void got_data_checked(SieveSession *session, gboolean abort,
+               SieveResult *result, SieveEditorPage *page)
 {
+       if (abort)
+               return;
        sieve_editor_update_status(page, result);
 }
 
@@ -618,8 +631,6 @@ MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Filter", "Revert", "Filter/Revert", GT
        undo_set_change_state_func(undostruct, &sieve_editor_undo_state_changed,
                        page);
 
-       gtk_widget_show_all(window);
-
        page->window = window;
        page->ui_manager = ui_manager;
        page->text = text;
@@ -654,6 +665,11 @@ void sieve_editor_present(SieveEditorPage *page)
        gtk_window_present(GTK_WINDOW(page->window));
 }
 
+void sieve_editor_show(SieveEditorPage *page)
+{
+       gtk_widget_show_all(GTK_WIDGET(page->window));
+}
+
 static void sieve_editor_set_modified(SieveEditorPage *page,
                gboolean modified)
 {
@@ -664,7 +680,7 @@ static void sieve_editor_set_modified(SieveEditorPage *page,
                        modified);
 
        title = g_strdup_printf(_("%s - Sieve Filter%s"), page->script_name,
-                       modified ? " [Edited]") : "");
+                       modified ? _(" [Edited]") : "");
        gtk_window_set_title (GTK_WINDOW (page->window), title);
        g_free(title);
 
@@ -673,3 +689,49 @@ static void sieve_editor_set_modified(SieveEditorPage *page,
                sieve_editor_set_status_icon(page, NULL);
        }
 }
+
+static void got_data_loading(SieveSession *session, gboolean aborted,
+               gchar *contents, SieveEditorPage *page)
+{
+       if (aborted)
+               return;
+       if (contents == NULL) {
+               /* end of data */
+               sieve_editor_set_status(page, "");
+               return;
+       }
+       if (contents == (void *)-1) {
+               /* error */
+               if (page->first_line) {
+                       /* no data. show error in manager window */
+                       if (page->on_load_error)
+                               page->on_load_error(session, page->on_load_error_data);
+               } else {
+                       /* partial failure. show error in editor window */
+                       sieve_editor_set_status(page, _("Unable to get script contents"));
+                       sieve_editor_set_status_icon(page, GTK_STOCK_DIALOG_ERROR);
+               }
+               return;
+       }
+
+       if (page->first_line) {
+               page->first_line = FALSE;
+               sieve_editor_show(page);
+       } else {
+               sieve_editor_append_text(page, "\n", 1);
+       }
+       sieve_editor_append_text(page, contents, strlen(contents));
+}
+
+/* load the script for this editor */
+void sieve_editor_load(SieveEditorPage *page,
+               sieve_session_cb_fn on_load_error, gpointer load_error_data)
+{
+       page->first_line = TRUE;
+       page->on_load_error = on_load_error;
+       page->on_load_error_data = load_error_data;
+       sieve_editor_set_status(page, _("Loading..."));
+       sieve_editor_set_status_icon(page, NULL);
+       sieve_session_get_script(page->session, page->script_name,
+                       (sieve_session_data_cb_fn)got_data_loading, page);
+}