From: Andrea Zagli Date: Mon, 18 Jul 2011 07:24:24 +0000 (+0200) Subject: Aggiunti i parametri al costruttore. X-Git-Tag: 0.5.0~49 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=52fa0081daac6fa15601778885488f3a43dc12f3;p=solipa%2Flibsolipa Aggiunti i parametri al costruttore. Aggiunte le funzioni SolipaOOO::apri_documento e SolipaOOO::string_replace. --- diff --git a/src/ooo.c b/src/ooo.c index 83d75aa..2379a6f 100644 --- a/src/ooo.c +++ b/src/ooo.c @@ -20,6 +20,8 @@ #include #endif +#include + #include "ooo.h" static void solipa_ooo_class_init (SolipaOOOClass *class); @@ -37,12 +39,19 @@ 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_apri_openoffice (SolipaOOO *ooo); + #define SOLIPA_OOO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), SOLIPA_OOO_TYPE, SolipaOOOPrivate)) typedef struct _SolipaOOOPrivate SolipaOOOPrivate; struct _SolipaOOOPrivate { - gpointer foo; + GtkWindow *transient; + + gchar *ooo_path; + gchar *ooo_soffice; + + gboolean dialog_response; }; G_DEFINE_TYPE (SolipaOOO, solipa_ooo, G_TYPE_OBJECT) @@ -68,22 +77,58 @@ solipa_ooo_init (SolipaOOO *solipa_ooo) /** * solipa_ooo_new: + * @transient: + * @ooo_path: + * @ooo_soffice: * * Returns: the newly created #SolipaOOO object. */ SolipaOOO -*solipa_ooo_new () +*solipa_ooo_new (GtkWindow *transient, + const gchar *ooo_path, + const gchar *ooo_soffice) { SolipaOOO *solipa_ooo; SolipaOOOPrivate *priv; + g_return_val_if_fail (GTK_IS_WINDOW (transient), NULL); + solipa_ooo = SOLIPA_OOO (g_object_new (solipa_ooo_get_type (), NULL)); priv = SOLIPA_OOO_GET_PRIVATE (solipa_ooo); + priv->transient = transient; + + /* TODO controllare i valori */ + priv->ooo_path = g_strdup (ooo_path); + priv->ooo_soffice = g_strdup (ooo_soffice); + + solipa_ooo_apri_openoffice (solipa_ooo); + return solipa_ooo; } +gboolean +solipa_ooo_apri_documento (SolipaOOO *ooo, const gchar *filename) +{ + PyRun_SimpleString (g_strdup_printf ("document = desktop.loadComponentFromURL(\"file:///%s\", \"_default\", 0, ())\n", filename)); + /* TODO ritornare TRUE/FALSE se document è stato creato o meno */ + return TRUE; +} + +void +solipa_ooo_string_replace (SolipaOOO *ooo, const gchar *search, const gchar *replace) +{ + /* TODO controllare se SearchDescriptor esiste o no */ + PyRun_SimpleString (g_strdup_printf ("search = document.createSearchDescriptor()\n" + "search.SearchString = u\"%s\"\n" + "found = document.findFirst(search)\n" + "found.String = string.replace(found.String, u\"%s\", u\"%s\")\n", + search, + search, + replace)); +} + /* PRIVATE */ static void solipa_ooo_set_property (GObject *object, @@ -141,8 +186,164 @@ solipa_ooo_finalize (GObject *gobject) SolipaOOO *solipa_ooo = (SolipaOOO *)gobject; SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (solipa_ooo); - /*g_free (priv->camel_tmpdir);*/ + g_free (priv->ooo_path); + g_free (priv->ooo_soffice); GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); parent_class->finalize (gobject); } + +static void +dialog_response (GtkDialog *dialog, + gint response_id, + gpointer user_data) +{ + SolipaOOO *ooo = (SolipaOOO *)user_data; + SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo); + + priv->dialog_response = TRUE; +} + +static gboolean +solipa_ooo_apri_openoffice (SolipaOOO *ooo) +{ + GtkWidget *dialog; + + FILE *pipa; + + PyObject *m_main; + PyObject *dict; + PyObject *obj; + + gchar *ooo_path; + + SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo); + + /* TODO controllare se OpenOffice è già in ascolto */ + + 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.org.\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 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 (); + } + + /* chiudo le eventuali sessioni di openoffice attive */ +#ifdef G_OS_WIN32 + pipa = popen ("taskkill /T /F /IM soffice.bin", "r"); +#else + pipa = popen ("killall soffice.bin", "r"); +#endif + pclose (pipa); + + /* apro openoffice in ascolto */ + gchar *curdir = g_get_current_dir (); + chdir (g_build_filename (priv->ooo_path, "program", NULL)); + +#ifdef G_OS_WIN32 + pipa = popen (g_strconcat ("\"", + priv->ooo_soffice, + " -accept=\"socket,host=localhost,port=8100;urp;\" -no-restore -invisible\"", + NULL), + "r"); +#else + pipa = popen (g_strconcat ("\"", + priv->ooo_soffice, + "\" -accept=\"socket,host=localhost,port=8100;urp;\" -no-restore -invisible", + NULL), + "r"); +#endif + + chdir (curdir); + g_free (curdir); + + while (gtk_events_pending ()) + { + gtk_main_iteration (); + } + + /* apro python */ + Py_Initialize (); + m_main = PyImport_AddModule ("__main__"); + dict = PyModule_GetDict (m_main); + + ooo_path = g_strjoinv ("\\\\", g_strsplit (priv->ooo_path, "\\", -1)); + PyRun_SimpleString (g_strdup_printf ( + "import os\n" + "import sys\n" + "os.environ['PATH'] = os.getenv('PATH') + ';%s\\\\URE\\\\bin'\n" + "os.environ['PYTHONPATH'] = '%s\\\\Basis\\\\Program'\n" + "sys.path.append('%s\\\\Basis\\\\Program')\n" + "os.environ['URE_BOOTSTRAP'] = 'file:///%s/program/fundamental.ini'\n", + ooo_path, + ooo_path, + ooo_path, + g_strjoinv ("%20", g_strsplit (g_strjoinv ("/", g_strsplit (priv->ooo_path, "\\", -1)), " ", -1))) + ); + PyRun_SimpleString ("import uno\n" + "import string\n" + "local = uno.getComponentContext()\n" + "resolver = local.ServiceManager.createInstanceWithContext(\"com.sun.star.bridge.UnoUrlResolver\", local)\n" + ); + + while (gtk_events_pending ()) + { + gtk_main_iteration (); + } + + do + { + PyRun_SimpleString ("context = resolver.resolve(\"uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext\")\n"); + + while (gtk_events_pending ()) + { + gtk_main_iteration (); + } + + obj = PyDict_GetItemString (dict, "context"); + + if (obj != NULL || priv->dialog_response) break; + } while (TRUE); + gtk_widget_destroy (dialog); + if (priv->dialog_response) return FALSE; + + obj = PyDict_GetItemString (dict, "context"); + if (obj == NULL) + { + dialog = gtk_message_dialog_new (priv->transient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Errore nella connessione a OpenOffice.org."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return FALSE; + } + + PyRun_SimpleString ("desktop = context.ServiceManager.createInstanceWithContext(\"com.sun.star.frame.Desktop\", context)\n"); + /* TODO ritornare TRUE/FALSE se desktop è stato creato o meno */ + return TRUE; +} diff --git a/src/ooo.h b/src/ooo.h index 63ff517..556fa5a 100644 --- a/src/ooo.h +++ b/src/ooo.h @@ -55,7 +55,15 @@ struct _SolipaOOOClass GType solipa_ooo_get_type (void) G_GNUC_CONST; -SolipaOOO *solipa_ooo_new (void); +SolipaOOO *solipa_ooo_new (GtkWindow *transient, + const gchar *ooo_path, + const gchar *ooo_soffice); + +gboolean solipa_ooo_apri_documento (SolipaOOO *ooo, + const gchar *filename); +void solipa_ooo_string_replace (SolipaOOO *ooo, + const gchar *search, + const gchar *replace); G_END_DECLS