[gtranslator] Enable use of local mirrors of open-tran.eu.



commit eb11a30b5067cdad921a102b502e0c8902b8ccfa
Author: Yaron Sheffer <yaronf gmx com>
Date:   Tue Oct 26 13:55:36 2010 +0200

    Enable use of local mirrors of open-tran.eu.

 plugins/open-tran/gtr-open-tran-dialog.ui          |  142 ++++++++++++++++----
 plugins/open-tran/gtr-open-tran-panel.c            |   91 +++++++++----
 plugins/open-tran/gtr-open-tran-panel.h            |    2 +
 plugins/open-tran/gtr-open-tran-plugin.c           |   52 +++++++-
 ...gtranslator.plugins.open-tran.gschema.xml.in.in |   10 ++
 5 files changed, 245 insertions(+), 52 deletions(-)
---
diff --git a/plugins/open-tran/gtr-open-tran-dialog.ui b/plugins/open-tran/gtr-open-tran-dialog.ui
index 5fa869d..07da709 100644
--- a/plugins/open-tran/gtr-open-tran-dialog.ui
+++ b/plugins/open-tran/gtr-open-tran-dialog.ui
@@ -20,14 +20,33 @@
             <property name="border_width">12</property>
             <property name="spacing">6</property>
             <child>
-              <object class="GtkLabel" id="label1">
+              <object class="GtkHBox" id="hbox2">
                 <property name="visible">True</property>
-                <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-                <property name="xalign">0</property>
-                <property name="label" translatable="yes">Language code for the Open-Tran query:</property>
-                <attributes>
-                  <attribute name="weight" value="bold"/>
-                </attributes>
+                <child>
+                  <object class="GtkLabel" id="label1">
+                    <property name="visible">True</property>
+                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+                    <property name="xalign">0</property>
+                    <property name="xpad">5</property>
+                    <property name="label" translatable="yes">Language code for the Open-Tran query:</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">False</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkEntry" id="search_code">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+                    <property name="invisible_char">â??</property>
+                  </object>
+                  <packing>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
               </object>
               <packing>
                 <property name="expand">False</property>
@@ -36,52 +55,125 @@
               </packing>
             </child>
             <child>
-              <object class="GtkAlignment" id="alignment1">
+              <object class="GtkHBox" id="hbox3">
                 <property name="visible">True</property>
-                <property name="left_padding">12</property>
                 <child>
-                  <object class="GtkEntry" id="search_code">
+                  <object class="GtkLabel" id="label3">
+                    <property name="visible">True</property>
+                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+                    <property name="xalign">0</property>
+                    <property name="xpad">5</property>
+                    <property name="label" translatable="yes">Language code of the translated results:</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkEntry" id="own_code">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
                     <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                     <property name="invisible_char">â??</property>
                   </object>
+                  <packing>
+                    <property name="position">1</property>
+                  </packing>
                 </child>
               </object>
               <packing>
+                <property name="expand">False</property>
                 <property name="position">1</property>
               </packing>
             </child>
             <child>
-              <object class="GtkLabel" id="label3">
+              <object class="GtkLayout" id="layout1">
                 <property name="visible">True</property>
-                <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-                <property name="xalign">0</property>
-                <property name="label" translatable="yes">Language code of the translated results:</property>
-                <attributes>
-                  <attribute name="weight" value="bold"/>
-                </attributes>
               </object>
               <packing>
-                <property name="expand">False</property>
                 <property name="position">2</property>
               </packing>
             </child>
             <child>
-              <object class="GtkAlignment" id="alignment2">
+              <object class="GtkCheckButton" id="use_mirror_server">
+                <property name="label" translatable="yes">Use a local "mirror" copy of the Open-Tran server</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="image_position">bottom</property>
+                <property name="draw_indicator">True</property>
+              </object>
+              <packing>
+                <property name="position">3</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkFrame" id="mirror_server_frame">
                 <property name="visible">True</property>
-                <property name="left_padding">12</property>
+                <property name="sensitive">False</property>
+                <property name="label_xalign">0</property>
                 <child>
-                  <object class="GtkEntry" id="own_code">
+                  <object class="GtkAlignment" id="alignment5">
                     <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-                    <property name="invisible_char">â??</property>
+                    <property name="left_padding">12</property>
+                    <child>
+                      <object class="GtkVBox" id="vbox1">
+                        <property name="visible">True</property>
+                        <child>
+                          <object class="GtkLabel" id="label2">
+                            <property name="visible">True</property>
+                            <property name="xalign">0.039999999105930328</property>
+                            <property name="label" translatable="yes">Mirror server URL:</property>
+                          </object>
+                          <packing>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkAlignment" id="alignment1">
+                            <property name="visible">True</property>
+                            <property name="left_padding">30</property>
+                            <child>
+                              <object class="GtkEntry" id="mirror_server_url">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="invisible_char">â??</property>
+                              </object>
+                            </child>
+                          </object>
+                          <packing>
+                            <property name="position">1</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkLabel" id="mirror_server_url_note">
+                            <property name="visible">True</property>
+                            <property name="xalign">0</property>
+                            <property name="label" translatable="yes">&lt;i&gt;The URL should be of the form:
+&lt;b&gt;http://%s.%s.open-tran.example.com/json/suggest/&lt;/b&gt;
+The two "%s" markers and the final slash must not be omitted.&lt;/i&gt;</property>
+                            <property name="use_markup">True</property>
+                            <property name="wrap">True</property>
+                          </object>
+                          <packing>
+                            <property name="position">2</property>
+                          </packing>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+                <child type="label">
+                  <object class="GtkLabel" id="label4">
+                    <property name="visible">True</property>
+                    <property name="label" translatable="yes">Mirror Server</property>
+                    <property name="use_markup">True</property>
                   </object>
                 </child>
               </object>
               <packing>
-                <property name="position">3</property>
+                <property name="position">4</property>
               </packing>
             </child>
           </object>
diff --git a/plugins/open-tran/gtr-open-tran-panel.c b/plugins/open-tran/gtr-open-tran-panel.c
index 8505344..a3bad0d 100644
--- a/plugins/open-tran/gtr-open-tran-panel.c
+++ b/plugins/open-tran/gtr-open-tran-panel.c
@@ -104,6 +104,32 @@ show_error_dialog (GtrWindow * parent,
   gtk_widget_show (dialog);
 }
 
+static gchar *
+get_service_url (gboolean use_mirror_server, gchar *mirror_server_url,
+		 const gchar * search_text,
+                 const gchar * search_code, const gchar * own_code)
+{
+  const gchar *base_url;
+  gchar *url, *full_url, *escaped_text;
+  const gchar * open_tran_url = "http://%s.%s.open-tran.eu/json/suggest/";;
+  /* URL placeholders: source lang, target lang. Search string is appended. */
+
+  if (!use_mirror_server)
+    base_url = open_tran_url;
+  else
+    base_url = mirror_server_url;
+
+  escaped_text = (gchar *) xmlURIEscapeStr ((const xmlChar *) search_text, (const xmlChar *) "");
+  url = g_strdup_printf (base_url, search_code, own_code);
+  full_url = g_strconcat (url, escaped_text, NULL);
+
+  g_free (url);
+  if (escaped_text)
+    xmlFree (escaped_text);
+
+  return full_url;
+}
+
 static GdkPixbuf *
 create_pixbuf (const gchar * path)
 {
@@ -170,16 +196,12 @@ print_string_to_tree_view (const gchar *iconname, const gchar * str,
 
 static void
 open_connection (GtrOpenTranPanel * panel,
-                 const gchar * text,
-                 const gchar * search_code, const gchar * own_code)
+		 const gchar *service_url,
+                 const gchar * text)
 {
-  gchar * open_tran_url = "http://%s.%s.open-tran.eu/json/suggest/%s";;
-  /* URL placeholders: source lang, target lang, phrase */
-  gchar *url_buf;
   GError *err = NULL;
   GtkTreeIter treeiter;
   void * ctx;
-  xmlChar *escaped;
   unsigned int offset = 0;
   int ret;
   gchar * json_buf;
@@ -188,21 +210,14 @@ open_connection (GtrOpenTranPanel * panel,
   JsonArray *array;
   int idx;
 
-  escaped = xmlURIEscapeStr ((const xmlChar *) text, (const xmlChar *) "");
-  url_buf = g_strdup_printf (open_tran_url, search_code, own_code, escaped);
-
-  if (escaped)
-    xmlFree (escaped);
-
-  ctx = xmlNanoHTTPOpen (url_buf, NULL);
+  ctx = xmlNanoHTTPOpen (service_url, NULL);
   if (!ctx)
     {
       show_error_dialog (panel->priv->window,
-                         _("ERROR: Cannot access %s\n"), url_buf);
+                         _("ERROR: Cannot access %s\n"), service_url);
       return;
     }
 
-  g_free (url_buf);
   json_buf = g_malloc (JSON_LENGTH);
 
   do
@@ -348,8 +363,11 @@ static void
 entry_activate_cb (GtkEntry * entry, GtrOpenTranPanel * panel)
 {
   const gchar *entry_text;
-  gchar *search_code;
-  gchar *own_code;
+  gchar *search_code = NULL;
+  gchar *own_code = NULL;
+  gboolean use_mirror_server;
+  gchar *mirror_server_url = NULL;
+  gchar *service_url = NULL;
 
   gtk_tree_store_clear (panel->priv->store);
 
@@ -358,29 +376,52 @@ entry_activate_cb (GtkEntry * entry, GtrOpenTranPanel * panel)
     {
       show_error_dialog (panel->priv->window,
                          _("You have to provide a phrase to search"));
-      return;
+      goto cleanup;
     }
 
   search_code = g_settings_get_string (panel->priv->settings,
                                        GTR_SETTINGS_SEARCH_CODE);
-  if (!search_code)
+  if (!search_code || strlen (search_code) == 0)
     {
       show_error_dialog (panel->priv->window,
-                         _("You have to provide a search language code"));
-      return;
+                         _("You have to provide a search language code in the plugin configuration"));
+      goto cleanup;
     }
 
   own_code = g_settings_get_string (panel->priv->settings,
                                     GTR_SETTINGS_OWN_CODE);
 
-  if (!own_code)
+  if (!own_code || strlen (own_code) == 0)
     {
       show_error_dialog (panel->priv->window,
-                         _("You have to provide a language code for your language"));
-      return;
+                         _("You have to provide a language code for your language in the plugin configuration"));
+      goto cleanup;
+    }
+
+  use_mirror_server = g_settings_get_boolean (panel->priv->settings,
+                                       GTR_SETTINGS_USE_MIRROR_SERVER);
+  mirror_server_url = g_settings_get_string (panel->priv->settings,
+                                       GTR_SETTINGS_MIRROR_SERVER_URL);
+  
+  if (use_mirror_server && (!mirror_server_url || strlen (mirror_server_url) == 0))
+    {
+      /* Note: the two "%s" in the next string are not interpolated */
+      show_error_dialog (panel->priv->window,
+                         _("Either use the main open-tran.eu server,"
+			   " or enter a server URL in the plugin configuration,"));
+      goto cleanup;
     }
 
-  open_connection (panel, entry_text, search_code, own_code);
+  service_url = get_service_url (use_mirror_server, mirror_server_url,
+				 entry_text,
+				 search_code, own_code);
+  open_connection (panel, service_url, entry_text);
+
+ cleanup:
+  g_free (search_code);
+  g_free (own_code);
+  g_free (service_url);
+  g_free (mirror_server_url);
 }
 
 static void
diff --git a/plugins/open-tran/gtr-open-tran-panel.h b/plugins/open-tran/gtr-open-tran-panel.h
index efb4435..910287b 100644
--- a/plugins/open-tran/gtr-open-tran-panel.h
+++ b/plugins/open-tran/gtr-open-tran-panel.h
@@ -39,6 +39,8 @@ G_BEGIN_DECLS
 /* GSettings keys */
 #define GTR_SETTINGS_SEARCH_CODE "search-code"
 #define GTR_SETTINGS_OWN_CODE "own-code"
+#define GTR_SETTINGS_USE_MIRROR_SERVER "use-mirror-server"
+#define GTR_SETTINGS_MIRROR_SERVER_URL "mirror-server-url"
 
 /* Private structure type */
 typedef struct _GtrOpenTranPanelPrivate GtrOpenTranPanelPrivate;
diff --git a/plugins/open-tran/gtr-open-tran-plugin.c b/plugins/open-tran/gtr-open-tran-plugin.c
index d778deb..e7f2e88 100644
--- a/plugins/open-tran/gtr-open-tran-plugin.c
+++ b/plugins/open-tran/gtr-open-tran-plugin.c
@@ -50,6 +50,9 @@ struct _GtrOpenTranPluginPrivate
   GtkWidget *main_box;
   GtkWidget *search_code_entry;
   GtkWidget *own_code_entry;
+  GtkWidget *use_mirror_server_entry;
+  GtkWidget *mirror_server_url_entry;
+  GtkWidget *mirror_server_frame_entry;
 };
 
 typedef struct
@@ -146,7 +149,14 @@ get_configuration_dialog (GtrOpenTranPlugin * plugin)
                                   "search_code",
                                   &plugin->priv->search_code_entry,
                                   "own_code",
-                                  &plugin->priv->own_code_entry, NULL);
+                                  &plugin->priv->own_code_entry,
+                                  "use_mirror_server",
+                                  &plugin->priv->use_mirror_server_entry,
+                                  "mirror_server_url",
+                                  &plugin->priv->mirror_server_url_entry,
+                                  "mirror_server_frame",
+                                  &plugin->priv->mirror_server_frame_entry,
+				  NULL);
   if (!ret)
     {
       g_error (_("Error from configuration dialog %s"), path);
@@ -166,6 +176,18 @@ get_configuration_dialog (GtrOpenTranPlugin * plugin)
                    "text",
                    G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
 
+  g_settings_bind (plugin->priv->settings,
+                   GTR_SETTINGS_USE_MIRROR_SERVER,
+                   plugin->priv->use_mirror_server_entry,
+                   "active",
+                   G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
+
+  g_settings_bind (plugin->priv->settings,
+                   GTR_SETTINGS_MIRROR_SERVER_URL,
+                   plugin->priv->mirror_server_url_entry,
+                   "text",
+                   G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
+
   return plugin->priv->dialog;
 }
 
@@ -176,20 +198,46 @@ configure_dialog_response_cb (GtkWidget * widget,
   gtk_widget_destroy (plugin->priv->dialog);
 }
 
+static void
+use_mirror_server_toggled_cb (GtkWidget *widget,
+			      GtrOpenTranPlugin * plugin)
+{
+  GtkWidget *mirror_server_url_entry;
+  GtkWidget *mirror_server_frame_entry;
+  gboolean use_mirror_server;
+
+  mirror_server_url_entry = plugin->priv->mirror_server_url_entry;
+  mirror_server_frame_entry = plugin->priv->mirror_server_frame_entry;
+  use_mirror_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
+  gtk_widget_set_sensitive (mirror_server_frame_entry, use_mirror_server);
+}
+
 static GtkWidget *
 impl_create_configure_dialog (GtrPlugin * plugin)
 {
   GtkWidget *dialog;
+  GtrOpenTranPluginPrivate *priv = GTR_OPEN_TRAN_PLUGIN (plugin)->priv;
+  gboolean use_mirror_server;
 
   dialog = get_configuration_dialog (GTR_OPEN_TRAN_PLUGIN (plugin));
 
   g_signal_connect (dialog,
                     "response",
                     G_CALLBACK (configure_dialog_response_cb),
-                    GTR_OPEN_TRAN_PLUGIN (plugin));
+                    plugin);
   g_signal_connect (dialog,
                     "destroy", G_CALLBACK (gtk_widget_destroy), &dialog);
 
+  g_signal_connect (priv->use_mirror_server_entry,
+                    "toggled",
+                    G_CALLBACK (use_mirror_server_toggled_cb),
+                    plugin);
+
+  /* Set the sensitivity of the mirror server area */
+  use_mirror_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->use_mirror_server_entry));
+  gtk_widget_set_sensitive (priv->mirror_server_frame_entry,
+			    use_mirror_server);
+
   return dialog;
 }
 
diff --git a/plugins/open-tran/org.gnome.gtranslator.plugins.open-tran.gschema.xml.in.in b/plugins/open-tran/org.gnome.gtranslator.plugins.open-tran.gschema.xml.in.in
index 6de8a8d..6133d61 100644
--- a/plugins/open-tran/org.gnome.gtranslator.plugins.open-tran.gschema.xml.in.in
+++ b/plugins/open-tran/org.gnome.gtranslator.plugins.open-tran.gschema.xml.in.in
@@ -10,5 +10,15 @@
       <_summary>Own Code</_summary>
       <_description>The language code in which you want the results</_description>
     </key>
+    <key name="use-mirror-server" type="b">
+      <default>false</default>
+      <_summary>Use Mirror Server</_summary>
+      <_description>Use a local copy of the main Open-Tran server at open-tran.eu</_description>
+    </key>
+    <key name="mirror-server-url" type="s">
+      <default>''</default>
+      <_summary>Mirror Server URL</_summary>
+      <_description>URL of an Open-Tran mirror server</_description>
+    </key>
   </schema>
 </schemalist>



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