[gtranslator/dl-comment] Send async in upload to DL




commit 182ae9df5996265224097f758ec40d744c91dd25
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Fri Jul 30 01:13:58 2021 +0200

    Send async in upload to DL
    
    See https://gitlab.gnome.org/GNOME/gtranslator/-/issues/135

 src/gtr-actions-file.c  | 117 +++++++++++++++++++++++++-----------------------
 src/gtr-upload-dialog.c |  11 +++++
 src/gtr-upload-dialog.h |   1 +
 3 files changed, 74 insertions(+), 55 deletions(-)
---
diff --git a/src/gtr-actions-file.c b/src/gtr-actions-file.c
index 2324016f..9abc5519 100644
--- a/src/gtr-actions-file.c
+++ b/src/gtr-actions-file.c
@@ -51,6 +51,7 @@
 static void load_file_list (GtrWindow * window, const GSList * uris);
 static GList * get_modified_documents (GtrWindow * window);
 
+
 /*
  * The main file opening function. Checks that the file isn't already open,
  * and if not, opens it in a new tab.
@@ -343,6 +344,66 @@ confirm_overwrite_callback (GtkFileChooser * dialog, gpointer data)
   return res;
 }
 
+static void
+_upload_file_callback (SoupSession *session,
+                       SoupMessage *msg,
+                       gpointer     user_data)
+{
+  GtkWidget *dialog, *success_dialog;
+  GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL;
+  GtrNotebook *active_notebook;
+
+  GtkWidget *upload_dialog = user_data;
+  GtkWidget *window = gtr_upload_dialog_get_parent (GTR_UPLOAD_DIALOG (upload_dialog));
+
+  active_notebook = gtr_window_get_notebook (GTR_WINDOW (window));
+
+  if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
+    {
+      if (msg->status_code == 403)
+        {
+          dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+                                           flags,
+                                           GTK_MESSAGE_INFO,
+                                           GTK_BUTTONS_OK,
+                                           _("This file has already been uploaded"));
+          gtk_dialog_run (GTK_DIALOG (dialog));
+          gtk_widget_destroy (dialog);
+          gtr_notebook_enable_upload (active_notebook, FALSE);
+          gtk_widget_destroy (upload_dialog);
+          return;
+        }
+
+      dialog = gtk_message_dialog_new_with_markup (
+        GTK_WINDOW (window),
+        flags,
+        GTK_MESSAGE_WARNING,
+        GTK_BUTTONS_CLOSE,
+        _(
+          "An error occurred while uploading the file: %s\n"
+          "Maybe you've not configured your <i>l10n.gnome.org</i> "
+          "<b>token</b> correctly in your profile or you don't have "
+          "permissions to upload this module."
+        ),
+        soup_status_get_phrase (msg->status_code));
+      gtk_dialog_run (GTK_DIALOG (dialog));
+      gtk_widget_destroy (dialog);
+      gtk_widget_destroy (upload_dialog);
+      return;
+    }
+
+  gtk_widget_destroy (upload_dialog);
+  success_dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+                                           flags,
+                                           GTK_MESSAGE_INFO,
+                                           GTK_BUTTONS_OK,
+                                           _("The file has been uploaded!"));
+
+  gtk_dialog_run (GTK_DIALOG (success_dialog));
+  gtk_widget_destroy (success_dialog);
+  gtr_notebook_enable_upload (active_notebook, FALSE);
+}
+
 void
 gtr_upload_file (GtkWidget *upload_dialog,
                  int        response_id,
@@ -350,12 +411,9 @@ gtr_upload_file (GtkWidget *upload_dialog,
 {
   GtrTab *tab;
   GtrPo *po;
-  GtkWidget *dialog, *success_dialog;
-  GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL;
   GMappedFile *mapped;
   GError *error = NULL;
   GtrProfileManager *pmanager = NULL;
-  GtrNotebook *active_notebook;
   GtrProfile *profile;
   GtrHeader *header;
   g_autoptr (SoupMessage) msg = NULL;
@@ -395,7 +453,6 @@ gtr_upload_file (GtkWidget *upload_dialog,
   }
   content = g_mapped_file_get_contents (mapped);
   size = g_mapped_file_get_length (mapped);
-  active_notebook = gtr_window_get_notebook (window);
   header = gtr_po_get_header (po);
 
   /* Check mimetype */
@@ -448,58 +505,8 @@ gtr_upload_file (GtkWidget *upload_dialog,
 
   session = soup_session_new ();
   gtr_upload_dialog_set_loading (GTR_UPLOAD_DIALOG (upload_dialog), TRUE);
-  // TODO: Send async
-  soup_session_send_message (session, msg);
-
-  if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
-    {
-      if (msg->status_code == 403)
-        {
-          dialog = gtk_message_dialog_new (GTK_WINDOW (window),
-                                           flags,
-                                           GTK_MESSAGE_INFO,
-                                           GTK_BUTTONS_OK,
-                                           _("This file has already been uploaded"));
-          gtk_dialog_run (GTK_DIALOG (dialog));
-          gtk_widget_destroy (dialog);
-          gtr_notebook_enable_upload (active_notebook, FALSE);
-          gtk_widget_destroy (upload_dialog);
-          return;
-        }
 
-      dialog = gtk_message_dialog_new_with_markup (
-        GTK_WINDOW (window),
-        flags,
-        GTK_MESSAGE_WARNING,
-        GTK_BUTTONS_CLOSE,
-        _(
-          "An error occurred while uploading the file: %s\n"
-          "Maybe you've not configured your <i>l10n.gnome.org</i> "
-          "<b>token</b> correctly in your profile or you don't have "
-          "permissions to upload this module."
-        ),
-        soup_status_get_phrase (msg->status_code));
-      gtk_dialog_run (GTK_DIALOG (dialog));
-      gtk_widget_destroy (dialog);
-      gtk_widget_destroy (upload_dialog);
-      return;
-    }
-
-  gtk_widget_destroy (upload_dialog);
-
-  success_dialog = gtk_message_dialog_new (GTK_WINDOW (window),
-                                           flags,
-                                           GTK_MESSAGE_INFO,
-                                           GTK_BUTTONS_OK,
-                                           _("The file '%s.%s.%s.%s' has been uploaded!"),
-                                           selected_module,
-                                           selected_branch,
-                                           selected_team,
-                                           selected_domain);
-
-  gtk_dialog_run (GTK_DIALOG (success_dialog));
-  gtk_widget_destroy (success_dialog);
-  gtr_notebook_enable_upload (active_notebook, FALSE);
+  soup_session_queue_message (session, msg, _upload_file_callback, upload_dialog);
 }
 
 /*
diff --git a/src/gtr-upload-dialog.c b/src/gtr-upload-dialog.c
index d9e4c297..c9a5c237 100644
--- a/src/gtr-upload-dialog.c
+++ b/src/gtr-upload-dialog.c
@@ -35,6 +35,7 @@ typedef struct
   GtkWidget *text_view;
   GtkWidget *label;
   GtkWidget *spinner;
+  GtkWidget *parent;
 } GtrUploadDialogPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtrUploadDialog, gtr_upload_dialog, GTK_TYPE_DIALOG)
@@ -91,8 +92,11 @@ GtrUploadDialog *
 gtr_upload_dialog_new (GtkWidget  *parent)
 {
   GtrUploadDialog *dlg;
+  GtrUploadDialogPrivate *priv = NULL;
 
   dlg = g_object_new (GTR_TYPE_UPLOAD_DIALOG, NULL);
+  priv = gtr_upload_dialog_get_instance_private (dlg);
+  priv->parent = parent;
 
   if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dlg)))
     {
@@ -136,3 +140,10 @@ gtr_upload_dialog_set_loading (GtrUploadDialog *dlg,
       gtk_spinner_stop (GTK_SPINNER (priv->spinner));
     }
 }
+
+GtkWidget *
+gtr_upload_dialog_get_parent (GtrUploadDialog *dlg)
+{
+  GtrUploadDialogPrivate *priv = gtr_upload_dialog_get_instance_private (dlg);
+  return priv->parent;
+}
diff --git a/src/gtr-upload-dialog.h b/src/gtr-upload-dialog.h
index 889297c9..d46f2e4f 100644
--- a/src/gtr-upload-dialog.h
+++ b/src/gtr-upload-dialog.h
@@ -67,6 +67,7 @@ GtrUploadDialog   *gtr_upload_dialog_new              (GtkWidget *parent);
 char              *gtr_upload_dialog_get_comment      (GtrUploadDialog *dlg);
 void               gtr_upload_dialog_set_loading      (GtrUploadDialog *dlg,
                                                        gboolean loading);
+GtkWidget         *gtr_upload_dialog_get_parent       (GtrUploadDialog *dlg);
 
 G_END_DECLS
 


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