From: Andrea Zagli <azagli@libero.it>
Date: Sat, 30 Jun 2018 14:01:26 +0000 (+0200)
Subject: Aggiustamenti di ooo per funzionare senza finestre gtk.
X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=4b0b5161e44f0429bd272272aff4d31b8564133a;p=solipa%2Flibsolipa

Aggiustamenti di ooo per funzionare senza finestre gtk.
---

diff --git a/.gitignore b/.gitignore
index c6955c9..95dbbab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,6 +56,7 @@ tests/mail_check_address
 tests/mailui
 tests/log
 tests/ooo
+tests/ooo_nox
 tests/ooo_pycmd
 tests/progresswindow
 tests/utils
@@ -71,4 +72,4 @@ tests/utils_message_dialog
 tests/utils_round
 *.csv
 *.gir
-*.typelib
\ No newline at end of file
+*.typelib
diff --git a/src/ooo.c b/src/ooo.c
index c4aa4c7..901f6bc 100644
--- a/src/ooo.c
+++ b/src/ooo.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2011-2018 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -111,7 +111,10 @@ static SolipaOOO
 	SolipaOOO *solipa_ooo;
 	SolipaOOOPrivate *priv;
 
-	g_return_val_if_fail (GTK_IS_WINDOW (transient), NULL);
+	if (transient != NULL)
+		{
+			g_return_val_if_fail (GTK_IS_WINDOW (transient), NULL);
+		}
 
 	solipa_ooo = SOLIPA_OOO (g_object_new (solipa_ooo_get_type (), NULL));
 
@@ -329,7 +332,7 @@ gchar
 	                       "found.String = %s.replace(found.String, u\"%s\", u\"%s\")\n",
 	                       !priv->with_search ? "search = document.createSearchDescriptor()\n" : "",
 	                       _search,
-						   priv->ooo_version == NULL || zak_utils_compare_version (priv->ooo_version, "3.0.0", NULL, &part) > 0 ? "str" : "unicode",
+	                       priv->ooo_version == NULL || zak_utils_compare_version (priv->ooo_version, "3.0.0", NULL, &part) > 0 ? "str" : "unicode",
 	                       _search,
 	                       _replace);
 	priv->with_search = TRUE;
@@ -359,21 +362,6 @@ solipa_ooo_string_replace (SolipaOOO *ooo, const gchar *search, const gchar *rep
 
 	str = solipa_ooo_pycmd_string_replace (ooo, search, replace);
 
-	/* controllo se SearchDescriptor esiste o no */
-	if (priv->search == NULL)
-		{
-			priv->search = PyDict_GetItemString (priv->dict, "search");
-			if (priv->search == NULL)
-				{
-					priv->search = PyDict_GetItemString (priv->dict, "search");
-					if (priv->search == NULL)
-						{
-							g_warning ("Unable to create Search object.");
-							return;
-						}
-				}
-		}
-
 	PyRun_SimpleString (str);
 	g_free (str);
 }
@@ -502,7 +490,9 @@ solipa_ooo_pycmd_apri_openoffice (SolipaOOO *ooo)
 
 	pclose (pipa);
 
-	if (in_esecuzione)
+	if (in_esecuzione
+	    && priv->transient != NULL
+	    && GTK_IS_WINDOW (priv->transient))
 		{
 			dialog = gtk_message_dialog_new (priv->transient,
 			                                 GTK_DIALOG_DESTROY_WITH_PARENT,
@@ -518,21 +508,25 @@ solipa_ooo_pycmd_apri_openoffice (SolipaOOO *ooo)
 			gtk_widget_destroy (dialog);
 		}
 
-	priv->dialog_response = FALSE;
-	dialog = gtk_message_dialog_new (priv->transient,
-	                                 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
-	                                 GTK_MESSAGE_INFO,
-	                                 GTK_BUTTONS_CANCEL,
-	                                 "Apertura di OpenOffice/LibreOffice in corso...");
-	g_signal_connect (G_OBJECT (dialog), "response",
-	                  G_CALLBACK (dialog_response), ooo);
-	gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->transient);
-	gtk_window_set_deletable (GTK_WINDOW (dialog), FALSE);
-	gtk_widget_show_now (dialog);
-
-	while (gtk_events_pending ())
+	if (priv->transient != NULL
+	    && GTK_IS_WINDOW (priv->transient))
 		{
-			gtk_main_iteration ();
+			priv->dialog_response = FALSE;
+			dialog = gtk_message_dialog_new (priv->transient,
+			                                 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+			                                 GTK_MESSAGE_INFO,
+			                                 GTK_BUTTONS_CANCEL,
+			                                 "Apertura di OpenOffice/LibreOffice in corso...");
+			g_signal_connect (G_OBJECT (dialog), "response",
+			                  G_CALLBACK (dialog_response), ooo);
+			gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->transient);
+			gtk_window_set_deletable (GTK_WINDOW (dialog), FALSE);
+			gtk_widget_show_now (dialog);
+
+			while (gtk_events_pending ())
+				{
+					gtk_main_iteration ();
+				}
 		}
 
 	if (in_esecuzione)
@@ -558,38 +552,44 @@ solipa_ooo_pycmd_apri_openoffice (SolipaOOO *ooo)
 #ifdef G_OS_WIN32
 
 	command = g_strconcat ("\"",
-						   priv->ooo_soffice,
-						   g_strdup_printf (" -accept=\"socket,host=localhost,port=%d;urp;\" %s-norestore %s\"",
-											priv->ooo_port,
-											dash,
-											g_strdup_printf ("%s%s",
-															 dash,
-															 priv->visible ? "-invisible" : "-headless")),
-						   NULL);
+	                       priv->ooo_soffice,
+	                       g_strdup_printf (" %s-accept=\"socket,host=localhost,port=%d;urp;\" %s-norestore %s%s\"",
+	                                        dash,
+	                                        priv->ooo_port,
+	                                        dash,
+	                                        dash,
+	                                        priv->visible ? "-invisible" : "-headless"),
+	                       NULL);
 
 #else
 
 	command = g_strconcat ("\"",
-						   priv->ooo_soffice,
-						   g_strdup_printf ("\" -accept=\"socket,host=localhost,port=%d;urp;\" %s-norestore %s",
-											priv->ooo_port,
-											dash,
-											g_strdup_printf ("%s%s",
-															 dash,
-															 priv->visible ? "-invisible" : "-headless")),
-						   NULL);
+	                       priv->ooo_soffice,
+	                       g_strdup_printf ("\" %s-accept=\"socket,host=localhost,port=%d;urp;\" %s-norestore %s%s",
+	                                        dash,
+	                                        priv->ooo_port,
+	                                        dash,
+	                                        dash,
+	                                        priv->visible ? "-invisible" : "-headless"),
+	                       NULL);
 
 #endif
 
 	pipa = popen (command, "r");
 	g_free (command);
 
+	g_free (dash);
+
 	g_string_append_printf (str,
 	                        "context = resolver.resolve(\"uno:socket,host=localhost,port=%d;urp;StarOffice.ComponentContext\")\n"
 	                        "desktop = context.ServiceManager.createInstanceWithContext(\"com.sun.star.frame.Desktop\", context)\n",
 	                        priv->ooo_port);
 
-	gtk_widget_destroy (dialog);
+	if (priv->transient != NULL
+	    && GTK_IS_WINDOW (priv->transient))
+		{
+			gtk_widget_destroy (dialog);
+		}
 
 	if (g_chdir (curdir) == -1)
 		{
@@ -612,6 +612,14 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
 	gboolean in_esecuzione;
 	gchar *ooo_path;
 
+	gchar *dash;
+	guint part;
+	gchar *command;
+
+	guint timeout;
+
+	GError *error;
+
 	SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
 
 	/* apro python */
@@ -670,7 +678,9 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
 
 			pclose (pipa);
 
-			if (in_esecuzione)
+			if (in_esecuzione
+			    && priv->transient != NULL
+			    && GTK_IS_WINDOW (priv->transient))
 				{
 					dialog = gtk_message_dialog_new (priv->transient,
 					                                 GTK_DIALOG_DESTROY_WITH_PARENT,
@@ -686,21 +696,25 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
 					gtk_widget_destroy (dialog);
 				}
 
-			priv->dialog_response = FALSE;
-			dialog = gtk_message_dialog_new (priv->transient,
-			                                 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
-			                                 GTK_MESSAGE_INFO,
-			                                 GTK_BUTTONS_CANCEL,
-			                                 "Apertura e connessione a OpenOffice.org in corso...");
-			g_signal_connect (G_OBJECT (dialog), "response",
-			                  G_CALLBACK (dialog_response), ooo);
-			gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->transient);
-			gtk_window_set_deletable (GTK_WINDOW (dialog), FALSE);
-			gtk_widget_show_now (dialog);
-
-			while (gtk_events_pending ())
+			if (priv->transient != NULL
+			    && GTK_IS_WINDOW (priv->transient))
 				{
-					gtk_main_iteration ();
+					priv->dialog_response = FALSE;
+					dialog = gtk_message_dialog_new (priv->transient,
+					                                 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+					                                 GTK_MESSAGE_INFO,
+					                                 GTK_BUTTONS_CANCEL,
+					                                 "Apertura e connessione a OpenOffice.org in corso...");
+					g_signal_connect (G_OBJECT (dialog), "response",
+					                  G_CALLBACK (dialog_response), ooo);
+					gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->transient);
+					gtk_window_set_deletable (GTK_WINDOW (dialog), FALSE);
+					gtk_widget_show_now (dialog);
+
+					while (gtk_events_pending ())
+						{
+							gtk_main_iteration ();
+						}
 				}
 
 			if (in_esecuzione)
@@ -708,59 +722,110 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
 					solipa_ooo_chiudi_openoffice (ooo);
 				}
 
+			if (priv->ooo_version == NULL
+			    || zak_utils_compare_version (priv->ooo_version, "3.0.0", NULL, &part) > 0)
+				{
+					dash = g_strdup ("-");
+				}
+			else
+				{
+					dash = g_strdup ("");
+				}
+
 			/* apro openoffice in ascolto */
 #ifdef G_OS_WIN32
 
 			gchar *curdir = g_get_current_dir ();
 			chdir (g_build_filename (priv->ooo_path, "program", NULL));
 
-			pipa = popen (g_strconcat ("\"",
+			command = g_strconcat ("\"",
 			                           priv->ooo_soffice,
-			                           g_strdup_printf (" -accept=\"socket,host=localhost,port=%d;urp;\" -norestore %s\"", priv->ooo_port, priv->visible ? "-invisible" : "-headless"),
-			                           NULL),
-			              "r");
+			                           g_strdup_printf (" %s-accept=\"socket,host=localhost,port=%d;urp;\" %s-norestore %s%s\"",
+			                                            dash,
+			                                            priv->ooo_port,
+			                                            dash,
+			                                            dash,
+			                                            priv->visible ? "-invisible" : "-headless"),
+			                       NULL);
+
+			pipa = popen (command, "r");
 
 			chdir (curdir);
 			g_free (curdir);
 
 #else
 
-			pipa = popen (g_strconcat ("\"",
-			                           priv->ooo_soffice,
-			                           g_strdup_printf ("\" -accept=\"socket,host=localhost,port=%d;urp;\" -norestore %s", priv->ooo_port, priv->visible ? "-invisible" : "-headless"),
-			                           NULL),
-			              "r");
+			command = g_strconcat ("\"",
+			                       priv->ooo_soffice,
+			                       g_strdup_printf ("\" %s-accept=\"socket,host=localhost,port=%d;urp;\" %s-norestore %s%s",
+			                                        dash,
+			                                        priv->ooo_port,
+			                                        dash,
+			                                        dash,
+			                                        priv->visible ? "-invisible" : "-headless"),
+			                       NULL);
+			if (!g_spawn_command_line_async (command, NULL))
+				{
+					g_warning ("Unable to run soffice");
+				}
 
 #endif
 
-			while (gtk_events_pending ())
+			g_free (command);
+			g_free (dash);
+
+			if (priv->transient != NULL
+			    && GTK_IS_WINDOW (priv->transient))
 				{
-					gtk_main_iteration ();
+					while (gtk_events_pending ())
+						{
+							gtk_main_iteration ();
+						}
 				}
 
+			timeout = 0;
 			g_usleep (5000000);
 			do
 				{
 					PyRun_SimpleString (g_strdup_printf ("context = resolver.resolve(\"uno:socket,host=localhost,port=%d;urp;StarOffice.ComponentContext\")\n", priv->ooo_port));
 
-					while (gtk_events_pending ())
+					if (priv->transient != NULL
+					    && GTK_IS_WINDOW (priv->transient))
 						{
-							gtk_main_iteration ();
+							while (gtk_events_pending ())
+								{
+									gtk_main_iteration ();
+								}
 						}
 
 					obj = PyDict_GetItemString (priv->dict, "context");
 
-					if (obj != NULL || priv->dialog_response) break;
+					if (obj != NULL ||
+					    priv->dialog_response) break;
+
+					if (++timeout > 15
+					    && (priv->transient == NULL || !GTK_IS_WINDOW (priv->transient)))
+						{
+							break;
+						}
+
 					g_usleep (1000000);
 				} while (TRUE);
-			gtk_widget_destroy (dialog);
-			if (priv->dialog_response)
+
+			if (priv->transient != NULL
+			    && GTK_IS_WINDOW (priv->transient))
 				{
-					return FALSE;
+					gtk_widget_destroy (dialog);
+					if (priv->dialog_response)
+						{
+							return FALSE;
+						}
 				}
 
 			obj = PyDict_GetItemString (priv->dict, "context");
-			if (obj == NULL)
+			if (obj == NULL
+			    && priv->transient != NULL
+			    && GTK_IS_WINDOW (priv->transient))
 				{
 					dialog = gtk_message_dialog_new (priv->transient,
 					                                 GTK_DIALOG_DESTROY_WITH_PARENT,
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a39695f..3467a45 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -23,6 +23,7 @@ mailui_LDADD = $(top_builddir)/src/libsolipa.la \
                $(top_builddir)/src/libsolipamail.la
 
 ooo_LDADD = $(top_builddir)/src/libsolipaooo.la
+ooo_nox_LDADD = $(top_builddir)/src/libsolipaooo.la
 ooo_pycmd_LDADD = $(top_builddir)/src/libsolipaooo.la
 
 noinst_PROGRAMS = allegato \
@@ -32,6 +33,7 @@ noinst_PROGRAMS = allegato \
                   mail_get_addresses_from_string \
                   mailui \
                   ooo \
+                  ooo_nox \
                   ooo_pycmd \
                   progresswindow \
                   utils \
diff --git a/tests/ooo_nox.c b/tests/ooo_nox.c
new file mode 100644
index 0000000..0cbb0aa
--- /dev/null
+++ b/tests/ooo_nox.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2018 Andrea Zagli <azagli@libero.it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <ooo.h>
+
+SolipaOOO *ooo;
+
+int
+main (int argc, char *argv[])
+{
+	ooo = solipa_ooo_new (NULL,
+	                      NULL,
+	                      NULL,
+	                      0, FALSE);
+
+	if (ooo == NULL)
+		{
+			g_error ("Unable to create SolipaOOO.");
+		}
+
+	solipa_ooo_apri_documento (ooo, argv[1]);
+
+	solipa_ooo_string_replace (ooo, "{prg::replace}", "\"blah blah blah ààà ììì òòò ÈÈÈ ÀÀÀ ÙÙÙ\"");
+
+	if (argc >= 3)
+		{
+			solipa_ooo_salva_documento (ooo, argv[2]);
+		}
+	if (argc >= 4)
+		{
+			solipa_ooo_salva_documento_pdf (ooo, argv[3]);
+		}
+
+	if (argc >= 3)
+		{
+			solipa_ooo_chiudi_documento (ooo);
+		}
+
+	solipa_ooo_chiudi_openoffice (ooo);
+
+	g_object_unref (ooo);
+
+	return 0;
+}