]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
SolipaOOO: possibilità di utilizzare il python di *Office.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 18 Jun 2015 13:48:27 +0000 (15:48 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 18 Jun 2015 13:48:27 +0000 (15:48 +0200)
.cproject
src/ooo.c
src/ooo.h
tests/Makefile.am
tests/ooo_pycmd.c [new file with mode: 0644]

index 1fdb2eeea51cfef9bcc79403ae97d98c784a7fad..fb003d638db9f9c51e51ddb5a57a6ec444045a34 100644 (file)
--- a/.cproject
+++ b/.cproject
@@ -21,7 +21,7 @@
                                                        <builder id="org.eclipse.cdt.build.core.settings.default.builder.1848867928" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="org.eclipse.cdt.build.core.settings.default.builder"/>
                                                        <tool id="org.eclipse.cdt.build.core.settings.holder.libs.620886343" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
                                                        <tool id="org.eclipse.cdt.build.core.settings.holder.1473604265" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
-                                                               <option id="org.eclipse.cdt.build.core.settings.holder.incpaths.1589114181" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath"/>
+                                                               <option id="org.eclipse.cdt.build.core.settings.holder.incpaths.1589114181" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths"/>
                                                                <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.127941636" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
                                                        </tool>
                                                        <tool id="org.eclipse.cdt.build.core.settings.holder.724923490" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
@@ -35,6 +35,7 @@
                                                                        <listOptionValue builtIn="false" value="&quot;C:\msys64\mingw32\include\gtk-3.0&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;C:\msys64\mingw32\include\libgda-5.0&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;C:\msys64\mingw32\include\evolution-data-server&quot;"/>
+                                                                       <listOptionValue builtIn="false" value="&quot;C:\msys64\mingw32\include\python2.7&quot;"/>
                                                                </option>
                                                                <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1810670508" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
                                                        </tool>
index b9122cf34c1133f62d7097b7acd76c0ac33f918d..c4475cbb563ee9f0c7738ab834175a9921c67092 100644 (file)
--- a/src/ooo.c
+++ b/src/ooo.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2011-2015 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
@@ -39,6 +39,7 @@ static void solipa_ooo_get_property (GObject *object,
 static void solipa_ooo_dispose (GObject *gobject);
 static void solipa_ooo_finalize (GObject *gobject);
 
+static gboolean solipa_ooo_pycmd_apri_openoffice (SolipaOOO *ooo);
 static gboolean solipa_ooo_apri_openoffice (SolipaOOO *ooo);
 
 #define SOLIPA_OOO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), SOLIPA_OOO_TYPE, SolipaOOOPrivate))
@@ -59,10 +60,13 @@ struct _SolipaOOOPrivate
                PyObject *m_main;
                PyObject *dict;
                PyObject *search;
+               gboolean with_search;
        };
 
 G_DEFINE_TYPE (SolipaOOO, solipa_ooo, G_TYPE_OBJECT)
 
+#define ENCODING "# coding: utf-8\n"
+
 static void
 solipa_ooo_class_init (SolipaOOOClass *class)
 {
@@ -86,24 +90,16 @@ solipa_ooo_init (SolipaOOO *solipa_ooo)
        priv->search = NULL;
 
        priv->ooo_port = 8100;
+
+       priv->with_search = FALSE;
 }
 
-/**
- * solipa_ooo_new:
- * @transient:
- * @ooo_path:
- * @ooo_soffice:
- * @ooo_port:
- * @visible:
- *
- * Returns: the newly created #SolipaOOO object.
- */
-SolipaOOO
-*solipa_ooo_new (GtkWindow *transient,
-                 const gchar *ooo_path,
-                 const gchar *ooo_soffice,
-                 guint ooo_port,
-                 gboolean visible)
+static SolipaOOO
+*_solipa_ooo_new (GtkWindow *transient,
+                  const gchar *ooo_path,
+                  const gchar *ooo_soffice,
+                  guint ooo_port,
+                  gboolean visible)
 {
        SolipaOOO *solipa_ooo;
        SolipaOOOPrivate *priv;
@@ -134,25 +130,138 @@ SolipaOOO
                        priv->ooo_port = ooo_port;
                }
 
+       return solipa_ooo;
+}
+
+/**
+ * solipa_ooo_pycmd_new:
+ * @transient:
+ * @ooo_path:
+ * @ooo_soffice:
+ * @ooo_port:
+ * @visible:
+ *
+ * Returns: the newly created #SolipaOOO object.
+ */
+SolipaOOO
+*solipa_ooo_pycmd_new (GtkWindow *transient,
+                 const gchar *ooo_path,
+                 const gchar *ooo_soffice,
+                 guint ooo_port,
+                 gboolean visible)
+{
+       SolipaOOO *solipa_ooo;
+
+       solipa_ooo = _solipa_ooo_new (transient, ooo_path, ooo_soffice, ooo_port, visible);
+
+       solipa_ooo_pycmd_apri_openoffice (solipa_ooo);
+
+       return solipa_ooo;
+}
+
+/**
+ * solipa_ooo_new:
+ * @transient:
+ * @ooo_path:
+ * @ooo_soffice:
+ * @ooo_port:
+ * @visible:
+ *
+ * Returns: the newly created #SolipaOOO object.
+ */
+SolipaOOO
+*solipa_ooo_new (GtkWindow *transient,
+                 const gchar *ooo_path,
+                 const gchar *ooo_soffice,
+                 guint ooo_port,
+                 gboolean visible)
+{
+       SolipaOOO *solipa_ooo;
+
+       solipa_ooo = _solipa_ooo_new (transient, ooo_path, ooo_soffice, ooo_port, visible);
+
        solipa_ooo_apri_openoffice (solipa_ooo);
 
        return solipa_ooo;
 }
 
+/**
+ * solipa_ooo_exec:
+ * @ooo:
+ * @statement:
+ */
 void
 solipa_ooo_exec (SolipaOOO *ooo, const gchar *statement)
 {
        PyRun_SimpleString (statement);
 }
 
+/**
+ * solipa_ooo_pycmd_header:
+ * @ooo:
+ *
+ * Returns:
+ */
+gchar
+*solipa_ooo_pycmd_header (SolipaOOO *ooo)
+{
+       gchar *ret;
+
+       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
+
+       ret = g_strdup_printf (ENCODING "\n"
+                              "import uno\n"
+                              "import string\n"
+                              "from com.sun.star.beans import PropertyValue\n"
+                              "local = uno.getComponentContext()\n"
+                              "resolver = local.ServiceManager.createInstanceWithContext(\"com.sun.star.bridge.UnoUrlResolver\", local)\n"
+                               "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);
+
+       return ret;
+}
+
+/**
+ * solipa_ooo_pycmd_apri_documento:
+ * @ooo:
+ * @filename:
+ *
+ * Returns:
+ */
+gchar
+*solipa_ooo_pycmd_apri_documento (SolipaOOO *ooo, const gchar *filename)
+{
+       gchar *ret;
+
+       gchar *_filename;
+
+       _filename = g_strjoinv ("/", g_strsplit (filename, "\\", -1));
+       ret = g_strdup_printf ("document = desktop.loadComponentFromURL(\"file:///%s\", \"_default\", 0, ())\n", _filename);
+
+       g_free (_filename);
+
+       return ret;
+}
+
+/**
+ * solipa_ooo_apri_documento:
+ * @ooo:
+ * @filename:
+ *
+ * Returns:
+ */
 gboolean
 solipa_ooo_apri_documento (SolipaOOO *ooo, const gchar *filename)
 {
+       gchar *str;
        PyObject *obj;
 
        SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
 
-       PyRun_SimpleString (g_strdup_printf ("document = desktop.loadComponentFromURL(\"file:///%s\", \"_default\", 0, ())\n", filename));
+       str = solipa_ooo_pycmd_apri_documento (ooo, filename);
+       PyRun_SimpleString (str);
+       g_free (str);
 
        /* ritorna TRUE/FALSE se document è stato creato o meno */
        obj = PyDict_GetItemString (priv->dict, "document");
@@ -167,13 +276,21 @@ solipa_ooo_apri_documento (SolipaOOO *ooo, const gchar *filename)
                }
 }
 
-void
-solipa_ooo_string_replace (SolipaOOO *ooo, const gchar *search, const gchar *replace)
+/**
+ * solipa_ooo_pycmd_string_replace:
+ * @ooo:
+ * @search:
+ * @replace:
+ *
+ * Returns:
+ */
+gchar
+*solipa_ooo_pycmd_string_replace (SolipaOOO *ooo, const gchar *search, const gchar *replace)
 {
        gchar *_search;
        gchar *_replace;
 
-       PyObject *obj;
+       gchar *ret;
 
        SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
 
@@ -183,13 +300,47 @@ solipa_ooo_string_replace (SolipaOOO *ooo, const gchar *search, const gchar *rep
        _search = g_strjoinv ("\\\"", g_strsplit (_search, "\"", -1));
        _replace = g_strjoinv ("\\\"", g_strsplit (_replace, "\"", -1));
 
+       ret = g_strdup_printf ("%s"
+                              "search.SearchString = u\"%s\"\n"
+                              "found = document.findFirst(search)\n"
+                              "found.String = str.replace(found.String, u\"%s\", \"%s\")\n",
+                              !priv->with_search ? "search = document.createSearchDescriptor()\n" : "",
+                              _search,
+                              _search,
+                              _replace);
+       priv->with_search = TRUE;
+
+       g_free (_search);
+       g_free (_replace);
+
+       return ret;
+}
+
+/**
+ * solipa_ooo_string_replace:
+ * @ooo:
+ * @search:
+ * @replace:
+ *
+ * Returns:
+ */
+void
+solipa_ooo_string_replace (SolipaOOO *ooo, const gchar *search, const gchar *replace)
+{
+       gchar *str;
+
+       PyObject *obj;
+
+       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
+
+       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)
                                {
-                                       PyRun_SimpleString ("search = document.createSearchDescriptor()\n");
                                        priv->search = PyDict_GetItemString (priv->dict, "search");
                                        if (priv->search == NULL)
                                                {
@@ -199,13 +350,8 @@ solipa_ooo_string_replace (SolipaOOO *ooo, const gchar *search, const gchar *rep
                                }
                }
 
-       PyRun_SimpleString (g_strdup_printf (
-                           "search.SearchString = u\"%s\"\n"
-                           "found = document.findFirst(search)\n"
-                           "found.String = string.replace(found.String, u\"%s\", \"%s\".decode(\"utf-8\"))\n",
-                           _search,
-                           _search,
-                           _replace));
+       PyRun_SimpleString (str);
+       g_free (str);
 }
 
 /* PRIVATE */
@@ -288,6 +434,120 @@ dialog_response (GtkDialog *dialog,
        priv->dialog_response = TRUE;
 }
 
+static gboolean
+solipa_ooo_pycmd_apri_openoffice (SolipaOOO *ooo)
+{
+       GtkWidget *dialog;
+
+       FILE *pipa;
+
+       gboolean in_esecuzione;
+
+       GString *str;
+
+       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
+
+       gchar *curdir;
+
+       curdir = g_get_current_dir ();
+       chdir (priv->ooo_path);
+
+       str = g_string_new (ENCODING "\n");
+
+       /* TODO
+        * controllo se OpenOffice è già in ascolto */
+
+       g_string_append (str, "import uno\n"
+                           "import string\n"
+                           "from com.sun.star.beans import PropertyValue\n"
+                           "local = uno.getComponentContext()\n"
+                           "resolver = local.ServiceManager.createInstanceWithContext(\"com.sun.star.bridge.UnoUrlResolver\", local)\n");
+
+       /* controllo se OpenOffice è già in esecuzione */
+       in_esecuzione = FALSE;
+
+       /* TODO parametrizzare il nome del processo da cercare */
+#ifdef G_OS_WIN32
+       pipa = popen (g_strdup_printf ("tasklist | findstr /i soffice.bin"), "r");
+#else
+       pipa = popen (g_strdup_printf ("pidof soffice.bin"), "r");
+#endif
+
+       if (pipa != NULL)
+               {
+                       in_esecuzione = (fgetc (pipa) != EOF);
+               }
+
+       pclose (pipa);
+
+       if (in_esecuzione)
+               {
+                       dialog = gtk_message_dialog_new (priv->transient,
+                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                        GTK_MESSAGE_INFO,
+                                                        GTK_BUTTONS_OK_CANCEL,
+                                                        "Verranno chiuse tutte le sessioni attive di OpenOffice/LibreOffice.\n"
+                                                        "Premere OK quando pronti.");
+                       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_CANCEL)
+                               {
+                                       gtk_widget_destroy (dialog);
+                                       return FALSE;
+                               }
+                       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 ())
+               {
+                       gtk_main_iteration ();
+               }
+
+       if (in_esecuzione)
+               {
+                       solipa_ooo_chiudi_openoffice (ooo);
+               }
+
+       /* apro openoffice in ascolto */
+#ifdef G_OS_WIN32
+
+       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");
+
+#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");
+
+#endif
+
+       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);
+
+       chdir (curdir);
+       g_free (curdir);
+}
+
 static gboolean
 solipa_ooo_apri_openoffice (SolipaOOO *ooo)
 {
@@ -307,7 +567,7 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
        priv->m_main = PyImport_AddModule ("__main__");
        priv->dict = PyModule_GetDict (priv->m_main);
 
-       PyRun_SimpleString ("# coding: utf-8\n");
+       PyRun_SimpleString (ENCODING);
 
 #ifdef G_OS_WIN32
 
@@ -477,16 +737,13 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
                }
 }
 
-gboolean
-solipa_ooo_salva_documento (SolipaOOO *ooo,
-                            const gchar *filename)
+gchar
+*solipa_ooo_pycmd_salva_documento (SolipaOOO *ooo,
+                                   const gchar *filename)
 {
-       PyObject *obj;
-
+       gchar *ret;
        gchar *_filename;
 
-       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
-
        _filename = NULL;
        if (filename != NULL)
                {
@@ -497,28 +754,47 @@ solipa_ooo_salva_documento (SolipaOOO *ooo,
        if (_filename == NULL
            || g_strcmp0 (_filename, "") == 0)
                {
-                       PyRun_SimpleString ("document.store()\n");
+                       ret = g_strdup ("document.store()\n");
                }
        else
                {
-                       PyRun_SimpleString (g_strdup_printf ("document.storeAsURL(\"file://%s\", ())\n", _filename));
+                       _filename = g_strjoinv ("/", g_strsplit (_filename, "\\", -1));
+                       ret = g_strdup_printf ("document.storeAsURL(\"file:///%s\", ())\n", _filename);
                }
+       if (_filename != NULL)
+               {
+                       g_free (_filename);
+               }
+
+       return ret;
+}
+
+gboolean
+solipa_ooo_salva_documento (SolipaOOO *ooo,
+                            const gchar *filename)
+{
+       gchar *str;
+       PyObject *obj;
+
+       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
+
+       str = solipa_ooo_pycmd_salva_documento (ooo, filename);
+       PyRun_SimpleString (str);
+       g_free (str);
 
        /* TODO capire se il documento è stato salvato o meno */
 
        return TRUE;
 }
 
-gboolean
-solipa_ooo_salva_documento_pdf (SolipaOOO *ooo,
-                                const gchar *filename)
+gchar
+*solipa_ooo_pycmd_salva_documento_pdf (SolipaOOO *ooo,
+                                       const gchar *filename)
 {
-       PyObject *obj;
+       gchar *ret;
 
        gchar *_filename;
 
-       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
-
        _filename = NULL;
        if (filename != NULL)
                {
@@ -526,20 +802,54 @@ solipa_ooo_salva_documento_pdf (SolipaOOO *ooo,
                        g_strstrip (_filename);
                }
 
-       g_return_val_if_fail (_filename != NULL && g_strcmp0 (_filename, "") != 0, FALSE);
+       g_return_val_if_fail (_filename != NULL && g_strcmp0 (_filename, "") != 0, "");
+
+       _filename = g_strjoinv ("/", g_strsplit (_filename, "\\", -1));
+       ret = g_strdup_printf ("property = (PropertyValue (\"FilterName\", 0, \"writer_pdf_Export\", 0), )\n"
+                              "document.storeToURL(\"file:///%s\", property)\n",
+                              _filename);
+       g_free (_filename);
+
+       return ret;
+}
+
+gboolean
+solipa_ooo_salva_documento_pdf (SolipaOOO *ooo,
+                                const gchar *filename)
+{
+       gchar *str;
+       PyObject *obj;
+
+       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
 
-       PyRun_SimpleString ("property = (PropertyValue (\"FilterName\", 0, \"writer_pdf_Export\", 0), )\n");
-       PyRun_SimpleString (g_strdup_printf ("document.storeToURL(\"file://%s\", property)\n", _filename));
+       str = solipa_ooo_pycmd_salva_documento_pdf (ooo, filename);
+
+       PyRun_SimpleString (str);
+       g_free (str);
 
        /* TODO capire se il documento è stato salvato o meno */
 
        return TRUE;
 }
 
+gchar
+*solipa_ooo_pycmd_chiudi_documento (SolipaOOO *ooo)
+{
+       gchar *ret;
+
+       ret = g_strdup ("document.dispose()\n");
+
+       return ret;
+}
+
 void
 solipa_ooo_chiudi_documento (SolipaOOO *ooo)
 {
-       solipa_ooo_exec (ooo, "document.dispose()\n");
+       gchar *str;
+
+       str = solipa_ooo_pycmd_chiudi_documento (ooo);
+       solipa_ooo_exec (ooo, str);
+       g_free (str);
 }
 
 void
@@ -556,3 +866,78 @@ solipa_ooo_chiudi_openoffice (SolipaOOO *ooo)
 #endif
        pclose (pipa);
 }
+
+/**
+ * solipa_ooo_pycmd_exec_script:
+ * @ooo:
+ * @body:
+ *
+ */
+void
+solipa_ooo_pycmd_exec_script (SolipaOOO *ooo, const gchar *body)
+{
+       FILE *pipa;
+
+       GString *str;
+
+       GFile *gfile;
+       GFileIOStream *stream;
+       GOutputStream *ostream;
+       GError *error;
+
+       gchar *tmp_path;
+
+       SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
+
+       str = g_string_new (solipa_ooo_pycmd_header (ooo));
+
+       g_string_append_printf (str, body);
+
+       /* creo il file temporaneo da eseguire */
+       stream = NULL;
+       error = NULL;
+       gfile = g_file_new_tmp ("ooo-XXXXXX.py",
+                               &stream,
+                               &error);
+       if (gfile == NULL
+           || error != NULL)
+               {
+                       g_warning ("Error on opening temp python file: %s",
+                                  error != NULL && error->message != NULL ? error->message : "no details");
+                       return;
+               }
+
+       ostream = g_io_stream_get_output_stream (G_IO_STREAM (stream));
+
+       error = NULL;
+       g_output_stream_write (ostream,
+                              str->str,
+                              str->len,
+                              NULL,
+                              &error);
+
+       error = NULL;
+       g_output_stream_close (ostream, NULL, &error);
+
+       gchar *curdir = g_get_current_dir ();
+       chdir (priv->ooo_path);
+
+       tmp_path = g_file_get_path (gfile);
+       g_string_printf (str, "python.exe %s", tmp_path);
+       g_free (tmp_path);
+
+       pipa = popen (str->str, "r");
+
+       pclose (pipa);
+
+       chdir (curdir);
+       g_free (curdir);
+
+       /* elimino il file */
+       error = NULL;
+       g_file_delete (gfile, NULL, &error);
+
+       g_object_unref (gfile);
+
+       g_string_free (str, TRUE);
+}
index 4cd2811ccd10763b7b529e6bcc9901ae59202f81..44d799d13464842cb63575f2d25c6ebe765abde2 100644 (file)
--- a/src/ooo.h
+++ b/src/ooo.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2011-2015 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
@@ -51,6 +51,12 @@ struct _SolipaOOOClass
 GType solipa_ooo_get_type (void) G_GNUC_CONST;
 
 
+SolipaOOO *solipa_ooo_pycmd_new (GtkWindow *transient,
+                                 const gchar *ooo_path,
+                                 const gchar *ooo_soffice,
+                                 guint ooo_port,
+                                 gboolean visible);
+
 SolipaOOO *solipa_ooo_new (GtkWindow *transient,
                            const gchar *ooo_path,
                            const gchar *ooo_soffice,
@@ -59,23 +65,42 @@ SolipaOOO *solipa_ooo_new (GtkWindow *transient,
 
 void solipa_ooo_exec (SolipaOOO *ooo, const gchar *statement);
 
+gchar *solipa_ooo_pycmd_header (SolipaOOO *ooo);
+
+gchar *solipa_ooo_pycmd_apri_documento (SolipaOOO *ooo,
+                                        const gchar *filename);
+
 gboolean solipa_ooo_apri_documento (SolipaOOO *ooo,
                                     const gchar *filename);
 
+gchar *solipa_ooo_pycmd_string_replace (SolipaOOO *ooo,
+                                        const gchar *search,
+                                        const gchar *replace);
+
 void solipa_ooo_string_replace (SolipaOOO *ooo,
                                 const gchar *search,
                                 const gchar *replace);
 
+gchar *solipa_ooo_pycmd_salva_documento (SolipaOOO *ooo,
+                                         const gchar *filename);
+
 gboolean solipa_ooo_salva_documento (SolipaOOO *ooo,
                                      const gchar *filename);
 
+gchar *solipa_ooo_pycmd_salva_documento_pdf (SolipaOOO *ooo,
+                                             const gchar *filename);
+
 gboolean solipa_ooo_salva_documento_pdf (SolipaOOO *ooo,
                                          const gchar *filename);
 
+gchar *solipa_ooo_pycmd_chiudi_documento (SolipaOOO *ooo);
+
 void solipa_ooo_chiudi_documento (SolipaOOO *ooo);
 
 void solipa_ooo_chiudi_openoffice (SolipaOOO *ooo);
 
+void solipa_ooo_pycmd_exec_script (SolipaOOO *ooo, const gchar *body);
+
 
 G_END_DECLS
 
index 7fa5182072ae98c040f0e03d58430007e0129d8b..320b06142b59fc8aa66c290fb75309f12fcb978f 100644 (file)
@@ -12,6 +12,7 @@ LIBS = $(SOLIPA_LIBS) \
 LDADD = $(top_builddir)/src/libsolipa.la
 
 ooo_LDADD = $(top_builddir)/src/libsolipaooo.la
+ooo_pycmd_LDADD = $(top_builddir)/src/libsolipaooo.la
 
 noinst_PROGRAMS = allegato \
                   log \
@@ -20,6 +21,7 @@ noinst_PROGRAMS = allegato \
                   mail_get_addresses_from_string \
                   mailui \
                   ooo \
+                  ooo_pycmd \
                   progresswindow \
                   utils \
                   utils_apri_collegamento \
diff --git a/tests/ooo_pycmd.c b/tests/ooo_pycmd.c
new file mode 100644 (file)
index 0000000..502ad27
--- /dev/null
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2015 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>
+
+GtkWidget *w;
+
+SolipaOOO *ooo;
+
+gboolean
+on_w_delete_event (GtkWidget *widget,
+                   GdkEvent *event,
+                   gpointer user_data)
+{
+       return FALSE;
+}
+
+int
+main (int argc, char *argv[])
+{
+       GString *str;
+       gchar *strtmp;
+
+       GError *error;
+       
+       gchar *python_path;
+       gchar *model;
+       gchar *new_file;
+       gchar *new_file_pdf;
+       
+       GOptionEntry entries[] = 
+       {
+               { "python_path", 'y', 0, G_OPTION_ARG_STRING, &python_path, "Python path", NULL },
+               { "model", 'm', 0, G_OPTION_ARG_STRING, &model, "OpenDocument model", NULL },
+               { "new", 'n', 0, G_OPTION_ARG_STRING, &new_file, "Save as", NULL },
+               { "new_pdf", 'p', 0, G_OPTION_ARG_STRING, &new_file_pdf, "Export to pdf", NULL },
+               { NULL }
+       };
+
+       GOptionContext *context;
+
+       gtk_init (&argc, &argv);
+
+       python_path = NULL;
+       model = NULL;
+       new_file = NULL;
+       new_file_pdf = NULL;
+       
+       /* gestione degli argomenti della riga di comando */
+       context = g_option_context_new ("");
+       g_option_context_add_main_entries (context, entries, NULL);
+       error = NULL;
+       if (!g_option_context_parse (context, &argc, &argv, &error))
+               {
+                       /* TO DO */
+                       g_error ("Errore argomenti riga comando.\n");
+                       return 0;
+               }
+
+       if (python_path == NULL || g_strcmp0 (g_strstrip (python_path), "") == 0)
+               {
+                       g_error ("Occorre specificare il path del python di openoffice/libreoffice.\n");
+                       return 0;
+               }
+       if (model == NULL || g_strcmp0 (g_strstrip (model), "") == 0)
+               {
+                       g_error ("Occorre specificare il modello di openoffice/libreoffice da aprire.\n");
+                       return 0;
+               }
+       if (new_file == NULL || g_strcmp0 (g_strstrip (new_file), "") == 0)
+               {
+                       g_error ("Occorre specificare il path del nuovo file.\n");
+                       return 0;
+               }
+       if (new_file_pdf == NULL || g_strcmp0 (g_strstrip (new_file_pdf), "") == 0)
+               {
+                       g_error ("Occorre specificare il path del nuovo file pdf.\n");
+                       return 0;
+               }
+
+       w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       g_signal_connect (w, "delete-event", G_CALLBACK (on_w_delete_event), NULL);
+       g_signal_connect (w, "destroy", gtk_main_quit, NULL);
+
+       gtk_widget_show (w);
+
+       ooo = solipa_ooo_pycmd_new (GTK_WINDOW (w),
+                             python_path,
+                             NULL,
+                             0,
+                             TRUE);
+
+       if (ooo == NULL)
+               {
+                       g_error ("Unable to create SolipaOOO.");
+               }
+
+       str = g_string_new ("");
+
+       strtmp = solipa_ooo_pycmd_apri_documento (ooo, model);
+       g_string_append (str, strtmp);
+       g_free (strtmp);
+
+       strtmp = solipa_ooo_pycmd_string_replace (ooo, "{prg::replace}", "\"blah blah blah ààà ììì òòò ÈÈÈ ÀÀÀ ÙÙÙ\"");
+       g_string_append (str, strtmp);
+       g_free (strtmp);
+
+       strtmp = solipa_ooo_pycmd_salva_documento (ooo, new_file);
+       g_string_append (str, strtmp);
+       g_free (strtmp);
+
+       strtmp = solipa_ooo_pycmd_salva_documento_pdf (ooo, new_file_pdf);
+       g_string_append (str, strtmp);
+       g_free (strtmp);
+
+       strtmp = solipa_ooo_pycmd_chiudi_documento (ooo);
+       g_string_append (str, strtmp);
+       g_free (strtmp);
+       
+       solipa_ooo_pycmd_exec_script (ooo, str->str);
+       g_message("%s", str->str);
+       g_string_free (str, TRUE);
+
+       GtkWidget *lbl = gtk_label_new ("FINE");
+       gtk_container_add (GTK_CONTAINER (w), lbl);
+       gtk_widget_show_all (w);
+
+       gtk_main ();
+
+       solipa_ooo_chiudi_openoffice (ooo);
+
+       g_object_unref (ooo);
+
+       return 0;
+}