Blame SOURCES/0377-lokdocview-Return-if-no-document-is-set.patch

135360
From 192a19277531a36bccadde67fbf307eebe60b3ea Mon Sep 17 00:00:00 2001
135360
From: Pranav Kant <pranavk@libreoffice.org>
135360
Date: Thu, 17 Dec 2015 21:03:47 +0530
135360
Subject: [PATCH 377/398] lokdocview: Return if no document is set
135360
135360
For example, when document has been destroyed using
135360
lok_doc_view_destroy_document()
135360
135360
Change-Id: I531b85018ffa25bcf88fb101c912b9f11b489a97
135360
Reviewed-on: https://gerrit.libreoffice.org/20779
135360
Reviewed-by: David Tardon <dtardon@redhat.com>
135360
Tested-by: David Tardon <dtardon@redhat.com>
135360
(cherry picked from commit df4a196b8f1e97d8a45d1d517942e01bd13182e7)
135360
Reviewed-on: https://gerrit.libreoffice.org/21267
135360
Tested-by: Jenkins <ci@libreoffice.org>
135360
Reviewed-by: Pranav Kant <pranavk@gnome.org>
135360
(cherry picked from commit dd6006be974ce808f276c550d32f1bc01582aaf3)
135360
---
135360
 include/LibreOfficeKit/LibreOfficeKitGtk.h         | 10 ++++---
135360
 .../qa/gtktiledviewer/gtktiledviewer.cxx           |  3 ++
135360
 libreofficekit/source/gtk/lokdocview.cxx           | 35 ++++++++++++++++++++--
135360
 3 files changed, 42 insertions(+), 6 deletions(-)
135360
135360
diff --git a/include/LibreOfficeKit/LibreOfficeKitGtk.h b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
index 62d2e7f29247..911bbdfa6049 100644
135360
--- a/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
+++ b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
@@ -125,7 +125,7 @@ gfloat                         lok_doc_view_get_zoom               (LOKDocView*
135360
  * @pDocView: The #LOKDocView instance
135360
  *
135360
  * Returns: Part refers to either individual sheets in a Calc, or slides in Impress,
135360
- * and has no relevance for Writer.
135360
+ * and has no relevance for Writer. Returns -1 if no document is set currently.
135360
  */
135360
 gint                           lok_doc_view_get_parts              (LOKDocView* pDocView);
135360
 
135360
@@ -133,7 +133,7 @@ gint                           lok_doc_view_get_parts              (LOKDocView*
135360
  * lok_doc_view_get_part:
135360
  * @pDocView: The #LOKDocView instance
135360
  *
135360
- * Returns: Current part number of the document
135360
+ * Returns: Current part number of the document. Returns -1 if no document is set currently.
135360
  */
135360
 gint                           lok_doc_view_get_part               (LOKDocView* pDocView);
135360
 
135360
@@ -150,7 +150,8 @@ void                           lok_doc_view_set_part               (LOKDocView*
135360
  * @pDocView: The #LOKDocView instance
135360
  * @nPart:
135360
  *
135360
- * Returns: Get current part name of loaded document
135360
+ * Returns: Get current part name of loaded document. Returns null if no
135360
+ * document is set, or document has been destroyed using lok_doc_view_destroy_document.
135360
  */
135360
 gchar*                         lok_doc_view_get_part_name          (LOKDocView* pDocView,
135360
                                                                     int nPart);
135360
@@ -248,7 +249,8 @@ void                           lok_doc_view_highlight_all          (LOKDocView*
135360
  * @pUsedMimeType: (out): output parameter to inform about the determined format
135360
  * (suggested or plain text).
135360
  *
135360
- * Returns: Selected text. The caller must free the returned buffer after use.
135360
+ * Returns: Selected text. The caller must free the returned buffer after
135360
+ * use. Returns null if no document is set.
135360
  */
135360
 gchar*                          lok_doc_view_copy_selection        (LOKDocView* pDocView,
135360
                                                                     const gchar* pMimeType,
135360
diff --git a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
index d064c4ce3e47..e867e1b239a7 100644
135360
--- a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
+++ b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
@@ -549,7 +549,10 @@ static void doCopy(GtkWidget* pButton, gpointer /*pItem*/)
135360
     TiledWindow& rWindow = lcl_getTiledWindow(pButton);
135360
     LOKDocView* pLOKDocView = LOK_DOC_VIEW(rWindow.m_pDocView);
135360
     char* pUsedFormat = nullptr;
135360
+    // TODO: Should check `text-selection` signal before trying to copy
135360
     char* pSelection = lok_doc_view_copy_selection(pLOKDocView, "text/html", &pUsedFormat);
135360
+    if (!pSelection)
135360
+        return;
135360
 
135360
     GtkClipboard* pClipboard = gtk_clipboard_get_for_display(gtk_widget_get_display(rWindow.m_pDocView), GDK_SELECTION_CLIPBOARD);
135360
     std::string aUsedFormat(pUsedFormat);
135360
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
135360
index 9d7f0740595e..858f08b66890 100644
135360
--- a/libreofficekit/source/gtk/lokdocview.cxx
135360
+++ b/libreofficekit/source/gtk/lokdocview.cxx
135360
@@ -342,6 +342,9 @@ doSearch(LOKDocView* pDocView, const char* pText, bool bBackwards, bool highligh
135360
     cairo_rectangle_int_t cairoVisRect;
135360
     int x, y;
135360
 
135360
+    if (!priv->m_pDocument)
135360
+        return;
135360
+
135360
     cairo_region_get_rectangle(cairoVisRegion, 0, &cairoVisRect);
135360
     x = pixelToTwip (cairoVisRect.x, priv->m_fZoom);
135360
     y = pixelToTwip (cairoVisRect.y, priv->m_fZoom);
135360
@@ -1630,8 +1633,6 @@ setClientZoomInThread(gpointer data)
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
     LOEvent* pLOEvent = static_cast<LOEvent*>(g_task_get_task_data(task));
135360
 
135360
-    if (!priv->m_pDocument)
135360
-        return;
135360
     priv->m_pDocument->pClass->setClientZoom(priv->m_pDocument,
135360
                                              pLOEvent->m_nTilePixelWidth,
135360
                                              pLOEvent->m_nTilePixelHeight,
135360
@@ -2518,6 +2519,9 @@ SAL_DLLPUBLIC_EXPORT gint
135360
 lok_doc_view_get_parts (LOKDocView* pDocView)
135360
 {
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
+    if (!priv->m_pDocument)
135360
+        return -1;
135360
+
135360
     priv->m_pDocument->pClass->setView(priv->m_pDocument, priv->m_nViewId);
135360
     return priv->m_pDocument->pClass->getParts( priv->m_pDocument );
135360
 }
135360
@@ -2526,6 +2530,9 @@ SAL_DLLPUBLIC_EXPORT gint
135360
 lok_doc_view_get_part (LOKDocView* pDocView)
135360
 {
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
+    if (!priv->m_pDocument)
135360
+        return -1;
135360
+
135360
     priv->m_pDocument->pClass->setView(priv->m_pDocument, priv->m_nViewId);
135360
     return priv->m_pDocument->pClass->getPart( priv->m_pDocument );
135360
 }
135360
@@ -2538,6 +2545,9 @@ lok_doc_view_set_part (LOKDocView* pDocView, int nPart)
135360
     LOEvent* pLOEvent = new LOEvent(LOK_SET_PART);
135360
     GError* error = nullptr;
135360
 
135360
+    if (!priv->m_pDocument)
135360
+        return;
135360
+
135360
     pLOEvent->m_nPart = nPart;
135360
     g_task_set_task_data(task, pLOEvent, LOEvent::destroy);
135360
 
135360
@@ -2554,6 +2564,10 @@ SAL_DLLPUBLIC_EXPORT gchar*
135360
 lok_doc_view_get_part_name (LOKDocView* pDocView, int nPart)
135360
 {
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
+
135360
+    if (!priv->m_pDocument)
135360
+        return nullptr;
135360
+
135360
     priv->m_pDocument->pClass->setView(priv->m_pDocument, priv->m_nViewId);
135360
     return priv->m_pDocument->pClass->getPartName( priv->m_pDocument, nPart );
135360
 }
135360
@@ -2566,6 +2580,10 @@ lok_doc_view_set_partmode(LOKDocView* pDocView,
135360
     GTask* task = g_task_new(pDocView, nullptr, nullptr, nullptr);
135360
     LOEvent* pLOEvent = new LOEvent(LOK_SET_PARTMODE);
135360
     GError* error = nullptr;
135360
+
135360
+    if (!priv->m_pDocument)
135360
+        return;
135360
+
135360
     pLOEvent->m_nPartMode = nPartMode;
135360
     g_task_set_task_data(task, pLOEvent, LOEvent::destroy);
135360
 
135360
@@ -2632,6 +2650,10 @@ lok_doc_view_set_edit(LOKDocView* pDocView,
135360
     GTask* task = g_task_new(pDocView, nullptr, nullptr, nullptr);
135360
     LOEvent* pLOEvent = new LOEvent(LOK_SET_EDIT);
135360
     GError* error = nullptr;
135360
+
135360
+    if (!priv->m_pDocument)
135360
+        return;
135360
+
135360
     pLOEvent->m_bEdit = bEdit;
135360
     g_task_set_task_data(task, pLOEvent, LOEvent::destroy);
135360
 
135360
@@ -2658,6 +2680,10 @@ lok_doc_view_post_command (LOKDocView* pDocView,
135360
                            gboolean bNotifyWhenFinished)
135360
 {
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
+
135360
+    if (!priv->m_pDocument)
135360
+        return;
135360
+
135360
     if (priv->m_bEdit)
135360
         LOKPostCommand(pDocView, pCommand, pArguments, bNotifyWhenFinished);
135360
     else
135360
@@ -2693,6 +2719,8 @@ lok_doc_view_copy_selection (LOKDocView* pDocView,
135360
                              gchar** pUsedMimeType)
135360
 {
135360
     LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(pDocView);
135360
+    if (!pDocument)
135360
+        return nullptr;
135360
     return pDocument->pClass->getTextSelection(pDocument, pMimeType, pUsedMimeType);
135360
 }
135360
 
135360
@@ -2706,6 +2734,9 @@ lok_doc_view_paste (LOKDocView* pDocView,
135360
     LibreOfficeKitDocument* pDocument = priv->m_pDocument;
135360
     gboolean ret = 0;
135360
 
135360
+    if (!pDocument)
135360
+        return false;
135360
+
135360
     if (!priv->m_bEdit)
135360
     {
135360
         g_info ("ignoring paste in view-only mode");
135360
-- 
135360
2.12.0
135360