Blame SOURCES/0006-Resolves-rhbz-144437-make-gnome-documents-not-crash-.patch

135360
From baa91265d38ac44294b93f8ce47162454fbeb6d4 Mon Sep 17 00:00:00 2001
135360
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
135360
Date: Mon, 8 May 2017 15:26:22 +0100
135360
Subject: [PATCH] Resolves: rhbz#144437 make gnome-documents not crash the
135360
 whole time
135360
135360
accept that once initted that LibreOffice cannot be deinitted and reinited
135360
(without lots of work), but allow the main loop to quit and restart so LOKs
135360
thread can run and exit successfully, new LOK connections will restart the main
135360
loop.
135360
135360
The buckets of global state continues to be valid the whole time this way
135360
135360
Change-Id: Ide54c0df2ce4065f7c192ae8c2cedfaaa2b58d72
135360
---
135360
 desktop/source/app/app.cxx               |  2 +-
135360
 desktop/source/app/officeipcthread.cxx   | 11 ++++++++---
135360
 desktop/source/app/officeipcthread.hxx   |  2 +-
135360
 desktop/source/lib/init.cxx              |  6 ++++++
135360
 framework/source/services/desktop.cxx    | 10 ++++++++--
135360
 libreofficekit/source/gtk/lokdocview.cxx |  6 ++++++
135360
 vcl/source/app/svapp.cxx                 |  1 +
135360
 vcl/source/app/svmain.cxx                |  8 ++++++++
135360
 8 files changed, 39 insertions(+), 7 deletions(-)
135360
135360
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
135360
index 5042752..372f605 100644
135360
--- a/desktop/source/app/app.cxx
135360
+++ b/desktop/source/app/app.cxx
135360
@@ -1890,7 +1890,7 @@ IMPL_LINK_NOARG(Desktop, OpenClients_Impl)
135360
     try {
135360
         OpenClients();
135360
 
135360
-        OfficeIPCThread::SetReady();
135360
+        OfficeIPCThread::SetReady(true);
135360
 
135360
         CloseSplashScreen();
135360
         CheckFirstRun( );
135360
diff --git a/desktop/source/app/officeipcthread.cxx b/desktop/source/app/officeipcthread.cxx
135360
index cb6ad0b..e65e935 100644
135360
--- a/desktop/source/app/officeipcthread.cxx
135360
+++ b/desktop/source/app/officeipcthread.cxx
135360
@@ -646,13 +646,15 @@ void OfficeIPCThread::DisableOfficeIPCThread(bool join)
135360
         // release mutex to avoid deadlocks
135360
         aMutex.clear();
135360
 
135360
-        OfficeIPCThread::SetReady(pOfficeIPCThread);
135360
+        OfficeIPCThread::SetReady(true, pOfficeIPCThread);
135360
 
135360
         // exit gracefully and join
135360
         if (join)
135360
         {
135360
             pOfficeIPCThread->join();
135360
         }
135360
+
135360
+        OfficeIPCThread::SetReady(false, pOfficeIPCThread);
135360
     }
135360
 }
135360
 
135360
@@ -675,14 +677,17 @@ OfficeIPCThread::~OfficeIPCThread()
135360
     pGlobalOfficeIPCThread.clear();
135360
 }
135360
 
135360
-void OfficeIPCThread::SetReady(
135360
+void OfficeIPCThread::SetReady(bool bIsReady,
135360
     rtl::Reference< OfficeIPCThread > const & pThread)
135360
 {
135360
     rtl::Reference< OfficeIPCThread > const & t(
135360
         pThread.is() ? pThread : pGlobalOfficeIPCThread);
135360
     if (t.is())
135360
     {
135360
-        t->cReady.set();
135360
+        if (bIsReady)
135360
+            t->cReady.set();
135360
+        else
135360
+            t->cReady.reset();
135360
     }
135360
 }
135360
 
135360
diff --git a/desktop/source/app/officeipcthread.hxx b/desktop/source/app/officeipcthread.hxx
135360
index e81f57a..f23eff2 100644
135360
--- a/desktop/source/app/officeipcthread.hxx
135360
+++ b/desktop/source/app/officeipcthread.hxx
135360
@@ -115,7 +115,7 @@ class OfficeIPCThread : public salhelper::Thread
135360
     static Status               EnableOfficeIPCThread();
135360
     static void                 DisableOfficeIPCThread(bool join = true);
135360
     // start dispatching events...
135360
-    static void                 SetReady(
135360
+    static void                 SetReady(bool bIsReady,
135360
         rtl::Reference< OfficeIPCThread > const & pThread =
135360
             rtl::Reference< OfficeIPCThread >());
135360
     static void                 WaitForReady(
135360
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
135360
index 184d777..2619539 100644
135360
--- a/desktop/source/lib/init.cxx
135360
+++ b/desktop/source/lib/init.cxx
135360
@@ -1697,7 +1697,12 @@ static void lo_startmain(void*)
135360
 {
135360
     osl_setThreadName("lo_startmain");
135360
 
135360
+    if (comphelper::SolarMutex::get())
135360
+        Application::GetSolarMutex().tryToAcquire();
135360
+
135360
     soffice_main();
135360
+
135360
+    Application::ReleaseSolarMutex();
135360
 }
135360
 
135360
 static bool bInitialized = false;
135360
@@ -1799,6 +1804,7 @@ static int lo_initialize(LibreOfficeKit* pThis, const char* pAppPath, const char
135360
         SAL_INFO("lok", "Enabling OfficeIPCThread");
135360
         OfficeIPCThread::EnableOfficeIPCThread();
135360
         SAL_INFO("lok", "Starting soffice_main");
135360
+        OfficeIPCThread::SetReady(false);
135360
         pLib->maThread = osl_createThread(lo_startmain, NULL);
135360
         SAL_INFO("lok", "Waiting for OfficeIPCThread");
135360
         OfficeIPCThread::WaitForReady();
135360
diff --git a/framework/source/services/desktop.cxx b/framework/source/services/desktop.cxx
135360
index 24b9761..c7dbaca 100644
135360
--- a/framework/source/services/desktop.cxx
135360
+++ b/framework/source/services/desktop.cxx
135360
@@ -59,6 +59,7 @@
135360
 #include <com/sun/star/frame/XTerminateListener2.hpp>
135360
 
135360
 #include <comphelper/sequence.hxx>
135360
+#include <comphelper/lok.hxx>
135360
 #include <cppuhelper/supportsservice.hxx>
135360
 #include <vcl/svapp.hxx>
135360
 
135360
@@ -231,8 +232,13 @@ sal_Bool SAL_CALL Desktop::terminate()
135360
 
135360
     // try to close all open frames.
135360
     // Allow using of any UI ... because Desktop.terminate() was designed as UI functionality in the past.
135360
-    bool bAllowUI      = true;
135360
-    bool bFramesClosed = impl_closeFrames(bAllowUI);
135360
+    bool bRestartableMainLoop = comphelper::LibreOfficeKit::isActive();
135360
+    bool bFramesClosed = impl_closeFrames(!bRestartableMainLoop);
135360
+    if (bRestartableMainLoop)
135360
+    {
135360
+         Application::Quit();
135360
+         return true;
135360
+    }
135360
     if ( ! bFramesClosed )
135360
     {
135360
         impl_sendCancelTerminationEvent(lCalledTerminationListener);
135360
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
135360
index 7d1ec01..92cf6b3 100644
135360
--- a/libreofficekit/source/gtk/lokdocview.cxx
135360
+++ b/libreofficekit/source/gtk/lokdocview.cxx
135360
@@ -2051,9 +2051,15 @@ static void lok_doc_view_destroy (GtkWidget* widget)
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
 
135360
     if (priv->m_pDocument)
135360
+    {
135360
         priv->m_pDocument->pClass->destroy (priv->m_pDocument);
135360
+        priv->m_pDocument = nullptr;
135360
+    }
135360
     if (priv->m_pOffice)
135360
+    {
135360
         priv->m_pOffice->pClass->destroy (priv->m_pOffice);
135360
+        priv->m_pOffice = nullptr;
135360
+    }
135360
 
135360
     GTK_WIDGET_CLASS (lok_doc_view_parent_class)->destroy (widget);
135360
 }
135360
diff --git a/vcl/source/app/svapp.cxx b/vcl/source/app/svapp.cxx
135360
index 302c4a7..2104243 100644
135360
--- a/vcl/source/app/svapp.cxx
135360
+++ b/vcl/source/app/svapp.cxx
135360
@@ -331,6 +331,7 @@ void Application::Execute()
135360
 {
135360
     ImplSVData* pSVData = ImplGetSVData();
135360
     pSVData->maAppData.mbInAppExecute = true;
135360
+    pSVData->maAppData.mbAppQuit = false;
135360
 
135360
     while ( !pSVData->maAppData.mbAppQuit )
135360
         Application::Yield();
135360
diff --git a/vcl/source/app/svmain.cxx b/vcl/source/app/svmain.cxx
135360
index f6d4e25..16977d3 100644
135360
--- a/vcl/source/app/svmain.cxx
135360
+++ b/vcl/source/app/svmain.cxx
135360
@@ -28,6 +28,7 @@
135360
 #include "tools/resmgr.hxx"
135360
 
135360
 #include "comphelper/processfactory.hxx"
135360
+#include "comphelper/lok.hxx"
135360
 
135360
 #include "unotools/syslocaleoptions.hxx"
135360
 #include "vcl/svapp.hxx"
135360
@@ -343,6 +344,13 @@ VCLUnoWrapperDeleter::disposing(lang::EventObject const& /* rSource */)
135360
 
135360
 void DeInitVCL()
135360
 {
135360
+    //rhbz#1444437, when using LibreOffice like a library you can't realistically
135360
+    //tear everything down and recreate them on the next call, there's too many
135360
+    //(c++) singletons that point to stuff that gets deleted during shutdown
135360
+    //which won't be recreated on restart.
135360
+    if (comphelper::LibreOfficeKit::isActive())
135360
+        return;
135360
+
135360
     ImplSVData* pSVData = ImplGetSVData();
135360
     pSVData->mbDeInit = true;
135360
 
135360
-- 
135360
1.8.3.1
135360