[gtranslator/dl-comment] Send async in upload to DL
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtranslator/dl-comment] Send async in upload to DL
- Date: Sat, 31 Jul 2021 16:57:26 +0000 (UTC)
commit 66a80da7b2344b4858c3122d04f6e42196b11bbe
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 | 125 +++++++++++++++++++++++++----------------------
src/gtr-upload-dialog.c | 27 +++++++---
src/gtr-upload-dialog.h | 1 +
src/gtr-upload-dialog.ui | 14 ++----
4 files changed, 90 insertions(+), 77 deletions(-)
---
diff --git a/src/gtr-actions-file.c b/src/gtr-actions-file.c
index 2324016f..fb5d4ae5 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,62 @@ confirm_overwrite_callback (GtkFileChooser * dialog, gpointer data)
return res;
}
+static void
+_upload_file_callback (SoupSession *session,
+ SoupMessage *msg,
+ gpointer user_data)
+{
+ GtkWidget *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"));
+ gtr_notebook_enable_upload (active_notebook, FALSE);
+ goto end;
+ }
+
+ 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));
+ goto end;
+ }
+
+ dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+ flags,
+ GTK_MESSAGE_INFO,
+ GTK_BUTTONS_OK,
+ _("The file has been uploaded!"));
+
+ gtr_notebook_enable_upload (active_notebook, FALSE);
+
+end:
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+ gtk_widget_destroy (upload_dialog);
+}
+
void
gtr_upload_file (GtkWidget *upload_dialog,
int response_id,
@@ -350,18 +407,17 @@ 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;
g_autoptr (SoupMultipart) mpart = NULL;
g_autoptr (SoupBuffer) buffer = NULL;
- g_autoptr (SoupSession) session = NULL;
+
+ SoupMessage *msg = NULL;
+ static SoupSession *session = NULL;
+
const gchar *content = NULL;
g_autofree gchar *mime_type = NULL;
g_autofree gchar *filename = NULL;
@@ -395,7 +451,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 */
@@ -434,10 +489,10 @@ gtr_upload_file (GtkWidget *upload_dialog,
/* Init multipart container */
mpart = soup_multipart_new (SOUP_FORM_MIME_TYPE_MULTIPART);
buffer = soup_buffer_new (SOUP_MEMORY_COPY, content, size);
- soup_multipart_append_form_string (mpart, "file", "txt");
soup_multipart_append_form_file (mpart, "file", filename,
mime_type, buffer);
- soup_multipart_append_form_string (mpart, "comment", upload_comment);
+ if (upload_comment)
+ soup_multipart_append_form_string (mpart, "comment", upload_comment);
/* Get the associated message */
msg = soup_form_request_new_from_multipart (upload_endpoint, mpart);
@@ -446,60 +501,12 @@ gtr_upload_file (GtkWidget *upload_dialog,
/* Append the authentication header*/
soup_message_headers_append (msg->request_headers, "Authentication", auth);
- 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;
- }
+ if (!session)
+ session = soup_session_new ();
- 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..d59512b8 100644
--- a/src/gtr-upload-dialog.c
+++ b/src/gtr-upload-dialog.c
@@ -34,7 +34,8 @@ typedef struct
GtkWidget *main_box;
GtkWidget *text_view;
GtkWidget *label;
- GtkWidget *spinner;
+ GtkWidget *parent;
+ GtkWidget *upload;
} GtrUploadDialogPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (GtrUploadDialog, gtr_upload_dialog, GTK_TYPE_DIALOG)
@@ -55,8 +56,8 @@ gtr_upload_dialog_init (GtrUploadDialog *dlg)
NULL
};
- gtk_dialog_add_button (GTK_DIALOG (dlg),
- _("_Upload"), GTK_RESPONSE_ACCEPT);
+ priv->upload = gtk_dialog_add_button (GTK_DIALOG (dlg),
+ _("_Upload"), GTK_RESPONSE_ACCEPT);
gtk_window_set_title (GTK_WINDOW (dlg), _("Upload to Damned Lies"));
gtk_window_set_resizable (GTK_WINDOW (dlg), FALSE);
@@ -81,7 +82,6 @@ gtr_upload_dialog_init (GtrUploadDialog *dlg)
g_object_ref (priv->main_box);
priv->text_view = GTK_WIDGET (gtk_builder_get_object (builder, "text_view"));
priv->label = GTK_WIDGET (gtk_builder_get_object (builder, "label"));
- priv->spinner = GTK_WIDGET (gtk_builder_get_object (builder, "spinner"));
g_object_unref (builder);
gtk_box_pack_start (content_area, priv->main_box, FALSE, FALSE, 0);
@@ -91,8 +91,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)))
{
@@ -123,16 +126,24 @@ gtr_upload_dialog_set_loading (GtrUploadDialog *dlg,
gboolean loading)
{
GtrUploadDialogPrivate *priv = gtr_upload_dialog_get_instance_private (dlg);
+
if (loading)
{
- gtk_widget_show (priv->spinner);
gtk_widget_set_sensitive (priv->text_view, FALSE);
- gtk_spinner_start (GTK_SPINNER (priv->spinner));
+ gtk_widget_set_sensitive (priv->upload, FALSE);
+ gtk_button_set_label (GTK_BUTTON (priv->upload), _("Uploading..."));
}
else
{
- gtk_widget_hide (priv->spinner);
gtk_widget_set_sensitive (priv->text_view, TRUE);
- gtk_spinner_stop (GTK_SPINNER (priv->spinner));
+ gtk_widget_set_sensitive (priv->upload, TRUE);
+ gtk_button_set_label (GTK_BUTTON (priv->upload), _("Upload"));
}
}
+
+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
diff --git a/src/gtr-upload-dialog.ui b/src/gtr-upload-dialog.ui
index 0fc8576b..a9328439 100644
--- a/src/gtr-upload-dialog.ui
+++ b/src/gtr-upload-dialog.ui
@@ -17,7 +17,7 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
- <property name="spacing">2</property>
+ <property name="spacing">6</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area1">
<property name="visible">True</property>
@@ -42,13 +42,13 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
- <property name="spacing">18</property>
+ <property name="spacing">6</property>
<child>
<object class="GtkLabel" id="label">
<property name="visible">False</property>
<property name="can_focus">False</property>
- <property name="label" translatable="yes">Upload to Damned Lies. You can leave a
comment:</property>
- <property name="xalign">0.5</property>
+ <property name="label" translatable="yes">comment:</property>
+ <property name="xalign">0</property>
</object>
</child>
<child>
@@ -62,12 +62,6 @@
</style>
</object>
</child>
- <child>
- <object class="GtkSpinner" id="spinner">
- <property name="visible">False</property>
- <property name="can_focus">False</property>
- </object>
- </child>
</object>
<packing>
<property name="expand">False</property>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]