]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Controllo se OpenOffice è già in esecuzione.
authorAndrea Zagli <azagli@libero.it>
Sat, 23 Jul 2011 06:48:49 +0000 (08:48 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 23 Jul 2011 06:48:49 +0000 (08:48 +0200)
Aggiunto il parametro porta sulla quale cercare/mettere in ascolto
OpenOffice.org.

src/ooo.c
src/ooo.h
tests/ooo.c

index 134e44ae240deff5ab20c57ddb31479f42664441..32b109874da2ddf66a231367d3ecb25e9c2f8202 100644 (file)
--- a/src/ooo.c
+++ b/src/ooo.c
@@ -50,6 +50,7 @@ struct _SolipaOOOPrivate
 
                gchar *ooo_path;
                gchar *ooo_soffice;
+               guint ooo_port;
 
                gboolean dialog_response;
        };
@@ -73,6 +74,8 @@ static void
 solipa_ooo_init (SolipaOOO *solipa_ooo)
 {
        SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (solipa_ooo);
+
+       priv->ooo_port = 8100;
 }
 
 /**
@@ -80,13 +83,15 @@ solipa_ooo_init (SolipaOOO *solipa_ooo)
  * @transient:
  * @ooo_path:
  * @ooo_soffice:
+ * @ooo_port:
  *
  * Returns: the newly created #SolipaOOO object.
  */
 SolipaOOO
 *solipa_ooo_new (GtkWindow *transient,
                  const gchar *ooo_path,
-                 const gchar *ooo_soffice)
+                 const gchar *ooo_soffice,
+                 guint ooo_port)
 {
        SolipaOOO *solipa_ooo;
        SolipaOOOPrivate *priv;
@@ -111,6 +116,11 @@ SolipaOOO
                        priv->ooo_soffice = g_strdup (ooo_soffice);
                }
 
+       if (ooo_port > 0)
+               {
+                       priv->ooo_port = ooo_port;
+               }
+
        solipa_ooo_apri_openoffice (solipa_ooo);
 
        return solipa_ooo;
@@ -246,78 +256,6 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
 
        SolipaOOOPrivate *priv = SOLIPA_OOO_GET_PRIVATE (ooo);
 
-       /* TODO controllare se OpenOffice è già in ascolto */
-
-       /* TODO controllare se OpenOffice è già 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.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 (g_strdup_printf ("taskkill /T /F /IM %s.bin", priv->ooo_soffice), "r");
-#else
-       pipa = popen (g_strdup_printf ("killall %s.bin", priv->ooo_soffice), "r");
-#endif
-       pclose (pipa);
-
-       /* 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 ("\"",
-                                  priv->ooo_soffice,
-                                  " -accept=\"socket,host=localhost,port=8100;urp;\" -no-restore -invisible\"",
-                                  NULL),
-                     "r");
-
-       chdir (curdir);
-       g_free (curdir);
-
-#else
-
-       pipa = popen (g_strconcat ("\"",
-                                  priv->ooo_soffice,
-                                  "\" -accept=\"socket,host=localhost,port=8100;urp;\" -no-restore -invisible",
-                                  NULL),
-                     "r");
-
-#endif
-
-       while (gtk_events_pending ())
-               {
-                       gtk_main_iteration ();
-               }
-
        /* apro python */
        Py_Initialize ();
        m_main = PyImport_AddModule ("__main__");
@@ -347,48 +285,130 @@ solipa_ooo_apri_openoffice (SolipaOOO *ooo)
                "resolver = local.ServiceManager.createInstanceWithContext(\"com.sun.star.bridge.UnoUrlResolver\", local)\n"
        );
 
-       while (gtk_events_pending ())
-               {
-                       gtk_main_iteration ();
-               }
+       /* TODO controllare se OpenOffice è già in ascolto */
+       PyRun_SimpleString (g_strdup_printf ("context = resolver.resolve(\"uno:socket,host=localhost,port=%d;urp;StarOffice.ComponentContext\")\n", priv->ooo_port));
 
-       g_usleep (5000000);
-       do
+       obj = PyDict_GetItemString (dict, "context");
+
+       if (obj == NULL)
                {
-                       PyRun_SimpleString ("context = resolver.resolve(\"uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext\")\n");
+                       /* TODO controllare se OpenOffice è già 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.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 ();
                                }
 
-                       obj = PyDict_GetItemString (dict, "context");
+                       /* chiudo le eventuali sessioni di openoffice attive */
+#ifdef G_OS_WIN32
+                       pipa = popen (g_strdup_printf ("taskkill /T /F /IM %s.bin", priv->ooo_soffice), "r");
+#else
+                       pipa = popen (g_strdup_printf ("killall %s.bin", priv->ooo_soffice), "r");
+#endif
+                       pclose (pipa);
 
-                       if (obj != NULL || priv->dialog_response) break;
-                       g_usleep (1000000);
-               } while (TRUE);
-       gtk_widget_destroy (dialog);
-       if (priv->dialog_response)
-               {
-                       return FALSE;
-               }
+                       /* apro openoffice in ascolto */
+#ifdef G_OS_WIN32
 
-       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));
+                       gchar *curdir = g_get_current_dir ();
+                       chdir (g_build_filename (priv->ooo_path, "program", NULL));
+
+                       pipa = popen (g_strconcat ("\"",
+                                                  priv->ooo_soffice,
+                                                  g_strdup_printf (" -accept=\"socket,host=localhost,port=%d;urp;\" -no-restore -invisible\"", priv->ooo_port),
+                                                  NULL),
+                                     "r");
+
+                       chdir (curdir);
+                       g_free (curdir);
+
+#else
+
+                       pipa = popen (g_strconcat ("\"",
+                                                  priv->ooo_soffice,
+                                                  g_strdup_printf ("\" -accept=\"socket,host=localhost,port=%d;urp;\" -no-restore -invisible", priv->ooo_port),
+                                                  NULL),
+                                     "r");
+
+#endif
+
+                       while (gtk_events_pending ())
+                               {
+                                       gtk_main_iteration ();
+                               }
+
+                       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 ())
+                                               {
+                                                       gtk_main_iteration ();
+                                               }
+
+                                       obj = PyDict_GetItemString (dict, "context");
+
+                                       if (obj != NULL || priv->dialog_response) break;
+                                       g_usleep (1000000);
+                               } while (TRUE);
                        gtk_widget_destroy (dialog);
-                       return FALSE;
+                       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 */
+       /* ritorno TRUE/FALSE se desktop è stato creato o meno */
+       obj = PyDict_GetItemString (dict, "desktop");
 
-       return TRUE;
+       if (obj == NULL)
+               {
+                       g_warning ("Errore nella creazione dell'oggetto «desktop» di OpenOffice.org.");
+                       return FALSE;
+               }
+       else
+               {
+                       return TRUE;
+               }
 }
index 5b7841951081602d198ee95747eaafe667193a12..30d1f0560a259e43c91ca950384371c2897947b9 100644 (file)
--- a/src/ooo.h
+++ b/src/ooo.h
@@ -57,7 +57,8 @@ GType solipa_ooo_get_type (void) G_GNUC_CONST;
 
 SolipaOOO *solipa_ooo_new (GtkWindow *transient,
                            const gchar *ooo_path,
-                           const gchar *ooo_soffice);
+                           const gchar *ooo_soffice,
+                           guint ooo_port);
 
 void solipa_ooo_exec (SolipaOOO *ooo, const gchar *statement);
 
index 4c78d359f00bd2267a8ccf6f1a9361827fb7696e..17de5815296bc5f783d999a8effa82c40395e59b 100644 (file)
@@ -45,7 +45,8 @@ main (int argc, char *argv[])
 
        ooo = solipa_ooo_new (GTK_WINDOW (w),
                              NULL,
-                             NULL);
+                             NULL,
+                             0);
 
        if (ooo == NULL)
                {