[gtranslator] Translation memory scan dir as a GTask



commit 753dc4e32b3fc9431a870d351d4da74b68428b40
Author: Daniel García Moreno <danigm wadobo com>
Date:   Wed Nov 14 14:30:53 2018 +0100

    Translation memory scan dir as a GTask
    
    I've removed the success and error dialogs because were causing some
    problems, and I've changed that by disable/enable the button. I think
    that we can show an information text in this dialog to show when finish
    or something like that, but for now it should work.
    
    Fix #37

 .../gtr-translation-memory-dialog.c                | 137 ++++++++++++---------
 .../gtr-translation-memory-dialog.ui               |  20 +--
 2 files changed, 92 insertions(+), 65 deletions(-)
---
diff --git a/src/translation-memory/gtr-translation-memory-dialog.c 
b/src/translation-memory/gtr-translation-memory-dialog.c
index 846417e8..0e88e5d1 100644
--- a/src/translation-memory/gtr-translation-memory-dialog.c
+++ b/src/translation-memory/gtr-translation-memory-dialog.c
@@ -104,6 +104,7 @@ typedef struct _IdleData
   GtkProgressBar *progress;
   GtrTranslationMemory *tm;
   GtkWindow *parent;
+  GtkWidget *add_database_button;
 } IdleData;
 
 static gboolean
@@ -140,26 +141,7 @@ add_to_database (gpointer data_pointer)
     }
   else
     {
-      GtkWidget *dialog;
-      gchar *markup;
-
-      gtk_progress_bar_set_fraction (data->progress, 1.0);
-
-      dialog = gtk_message_dialog_new (data->parent,
-                                       GTK_DIALOG_DESTROY_WITH_PARENT,
-                                       GTK_MESSAGE_INFO,
-                                       GTK_BUTTONS_CLOSE, NULL);
-
-      markup =
-        g_strdup_printf("<span weight=\"bold\" size=\"large\">%s</span>",
-                        _("Strings added to database"));
-      gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog), markup);
-      g_free(markup);
-
-      g_signal_connect (dialog, "response",
-                        G_CALLBACK (gtk_widget_destroy), NULL);
-      gtk_widget_show (dialog);
-
+      gtk_widget_set_sensitive (data->add_database_button, TRUE);
       return FALSE;
     }
 
@@ -190,14 +172,87 @@ destroy_idle_data (gpointer data)
   g_free (d);
 }
 
+typedef struct
+{
+  GFile *dir;
+  gchar *restriction;
+} ScanDirTaskData;
+
+static void
+task_data_destroy (ScanDirTaskData *data)
+{
+  if (data->restriction)
+    g_free (data->restriction);
+  g_object_unref (data->dir);
+
+  g_free (data);
+}
+
+static void
+scan_dir_task_func (GTask                      *task,
+                    GtrTranslationMemoryDialog *dlg,
+                    ScanDirTaskData            *data,
+                    GCancellable               *cancellable)
+{
+  GSList *list = NULL;
+  gtr_scan_dir (data->dir, &list, data->restriction);
+  g_task_return_pointer (task, list, (GDestroyNotify)g_slist_free);
+}
+
+static void
+scan_dir_task_ready_cb (GtrTranslationMemoryDialog *dlg,
+                        GTask                      *task,
+                        IdleData                   *data)
+{
+  data->list = g_task_propagate_pointer (task, NULL);
+
+  g_idle_add_full (G_PRIORITY_HIGH_IDLE + 30,
+                   (GSourceFunc) add_to_database,
+                   data, (GDestroyNotify) destroy_idle_data);
+}
+
+static void
+launch_gtr_scan_dir_task (GtrTranslationMemoryDialog *dlg,
+                          ScanDirTaskData            *data)
+{
+  GTask *task;
+  GCancellable *cancellable;
+  IdleData *idata;
+  GtrTranslationMemoryDialogPrivate *priv = gtr_translation_memory_dialog_get_instance_private (dlg);
+
+  cancellable = g_cancellable_new ();
+  // TODO: connect cancellable cancel signal
+
+  idata = g_new0 (IdleData, 1);
+  idata->list = NULL;
+  idata->tm = priv->translation_memory;
+  idata->progress = GTK_PROGRESS_BAR (priv->add_database_progressbar);
+  idata->parent = GTK_WINDOW (dlg);
+  idata->add_database_button = priv->add_database_button;
+
+  gtk_progress_bar_pulse (idata->progress);
+  gtk_widget_show (priv->add_database_progressbar);
+
+  gtk_widget_set_sensitive (priv->add_database_button, FALSE);
+
+  task = g_task_new (dlg,
+                     cancellable,
+                     (GAsyncReadyCallback)scan_dir_task_ready_cb,
+                     idata);
+
+  g_task_set_task_data (task, data, (GDestroyNotify)task_data_destroy);
+
+  g_task_run_in_thread (task,
+                        (GTaskThreadFunc) scan_dir_task_func);
+}
+
 static void
 on_add_database_button_clicked (GtkButton                  *button,
                                 GtrTranslationMemoryDialog *dlg)
 {
-  GFile *dir;
   gchar *dir_name;
-  IdleData *data;
   GtrTranslationMemoryDialogPrivate *priv = gtr_translation_memory_dialog_get_instance_private (dlg);
+  ScanDirTaskData *scan_dir_data;
 
   dir_name = g_settings_get_string (priv->tm_settings,
                                     "po-directory");
@@ -205,49 +260,21 @@ on_add_database_button_clicked (GtkButton                  *button,
   /* If dir name is empty, show a warning message */
   if (*dir_name == '\0')
     {
-      GtkWidget *dialog;
-      dialog = gtk_message_dialog_new (GTK_WINDOW (dlg),
-                                       GTK_DIALOG_DESTROY_WITH_PARENT,
-                                       GTK_MESSAGE_WARNING,
-                                       GTK_BUTTONS_CLOSE,
-                                       _("Please specify a valid path to build the translation memory"));
-
-      gtk_widget_show (dialog);
-      g_signal_connect (dialog, "response",
-                        G_CALLBACK (gtk_widget_destroy), NULL);
       g_free (dir_name);
       return;
     }
 
-  dir = g_file_new_for_path (dir_name);
+  scan_dir_data = g_new0 (ScanDirTaskData, 1);
+  scan_dir_data->dir = g_file_new_for_path (dir_name);
   g_free (dir_name);
 
-  data = g_new0 (IdleData, 1);
-  data->list = NULL;
-
   if (g_settings_get_boolean (priv->tm_settings,
                               "restrict-to-filename"))
     {
-      gchar *restriction;
-
-      restriction = g_settings_get_string (priv->tm_settings,
-                                           "filename-restriction");
-      gtr_scan_dir (dir, &data->list, restriction);
-      g_free (restriction);
+      scan_dir_data->restriction = g_settings_get_string (priv->tm_settings,
+                                                          "filename-restriction");
     }
-  else
-    gtr_scan_dir (dir, &data->list, NULL);
-
-  data->tm = priv->translation_memory;
-  data->progress = GTK_PROGRESS_BAR (priv->add_database_progressbar);
-  data->parent = GTK_WINDOW (dlg);
-
-  gtk_widget_show (priv->add_database_progressbar);
-  g_idle_add_full (G_PRIORITY_HIGH_IDLE + 30,
-                   (GSourceFunc) add_to_database,
-                   data, (GDestroyNotify) destroy_idle_data);
-
-  g_object_unref (dir);
+  launch_gtr_scan_dir_task (dlg, scan_dir_data);
 }
 
 static void
diff --git a/src/translation-memory/gtr-translation-memory-dialog.ui 
b/src/translation-memory/gtr-translation-memory-dialog.ui
index f77e5292..ad89ad12 100644
--- a/src/translation-memory/gtr-translation-memory-dialog.ui
+++ b/src/translation-memory/gtr-translation-memory-dialog.ui
@@ -1,10 +1,13 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
-  <!-- interface-requires gtk+ 3.0 -->
+  <requires lib="gtk+" version="3.0"/>
   <object class="GtkDialog" id="translation-memory-dialog">
     <property name="can_focus">False</property>
     <property name="border_width">5</property>
     <property name="type_hint">dialog</property>
+    <child>
+      <placeholder/>
+    </child>
     <child internal-child="vbox">
       <object class="GtkBox" id="dialog-vbox1">
         <property name="can_focus">False</property>
@@ -17,10 +20,10 @@
             <child>
               <object class="GtkButton" id="button1">
                 <property name="label">gtk-close</property>
+                <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="receives_default">True</property>
-                <property name="use_action_appearance">False</property>
                 <property name="use_stock">True</property>
               </object>
               <packing>
@@ -54,8 +57,8 @@
                   <object class="GtkLabel" id="label20">
                     <property name="visible">True</property>
                     <property name="can_focus">False</property>
-                    <property name="xalign">0</property>
                     <property name="label" translatable="yes">Database:</property>
+                    <property name="xalign">0</property>
                     <attributes>
                       <attribute name="weight" value="bold"/>
                     </attributes>
@@ -97,8 +100,8 @@
                               <object class="GtkLabel" id="label26">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
-                                <property name="xalign">0</property>
                                 <property name="label" translatable="yes">Select the directory which 
contains PO files:</property>
+                                <property name="xalign">0</property>
                               </object>
                               <packing>
                                 <property name="expand">False</property>
@@ -117,7 +120,6 @@
                                     <property name="can_focus">True</property>
                                     <property name="invisible_char">●</property>
                                     <property name="xalign">0.0099999997764825821</property>
-                                    <property name="invisible_char_set">True</property>
                                   </object>
                                   <packing>
                                     <property name="expand">True</property>
@@ -128,10 +130,10 @@
                                 <child>
                                   <object class="GtkButton" id="search-button">
                                     <property name="label">gtk-find</property>
+                                    <property name="use_action_appearance">False</property>
                                     <property name="visible">True</property>
                                     <property name="can_focus">True</property>
                                     <property name="receives_default">True</property>
-                                    <property name="use_action_appearance">False</property>
                                     <property name="use_stock">True</property>
                                   </object>
                                   <packing>
@@ -153,7 +155,6 @@
                                 <property name="visible">True</property>
                                 <property name="can_focus">True</property>
                                 <property name="receives_default">True</property>
-                                <property name="use_action_appearance">False</property>
                               </object>
                               <packing>
                                 <property name="expand">False</property>
@@ -205,8 +206,8 @@
                   <object class="GtkLabel" id="label21">
                     <property name="visible">True</property>
                     <property name="can_focus">False</property>
-                    <property name="xalign">0</property>
                     <property name="label" translatable="yes">Configuration:</property>
+                    <property name="xalign">0</property>
                     <attributes>
                       <attribute name="weight" value="bold"/>
                     </attributes>
@@ -250,10 +251,10 @@
                                 <child>
                                   <object class="GtkCheckButton" id="use-lang-profile-in-tm">
                                     <property name="label" translatable="yes">Use only files with this 
name:</property>
+                                    <property name="use_action_appearance">False</property>
                                     <property name="visible">True</property>
                                     <property name="can_focus">True</property>
                                     <property name="receives_default">False</property>
-                                    <property name="use_action_appearance">False</property>
                                     <property name="xalign">0</property>
                                     <property name="draw_indicator">True</property>
                                   </object>
@@ -268,7 +269,6 @@
                                     <property name="visible">True</property>
                                     <property name="can_focus">True</property>
                                     <property name="invisible_char">●</property>
-                                    <property name="invisible_char_set">True</property>
                                   </object>
                                   <packing>
                                     <property name="expand">False</property>


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