Blame SOURCES/0072-lokdocview-Call-open_document-in-another-thread.patch

135360
From ccd9b4a66c6cc65892321dafcb8a633514a717e5 Mon Sep 17 00:00:00 2001
135360
From: Pranav Kant <pranavk@gnome.org>
135360
Date: Mon, 6 Jul 2015 22:01:30 +0530
135360
Subject: [PATCH 072/398] lokdocview: Call open_document in another thread
135360
135360
This is to keep the widget responsive during document load.
135360
135360
Change-Id: I81acaffc75ca7deddd6cc2de6abae22d009d40cd
135360
(cherry picked from commit 645f00543405450cd3a3862482dc4e1cda65d098)
135360
---
135360
 include/LibreOfficeKit/LibreOfficeKitGtk.h         | 11 +++-
135360
 .../qa/gtktiledviewer/gtktiledviewer.cxx           | 42 +++++++++------
135360
 libreofficekit/source/gtk/lokdocview.cxx           | 59 +++++++++++++++++++---
135360
 3 files changed, 86 insertions(+), 26 deletions(-)
135360
135360
diff --git a/include/LibreOfficeKit/LibreOfficeKitGtk.h b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
index b98a85646e0e..3f56f08b2dce 100644
135360
--- a/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
+++ b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
@@ -45,8 +45,15 @@ GtkWidget*                     lok_doc_view_new                    (const gchar*
135360
                                                                     GCancellable *cancellable,
135360
                                                                     GError **error);
135360
 
135360
-gboolean                       lok_doc_view_open_document          (LOKDocView* pDocView,
135360
-                                                                    const gchar* pPath);
135360
+void                           lok_doc_view_open_document          (LOKDocView* pDocView,
135360
+                                                                    const gchar* pPath,
135360
+                                                                    GCancellable* cancellable,
135360
+                                                                    GAsyncReadyCallback callback,
135360
+                                                                    gpointer userdata);
135360
+
135360
+gboolean                       lok_doc_view_open_document_finish   (LOKDocView* pDocView,
135360
+                                                                    GAsyncResult* res,
135360
+                                                                    GError** error);
135360
 
135360
 /// Gets the document the viewer displays.
135360
 LibreOfficeKitDocument*        lok_doc_view_get_document           (LOKDocView* pDocView);
135360
diff --git a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
index 8c43e9d68092..c9fd3f2e5ca4 100644
135360
--- a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
+++ b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
@@ -41,6 +41,7 @@ std::map<std::string, GtkToolItem*> g_aCommandNameToolItems;
135360
 bool g_bToolItemBroadcast = true;
135360
 static GtkWidget* pVBox;
135360
 static GtkComboBoxText* pPartSelector;
135360
+static GtkWidget* pPartModeComboBox;
135360
 /// Should the part selector avoid calling lok::Document::setPart()?
135360
 static bool g_bPartSelectorBroadcast = true;
135360
 GtkWidget* pFindbar;
135360
@@ -290,6 +291,7 @@ static void signalSearch(LOKDocView* /*pLOKDocView*/, char* /*pPayload*/, gpoint
135360
     gtk_label_set_text(GTK_LABEL(pFindbarLabel), "Search key not found");
135360
 }
135360
 
135360
+
135360
 static void signalPart(LOKDocView* /*pLOKDocView*/, int nPart, gpointer /*pData*/)
135360
 {
135360
     g_bPartSelectorBroadcast = false;
135360
@@ -380,6 +382,28 @@ static void changePartMode( GtkWidget* pSelector, gpointer /* pItem */ )
135360
     }
135360
 }
135360
 
135360
+static void openDocumentCallback (GObject* source_object, GAsyncResult* res, gpointer /*userdata*/)
135360
+{
135360
+    LOKDocView* pDocView1 = LOK_DOC_VIEW (source_object);
135360
+    GError* error = NULL;
135360
+    GList *focusChain = NULL;
135360
+
135360
+    if (!lok_doc_view_open_document_finish(pDocView1, res, &error))
135360
+    {
135360
+        g_warning ("Error occurred while opening the document : %s", error->message);
135360
+        g_error_free (error);
135360
+    }
135360
+
135360
+    populatePartSelector();
135360
+    populatePartModeSelector( GTK_COMBO_BOX_TEXT(pPartModeComboBox) );
135360
+    // Connect these signals after populating the selectors, to avoid re-rendering on setting the default part/partmode.
135360
+    g_signal_connect(G_OBJECT(pPartModeComboBox), "changed", G_CALLBACK(changePartMode), 0);
135360
+    g_signal_connect(G_OBJECT(pPartSelector), "changed", G_CALLBACK(changePart), 0);
135360
+
135360
+    focusChain = g_list_append( focusChain, pDocView1 );
135360
+    gtk_container_set_focus_chain ( GTK_CONTAINER (pVBox), focusChain );
135360
+}
135360
+
135360
 int main( int argc, char* argv[] )
135360
 {
135360
     if( argc < 3 ||
135360
@@ -435,7 +459,7 @@ int main( int argc, char* argv[] )
135360
     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pSeparator2, -1);
135360
 
135360
     GtkToolItem* pPartModeSelectorToolItem = gtk_tool_item_new();
135360
-    GtkWidget* pPartModeComboBox = gtk_combo_box_text_new();
135360
+    pPartModeComboBox = gtk_combo_box_text_new();
135360
     gtk_container_add( GTK_CONTAINER(pPartModeSelectorToolItem), pPartModeComboBox );
135360
     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pPartModeSelectorToolItem, -1 );
135360
 
135360
@@ -542,21 +566,7 @@ int main( int argc, char* argv[] )
135360
     // Hide the findbar by default.
135360
     gtk_widget_hide(pFindbar);
135360
 
135360
-    int bOpened = lok_doc_view_open_document( LOK_DOC_VIEW(pDocView), argv[2] );
135360
-    if (!bOpened)
135360
-        g_error("main: lok_doc_view_open_document() failed");
135360
-    assert(lok_doc_view_get_document(LOK_DOC_VIEW(pDocView)));
135360
-
135360
-    populatePartSelector();
135360
-    populatePartModeSelector( GTK_COMBO_BOX_TEXT(pPartModeComboBox) );
135360
-    // Connect these signals after populating the selectors, to avoid re-rendering on setting the default part/partmode.
135360
-    g_signal_connect(G_OBJECT(pPartModeComboBox), "changed", G_CALLBACK(changePartMode), 0);
135360
-    g_signal_connect(G_OBJECT(pPartSelector), "changed", G_CALLBACK(changePart), 0);
135360
-
135360
-    // Make only LOKDocView widget as focussable
135360
-    GList *focusChain = NULL;
135360
-    focusChain = g_list_append( focusChain, pDocView );
135360
-    gtk_container_set_focus_chain ( GTK_CONTAINER (pVBox), focusChain );
135360
+    lok_doc_view_open_document( LOK_DOC_VIEW(pDocView), argv[2], NULL, openDocumentCallback, pDocView );
135360
 
135360
     gtk_main();
135360
 
135360
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
135360
index 27d597ae57e3..40c0860973ad 100644
135360
--- a/libreofficekit/source/gtk/lokdocview.cxx
135360
+++ b/libreofficekit/source/gtk/lokdocview.cxx
135360
@@ -108,8 +108,6 @@ struct _LOKDocViewPrivate
135360
 
135360
 enum
135360
 {
135360
-    LOAD_CHANGED,
135360
-    LOAD_FAILED,
135360
     EDIT_CHANGED,
135360
     COMMAND_CHANGED,
135360
     SEARCH_NOT_FOUND,
135360
@@ -337,19 +335,23 @@ static gboolean
135360
 globalCallback (gpointer pData)
135360
 {
135360
     CallbackData* pCallback = static_cast<CallbackData*>(pData);
135360
+    LOKDocViewPrivate* priv = static_cast<LOKDocViewPrivate*>(lok_doc_view_get_instance_private (pCallback->m_pDocView));
135360
 
135360
     switch (pCallback->m_nType)
135360
     {
135360
     case LOK_CALLBACK_STATUS_INDICATOR_START:
135360
     {
135360
+        priv->m_nLoadProgress = 0;
135360
     }
135360
     break;
135360
     case LOK_CALLBACK_STATUS_INDICATOR_SET_VALUE:
135360
     {
135360
+        priv->m_nLoadProgress = std::stoi(pCallback->m_aPayload);
135360
     }
135360
     break;
135360
     case LOK_CALLBACK_STATUS_INDICATOR_FINISH:
135360
     {
135360
+        priv->m_nLoadProgress = 100;
135360
     }
135360
     break;
135360
     default:
135360
@@ -1389,15 +1391,30 @@ lok_doc_view_new (const gchar* pPath, GCancellable *cancellable, GError **error)
135360
 }
135360
 
135360
 /**
135360
- * lok_doc_view_open_document:
135360
+ * lok_doc_view_open_document_finish:
135360
  * @pDocView: The #LOKDocView instance
135360
- * @pPath: The path of the document that #LOKDocView widget should try to open
135360
+ * @res:
135360
+ * @error:
135360
  *
135360
  * Returns: %TRUE if the document is loaded succesfully, %FALSE otherwise
135360
  */
135360
 SAL_DLLPUBLIC_EXPORT gboolean
135360
-lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
135360
+lok_doc_view_open_document_finish (LOKDocView* pDocView, GAsyncResult* res, GError** error)
135360
+{
135360
+    GTask* task = G_TASK(res);
135360
+
135360
+    g_return_val_if_fail(g_task_is_valid(res, pDocView), NULL);
135360
+    //FIXME: make source_tag workx
135360
+    //g_return_val_if_fail(g_task_get_source_tag(task) == lok_doc_view_open_document, NULL);
135360
+    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
135360
+
135360
+    return g_task_propagate_boolean(task, error);
135360
+}
135360
+
135360
+static void
135360
+lok_doc_view_open_document_func (GTask* task, gpointer source_object, gpointer task_data, GCancellable* cancellable)
135360
 {
135360
+    LOKDocView* pDocView = LOK_DOC_VIEW(source_object);
135360
     LOKDocViewPrivate *priv = static_cast<LOKDocViewPrivate*>(lok_doc_view_get_instance_private (pDocView));
135360
 
135360
     if ( priv->m_pDocument )
135360
@@ -1407,13 +1424,13 @@ lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
135360
     }
135360
 
135360
     priv->m_pOffice->pClass->registerCallback(priv->m_pOffice, globalCallbackWorker, pDocView);
135360
-    priv->m_pDocument = priv->m_pOffice->pClass->documentLoad( priv->m_pOffice, pPath );
135360
+    priv->m_pDocument = priv->m_pOffice->pClass->documentLoad( priv->m_pOffice, priv->m_aDocPath );
135360
     if ( !priv->m_pDocument )
135360
     {
135360
         // FIXME: should have a GError parameter and populate it.
135360
         char *pError = priv->m_pOffice->pClass->getError( priv->m_pOffice );
135360
         fprintf( stderr, "Error opening document '%s'\n", pError );
135360
-        return FALSE;
135360
+        g_task_return_new_error(task, 0, 0, pError);
135360
     }
135360
     else
135360
     {
135360
@@ -1438,8 +1455,34 @@ lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
135360
                                     nDocumentHeightPixels);
135360
         gtk_widget_set_can_focus(GTK_WIDGET(pDocView), TRUE);
135360
         gtk_widget_grab_focus(GTK_WIDGET(pDocView));
135360
+        g_task_return_boolean (task, true);
135360
     }
135360
-    return TRUE;
135360
+}
135360
+
135360
+/**
135360
+ * lok_doc_view_open_document:
135360
+ * @pDocView: The #LOKDocView instance
135360
+ * @pPath: The path of the document that #LOKDocView widget should try to open
135360
+ *
135360
+ * Returns: %TRUE if the document is loaded succesfully, %FALSE otherwise
135360
+ */
135360
+SAL_DLLPUBLIC_EXPORT void
135360
+lok_doc_view_open_document (LOKDocView* pDocView,
135360
+                            const gchar* pPath,
135360
+                            GCancellable* cancellable,
135360
+                            GAsyncReadyCallback callback,
135360
+                            gpointer userdata)
135360
+{
135360
+    GTask *task;
135360
+    LOKDocViewPrivate *priv = static_cast<LOKDocViewPrivate*>(lok_doc_view_get_instance_private (pDocView));
135360
+    priv->m_aDocPath = g_strdup(pPath);
135360
+
135360
+    task = g_task_new(pDocView, cancellable, callback, userdata);
135360
+    // FIXME: Use source_tag to check the task.
135360
+    //g_task_set_source_tag(task, lok_doc_view_open_document);
135360
+
135360
+    g_task_run_in_thread(task, lok_doc_view_open_document_func);
135360
+    g_object_unref(task);
135360
 }
135360
 
135360
 /**
135360
-- 
135360
2.12.0
135360