summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPranav Kant <pranavk@gnome.org>2015-07-06 22:01:30 +0530
committerAshod Nakashian <ashod.nakashian@collabora.co.uk>2016-02-07 21:55:22 -0500
commit5100e67c7ff0f208716852d2dd14a40340299c21 (patch)
treeb148d46d0bed89fa0bcca2e401c9634de57bd994
parentc2fd46e715549076b1f3e3b5ad9c5124d0e9e2fc (diff)
lokdocview: Call open_document in another thread
This is to keep the widget responsive during document load. Change-Id: I81acaffc75ca7deddd6cc2de6abae22d009d40cd (cherry picked from commit 645f00543405450cd3a3862482dc4e1cda65d098)
-rw-r--r--include/LibreOfficeKit/LibreOfficeKitGtk.h11
-rw-r--r--libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx42
-rw-r--r--libreofficekit/source/gtk/lokdocview.cxx59
3 files changed, 86 insertions, 26 deletions
diff --git a/include/LibreOfficeKit/LibreOfficeKitGtk.h b/include/LibreOfficeKit/LibreOfficeKitGtk.h
index b98a85646e0e..3f56f08b2dce 100644
--- a/include/LibreOfficeKit/LibreOfficeKitGtk.h
+++ b/include/LibreOfficeKit/LibreOfficeKitGtk.h
@@ -45,8 +45,15 @@ GtkWidget* lok_doc_view_new (const gchar*
GCancellable *cancellable,
GError **error);
-gboolean lok_doc_view_open_document (LOKDocView* pDocView,
- const gchar* pPath);
+void lok_doc_view_open_document (LOKDocView* pDocView,
+ const gchar* pPath,
+ GCancellable* cancellable,
+ GAsyncReadyCallback callback,
+ gpointer userdata);
+
+gboolean lok_doc_view_open_document_finish (LOKDocView* pDocView,
+ GAsyncResult* res,
+ GError** error);
/// Gets the document the viewer displays.
LibreOfficeKitDocument* lok_doc_view_get_document (LOKDocView* pDocView);
diff --git a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
index f81a5ccee596..b731ef54be4a 100644
--- a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
+++ b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
@@ -41,6 +41,7 @@ std::map<std::string, GtkToolItem*> g_aCommandNameToolItems;
bool g_bToolItemBroadcast = true;
static GtkWidget* pVBox;
static GtkComboBoxText* pPartSelector;
+static GtkWidget* pPartModeComboBox;
/// Should the part selector avoid calling lok::Document::setPart()?
static bool g_bPartSelectorBroadcast = true;
GtkWidget* pFindbar;
@@ -291,6 +292,7 @@ static void signalSearch(LOKDocView* /*pLOKDocView*/, char* /*pPayload*/, gpoint
gtk_label_set_text(GTK_LABEL(pFindbarLabel), "Search key not found");
}
+
static void signalPart(LOKDocView* /*pLOKDocView*/, int nPart, gpointer /*pData*/)
{
g_bPartSelectorBroadcast = false;
@@ -388,6 +390,28 @@ static void changePartMode( GtkWidget* pSelector, gpointer /* pItem */ )
}
}
+static void openDocumentCallback (GObject* source_object, GAsyncResult* res, gpointer /*userdata*/)
+{
+ LOKDocView* pDocView1 = LOK_DOC_VIEW (source_object);
+ GError* error = NULL;
+ GList *focusChain = NULL;
+
+ if (!lok_doc_view_open_document_finish(pDocView1, res, &error))
+ {
+ g_warning ("Error occurred while opening the document : %s", error->message);
+ g_error_free (error);
+ }
+
+ populatePartSelector();
+ populatePartModeSelector( GTK_COMBO_BOX_TEXT(pPartModeComboBox) );
+ // Connect these signals after populating the selectors, to avoid re-rendering on setting the default part/partmode.
+ g_signal_connect(G_OBJECT(pPartModeComboBox), "changed", G_CALLBACK(changePartMode), 0);
+ g_signal_connect(G_OBJECT(pPartSelector), "changed", G_CALLBACK(changePart), 0);
+
+ focusChain = g_list_append( focusChain, pDocView1 );
+ gtk_container_set_focus_chain ( GTK_CONTAINER (pVBox), focusChain );
+}
+
int main( int argc, char* argv[] )
{
if( argc < 3 ||
@@ -443,7 +467,7 @@ int main( int argc, char* argv[] )
gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pSeparator2, -1);
GtkToolItem* pPartModeSelectorToolItem = gtk_tool_item_new();
- GtkWidget* pPartModeComboBox = gtk_combo_box_text_new();
+ pPartModeComboBox = gtk_combo_box_text_new();
gtk_container_add( GTK_CONTAINER(pPartModeSelectorToolItem), pPartModeComboBox );
gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pPartModeSelectorToolItem, -1 );
@@ -553,21 +577,7 @@ int main( int argc, char* argv[] )
// Hide the findbar by default.
gtk_widget_hide(pFindbar);
- int bOpened = lok_doc_view_open_document( LOK_DOC_VIEW(pDocView), argv[2] );
- if (!bOpened)
- g_error("main: lok_doc_view_open_document() failed");
- assert(lok_doc_view_get_document(LOK_DOC_VIEW(pDocView)));
-
- populatePartSelector();
- populatePartModeSelector( GTK_COMBO_BOX_TEXT(pPartModeComboBox) );
- // Connect these signals after populating the selectors, to avoid re-rendering on setting the default part/partmode.
- g_signal_connect(G_OBJECT(pPartModeComboBox), "changed", G_CALLBACK(changePartMode), 0);
- g_signal_connect(G_OBJECT(pPartSelector), "changed", G_CALLBACK(changePart), 0);
-
- // Make only LOKDocView widget as focussable
- GList *focusChain = NULL;
- focusChain = g_list_append( focusChain, pDocView );
- gtk_container_set_focus_chain ( GTK_CONTAINER (pVBox), focusChain );
+ lok_doc_view_open_document( LOK_DOC_VIEW(pDocView), argv[2], NULL, openDocumentCallback, pDocView );
gtk_main();
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
index 807b202ab242..046578edad1e 100644
--- a/libreofficekit/source/gtk/lokdocview.cxx
+++ b/libreofficekit/source/gtk/lokdocview.cxx
@@ -109,8 +109,6 @@ struct _LOKDocViewPrivate
enum
{
- LOAD_CHANGED,
- LOAD_FAILED,
EDIT_CHANGED,
COMMAND_CHANGED,
SEARCH_NOT_FOUND,
@@ -352,19 +350,23 @@ static gboolean
globalCallback (gpointer pData)
{
CallbackData* pCallback = static_cast<CallbackData*>(pData);
+ LOKDocViewPrivate* priv = static_cast<LOKDocViewPrivate*>(lok_doc_view_get_instance_private (pCallback->m_pDocView));
switch (pCallback->m_nType)
{
case LOK_CALLBACK_STATUS_INDICATOR_START:
{
+ priv->m_nLoadProgress = 0;
}
break;
case LOK_CALLBACK_STATUS_INDICATOR_SET_VALUE:
{
+ priv->m_nLoadProgress = std::stoi(pCallback->m_aPayload);
}
break;
case LOK_CALLBACK_STATUS_INDICATOR_FINISH:
{
+ priv->m_nLoadProgress = 100;
}
break;
default:
@@ -1419,15 +1421,30 @@ lok_doc_view_new (const gchar* pPath, GCancellable *cancellable, GError **error)
}
/**
- * lok_doc_view_open_document:
+ * lok_doc_view_open_document_finish:
* @pDocView: The #LOKDocView instance
- * @pPath: The path of the document that #LOKDocView widget should try to open
+ * @res:
+ * @error:
*
* Returns: %TRUE if the document is loaded succesfully, %FALSE otherwise
*/
SAL_DLLPUBLIC_EXPORT gboolean
-lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
+lok_doc_view_open_document_finish (LOKDocView* pDocView, GAsyncResult* res, GError** error)
+{
+ GTask* task = G_TASK(res);
+
+ g_return_val_if_fail(g_task_is_valid(res, pDocView), NULL);
+ //FIXME: make source_tag workx
+ //g_return_val_if_fail(g_task_get_source_tag(task) == lok_doc_view_open_document, NULL);
+ g_return_val_if_fail(error == NULL || *error == NULL, NULL);
+
+ return g_task_propagate_boolean(task, error);
+}
+
+static void
+lok_doc_view_open_document_func (GTask* task, gpointer source_object, gpointer task_data, GCancellable* cancellable)
{
+ LOKDocView* pDocView = LOK_DOC_VIEW(source_object);
LOKDocViewPrivate *priv = static_cast<LOKDocViewPrivate*>(lok_doc_view_get_instance_private (pDocView));
if ( priv->m_pDocument )
@@ -1437,13 +1454,13 @@ lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
}
priv->m_pOffice->pClass->registerCallback(priv->m_pOffice, globalCallbackWorker, pDocView);
- priv->m_pDocument = priv->m_pOffice->pClass->documentLoad( priv->m_pOffice, pPath );
+ priv->m_pDocument = priv->m_pOffice->pClass->documentLoad( priv->m_pOffice, priv->m_aDocPath );
if ( !priv->m_pDocument )
{
// FIXME: should have a GError parameter and populate it.
char *pError = priv->m_pOffice->pClass->getError( priv->m_pOffice );
fprintf( stderr, "Error opening document '%s'\n", pError );
- return FALSE;
+ g_task_return_new_error(task, 0, 0, pError);
}
else
{
@@ -1468,8 +1485,34 @@ lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
nDocumentHeightPixels);
gtk_widget_set_can_focus(GTK_WIDGET(pDocView), TRUE);
gtk_widget_grab_focus(GTK_WIDGET(pDocView));
+ g_task_return_boolean (task, true);
}
- return TRUE;
+}
+
+/**
+ * lok_doc_view_open_document:
+ * @pDocView: The #LOKDocView instance
+ * @pPath: The path of the document that #LOKDocView widget should try to open
+ *
+ * Returns: %TRUE if the document is loaded succesfully, %FALSE otherwise
+ */
+SAL_DLLPUBLIC_EXPORT void
+lok_doc_view_open_document (LOKDocView* pDocView,
+ const gchar* pPath,
+ GCancellable* cancellable,
+ GAsyncReadyCallback callback,
+ gpointer userdata)
+{
+ GTask *task;
+ LOKDocViewPrivate *priv = static_cast<LOKDocViewPrivate*>(lok_doc_view_get_instance_private (pDocView));
+ priv->m_aDocPath = g_strdup(pPath);
+
+ task = g_task_new(pDocView, cancellable, callback, userdata);
+ // FIXME: Use source_tag to check the task.
+ //g_task_set_source_tag(task, lok_doc_view_open_document);
+
+ g_task_run_in_thread(task, lok_doc_view_open_document_func);
+ g_object_unref(task);
}
/**