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




commit ba9387b473ebdce627f32a90ea7d6264d4f70212
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 | 121 +++++++++++++++++++++++++++----------------------
 1 file changed, 67 insertions(+), 54 deletions(-)
---
diff --git a/src/gtr-actions-file.c b/src/gtr-actions-file.c
index 10b8657c..b93b3375 100644
--- a/src/gtr-actions-file.c
+++ b/src/gtr-actions-file.c
@@ -343,6 +343,68 @@ confirm_overwrite_callback (GtkFileChooser * dialog, gpointer data)
   return res;
 }
 
+static void
+_upload_file_callback (SoupSession *session,
+                       SoupMessage *msg,
+                       gpointer     user_data)
+{
+  g_autoptr(GInputStream) stream = NULL;
+  GtkWidget *dialog, *success_dialog;
+  GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL;
+  GtrNotebook *active_notebook;
+  GError *error = NULL;
+
+  GtrWindow *window = user_data[0];
+  GtrUploadDialog *upload_dialog = GTR_UPLOAD_DIALOG (user_data[1]);
+
+  active_notebook = gtr_window_get_notebook (window);
+
+  if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
+    {
+      if (msg->status_code == 403)
+        {
+          dialog = gtk_message_dialog_new (GTK_WIDGET (upload_dialog),
+                                           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_WIDGET (upload_dialog),
+        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_WIDGET (upload_dialog),
+                                           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 +412,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 +454,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 +506,13 @@ 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);
+  GtkWidget *user_data[] = {
+    window,
+    upload_dialog,
+  };
 
-  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, user_data);
 }
 
 /*


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