#include <config.h>
#endif
+#include <Python.h>
+
#include "ooo.h"
static void solipa_ooo_class_init (SolipaOOOClass *class);
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)
/**
* 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,
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;
+}