[gtranslator/dl-comment: 1/2] Allow adding a comment when uploading to DL




commit 00f1613b75fe224b88823409f0fb5a4789434ac8
Author: Daniel García Moreno <dani danigm net>
Date:   Fri Jul 30 00:52:48 2021 +0200

    Allow adding a comment when uploading to DL
    
    This patch adds a new dialog before uploading to DL where the user can
    add a comment that will be attached with the uploaded file.
    
    Fix https://gitlab.gnome.org/GNOME/gtranslator/-/issues/135

 po/POTFILES.in                |   2 +
 src/gtr-actions-file.c        |  44 ++++++++++++--
 src/gtr-upload-dialog.c       | 138 ++++++++++++++++++++++++++++++++++++++++++
 src/gtr-upload-dialog.h       |  73 ++++++++++++++++++++++
 src/gtr-upload-dialog.ui      |  81 +++++++++++++++++++++++++
 src/gtranslator.gresource.xml |   1 +
 src/meson.build               |   2 +
 7 files changed, 336 insertions(+), 5 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 43e8eae1..5caf8322 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -39,6 +39,8 @@ src/gtr-search-bar.ui
 src/gtr-tab-label.c
 src/gtr-tab.c
 src/gtr-tab.ui
+src/gtr-upload-dialog.c
+src/gtr-upload-dialog.ui
 src/gtr-utils.c
 src/gtr-view.c
 src/gtr-window.c
diff --git a/src/gtr-actions-file.c b/src/gtr-actions-file.c
index 2f8d6fd4..2324016f 100644
--- a/src/gtr-actions-file.c
+++ b/src/gtr-actions-file.c
@@ -41,6 +41,7 @@
 #include "gtr-tab.h"
 #include "gtr-utils.h"
 #include "gtr-window.h"
+#include "gtr-upload-dialog.h"
 
 #define GTR_TAB_SAVE_AS    "gtr-tab-save-as"
 #define GTR_IS_CLOSING_ALL "gtr-is-closing-all"
@@ -342,12 +343,10 @@ confirm_overwrite_callback (GtkFileChooser * dialog, gpointer data)
   return res;
 }
 
-/*
- * "Upload file" dialog
- *
- */
 void
-gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
+gtr_upload_file (GtkWidget *upload_dialog,
+                 int        response_id,
+                 gpointer   user_data)
 {
   GtrTab *tab;
   GtrPo *po;
@@ -369,12 +368,23 @@ gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
   g_autofree gchar *upload_endpoint = NULL;
   const char *auth_token = NULL;
   g_autofree char *auth = NULL;
+  g_autofree char *upload_comment = NULL;
   gsize size;
   const gchar *selected_team;
   const gchar *selected_module;
   const gchar *selected_branch;
   const gchar *selected_domain;
 
+  GtrWindow * window = GTR_WINDOW (user_data);
+
+  if (response_id != GTK_RESPONSE_ACCEPT)
+    {
+      gtk_widget_destroy (upload_dialog);
+      return;
+    }
+
+  upload_comment = gtr_upload_dialog_get_comment (GTR_UPLOAD_DIALOG (upload_dialog));
+
   /* Get file content */
   tab = gtr_window_get_active_tab (window);
   po = gtr_tab_get_po (tab);
@@ -427,6 +437,7 @@ gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
   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);
 
   /* Get the associated message */
   msg = soup_form_request_new_from_multipart (upload_endpoint, mpart);
@@ -436,6 +447,8 @@ gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
   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))
@@ -450,6 +463,7 @@ gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
           gtk_dialog_run (GTK_DIALOG (dialog));
           gtk_widget_destroy (dialog);
           gtr_notebook_enable_upload (active_notebook, FALSE);
+          gtk_widget_destroy (upload_dialog);
           return;
         }
 
@@ -467,9 +481,12 @@ gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
         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,
@@ -485,6 +502,23 @@ gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
   gtr_notebook_enable_upload (active_notebook, FALSE);
 }
 
+/*
+ * "Upload file" dialog
+ *
+ */
+void
+gtr_upload_file_dialog (GtkAction * action, GtrWindow * window)
+{
+  GtrUploadDialog *dialog = gtr_upload_dialog_new (GTK_WIDGET (window));
+
+  g_signal_connect (dialog,
+                   "response",
+                   G_CALLBACK (gtr_upload_file),
+                   window);
+
+  gtk_widget_show_all (GTK_WIDGET (dialog));
+}
+
 /*
  * "Save as" dialog.
  */
diff --git a/src/gtr-upload-dialog.c b/src/gtr-upload-dialog.c
new file mode 100644
index 00000000..d9e4c297
--- /dev/null
+++ b/src/gtr-upload-dialog.c
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2021  Daniel García <danigm gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *   Daniel García <danigm gnome org>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "gtr-upload-dialog.h"
+
+#include <string.h>
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+typedef struct
+{
+  GtkWidget *main_box;
+  GtkWidget *text_view;
+  GtkWidget *label;
+  GtkWidget *spinner;
+} GtrUploadDialogPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (GtrUploadDialog, gtr_upload_dialog, GTK_TYPE_DIALOG)
+
+static void
+gtr_upload_dialog_class_init (GtrUploadDialogClass *klass)
+{
+}
+
+static void
+gtr_upload_dialog_init (GtrUploadDialog *dlg)
+{
+  GtrUploadDialogPrivate *priv = gtr_upload_dialog_get_instance_private (dlg);
+  GtkBox *content_area;
+  GtkBuilder *builder;
+  gchar *root_objects[] = {
+    "main_box",
+    NULL
+  };
+
+  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);
+  gtk_window_set_destroy_with_parent (GTK_WINDOW (dlg), TRUE);
+  gtk_window_set_modal (GTK_WINDOW (dlg), TRUE);
+
+  content_area = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg)));
+
+  /* HIG defaults */
+  gtk_container_set_border_width (GTK_CONTAINER (dlg), 5);
+  gtk_box_set_spacing (content_area, 2); /* 2 * 5 + 2 = 12 */
+
+  builder = gtk_builder_new ();
+  GError *error = NULL;
+  gtk_builder_add_objects_from_resource (builder, "/org/gnome/translator/gtr-upload-dialog.ui",
+                                         root_objects, &error);
+  if (error)
+  {
+    g_message("%s", error->message);
+  }
+  priv->main_box = GTK_WIDGET (gtk_builder_get_object (builder, "main_box"));
+  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);
+}
+
+GtrUploadDialog *
+gtr_upload_dialog_new (GtkWidget  *parent)
+{
+  GtrUploadDialog *dlg;
+
+  dlg = g_object_new (GTR_TYPE_UPLOAD_DIALOG, NULL);
+
+  if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dlg)))
+    {
+      gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (parent));
+    }
+
+  return dlg;
+}
+
+char *
+gtr_upload_dialog_get_comment (GtrUploadDialog *dlg)
+{
+  GtrUploadDialogPrivate *priv = gtr_upload_dialog_get_instance_private (dlg);
+  GtkTextView *view = GTK_TEXT_VIEW (priv->text_view);
+  GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);
+  char * comment = NULL;
+
+  GtkTextIter start, end;
+  gtk_text_buffer_get_start_iter (buffer, &start);
+  gtk_text_buffer_get_end_iter (buffer, &end);
+  comment = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+
+  return comment;
+}
+
+void
+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));
+    }
+  else
+    {
+      gtk_widget_hide (priv->spinner);
+      gtk_widget_set_sensitive (priv->text_view, TRUE);
+      gtk_spinner_stop (GTK_SPINNER (priv->spinner));
+    }
+}
diff --git a/src/gtr-upload-dialog.h b/src/gtr-upload-dialog.h
new file mode 100644
index 00000000..889297c9
--- /dev/null
+++ b/src/gtr-upload-dialog.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2021  Daniel García <danigm gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *   Daniel García <danigm gnome org>
+ */
+
+#ifndef __UPLOAD_DIALOG_H__
+#define __UPLOAD_DIALOG_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+/*
+ * Type checking and casting macros
+ */
+#define GTR_TYPE_UPLOAD_DIALOG          (gtr_upload_dialog_get_type ())
+#define GTR_UPLOAD_DIALOG(o)            (G_TYPE_CHECK_INSTANCE_CAST ((o), GTR_TYPE_UPLOAD_DIALOG, 
GtrUploadDialog))
+#define GTR_UPLOAD_DIALOG_CLASS(k)      (G_TYPE_CHECK_CLASS_CAST((k), GTR_TYPE_UPLOAD_DIALOG, 
GtrUploadDialogClass))
+#define GTR_IS_UPLOAD_DIALOG(o)         (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTR_TYPE_UPLOAD_DIALOG))
+#define GTR_IS_UPLOAD_DIALOG_CLASS(k)   (G_TYPE_CHECK_CLASS_TYPE ((k), GTR_TYPE_UPLOAD_DIALOG))
+#define GTR_UPLOAD_DIALOG_GET_CLASS(o)  (G_TYPE_INSTANCE_GET_CLASS ((o), GTR_TYPE_UPLOAD_DIALOG, 
GtrUploadDialogClass))
+
+/*
+ * Main object structure
+ */
+typedef struct _GtrUploadDialog GtrUploadDialog;
+
+struct _GtrUploadDialog
+{
+  GtkDialog parent_instance;
+};
+
+/*
+ * Class definition
+ */
+typedef struct _GtrUploadDialogClass GtrUploadDialogClass;
+
+struct _GtrUploadDialogClass
+{
+  GtkDialogClass parent_class;
+};
+
+/*
+ * Public methods
+ */
+GType              gtr_upload_dialog_get_type         (void) G_GNUC_CONST;
+
+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);
+
+G_END_DECLS
+
+#endif
diff --git a/src/gtr-upload-dialog.ui b/src/gtr-upload-dialog.ui
new file mode 100644
index 00000000..0fc8576b
--- /dev/null
+++ b/src/gtr-upload-dialog.ui
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.22.1 -->
+<interface>
+  <requires lib="gtk+" version="3.0"/>
+  <object class="GtkDialog" id="dialog1">
+    <property name="can_focus">False</property>
+    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | 
GDK_BUTTON_RELEASE_MASK</property>
+    <property name="border_width">5</property>
+    <property name="modal">True</property>
+    <property name="window_position">center-on-parent</property>
+    <property name="type_hint">dialog</property>
+    <child type="titlebar">
+      <placeholder/>
+    </child>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="vbox1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child internal-child="action_area">
+          <object class="GtkButtonBox" id="dialog-action_area1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
+              <placeholder/>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkBox" id="main_box">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="orientation">vertical</property>
+            <property name="spacing">18</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>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTextView" id="text_view">
+                <property name="width_request">400</property>
+                <property name="height_request">100</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <style>
+                  <class name="upload-comment"/>
+                </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>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/src/gtranslator.gresource.xml b/src/gtranslator.gresource.xml
index 4b09aff7..17426cd4 100644
--- a/src/gtranslator.gresource.xml
+++ b/src/gtranslator.gresource.xml
@@ -19,5 +19,6 @@
     <file preprocess="xml-stripblanks">gtr-filter-selection.ui</file>
     <file preprocess="xml-stripblanks">help-overlay.ui</file>
     <file preprocess="xml-stripblanks">gtr-search-bar.ui</file>
+    <file preprocess="xml-stripblanks">gtr-upload-dialog.ui</file>
   </gresource>
 </gresources>
diff --git a/src/meson.build b/src/meson.build
index c4d7216b..763490b9 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -72,6 +72,7 @@ sources = files(
   'gtr-progress.c',
   'gtr-window.c',
   'gtr-search-bar.c',
+  'gtr-upload-dialog.c',
 )
 
 marshal = 'gtr-marshal'
@@ -111,6 +112,7 @@ resource_data = files(
   'gtr-tab.ui',
   'gtr-window.ui',
   'gtr-search-bar.ui',
+  'gtr-upload-dialog.ui',
 )
 
 sources += gnome.compile_resources(


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