Blob Blame History Raw
From 9a1d485f937bf92ae860078b94a5372b3a4ce718 Mon Sep 17 00:00:00 2001
From: Pranav Kant <pranavk@gnome.org>
Date: Thu, 18 Jun 2015 21:52:22 +0530
Subject: [PATCH 055/398] lokdocview: Use GInitable

The construction of LokDocView widget can fail because it
involves initializing the lok context via lok_init.

Having lok_init calls in constructed virtual method is a bad idea
since it assumes that construction will never fail. So, implement
GInitable for this class, and move the object initialization from
constructed to initable.

Change-Id: Idf18a054cf8ef2e946392458ec52cb0107bd7454
(cherry picked from commit a2aaf911e2e7b63af920186acb2a5e03bc58bd54)
---
 include/LibreOfficeKit/LibreOfficeKitGtk.h         |  6 ++-
 .../qa/gtktiledviewer/gtktiledviewer.cxx           |  2 +-
 libreofficekit/source/gtk/lokdocview.cxx           | 49 +++++++++++++++-------
 3 files changed, 39 insertions(+), 18 deletions(-)

diff --git a/include/LibreOfficeKit/LibreOfficeKitGtk.h b/include/LibreOfficeKit/LibreOfficeKitGtk.h
index 7048dbefc0a1..3eaf28352a11 100644
--- a/include/LibreOfficeKit/LibreOfficeKitGtk.h
+++ b/include/LibreOfficeKit/LibreOfficeKitGtk.h
@@ -42,10 +42,12 @@ struct _LOKDocViewClass
 
 GType                          lok_doc_view_get_type               (void) G_GNUC_CONST;
 
-GtkWidget*                     lok_doc_view_new                    (const char* pPath);
+GtkWidget*                     lok_doc_view_new                    (const gchar* pPath,
+                                                                    GCancellable *cancellable,
+                                                                    GError **error);
 
 gboolean                       lok_doc_view_open_document          (LOKDocView* pDocView,
-                                                                    char* pPath);
+                                                                    const gchar* pPath);
 
 /// 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 57556616d803..fedd6c9fdf13 100644
--- a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
+++ b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
@@ -520,7 +520,7 @@ int main( int argc, char* argv[] )
     gtk_box_pack_end(GTK_BOX(pVBox), pFindbar, FALSE, FALSE, 0);
 
     // Docview
-    pDocView = lok_doc_view_new(argv[1]);
+    pDocView = lok_doc_view_new (argv[1], NULL, NULL);
     if (pDocView == NULL)
         g_error ("Error while creating LOKDocView widget");
     g_signal_connect(pDocView, "edit-changed", G_CALLBACK(signalEdit), NULL);
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
index 20b1aa6b837e..144f17efba94 100644
--- a/libreofficekit/source/gtk/lokdocview.cxx
+++ b/libreofficekit/source/gtk/lokdocview.cxx
@@ -137,12 +137,16 @@ enum
 
 static guint doc_view_signals[LAST_SIGNAL] = { 0 };
 
+static void lok_doc_view_initable_iface_init (GInitableIface *iface);
+
 SAL_DLLPUBLIC_EXPORT GType lok_doc_view_get_type();
 #ifdef __GNUC__
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-function"
 #endif
-G_DEFINE_TYPE_WITH_PRIVATE (LOKDocView, lok_doc_view, GTK_TYPE_DRAWING_AREA)
+G_DEFINE_TYPE_WITH_CODE (LOKDocView, lok_doc_view, GTK_TYPE_DRAWING_AREA,
+                         G_ADD_PRIVATE (LOKDocView)
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, lok_doc_view_initable_iface_init));
 #ifdef __GNUC__
 #pragma GCC diagnostic pop
 #endif
@@ -1103,14 +1107,30 @@ static void lok_doc_view_finalize (GObject* object)
     G_OBJECT_CLASS (lok_doc_view_parent_class)->finalize (object);
 }
 
-static void lok_doc_view_constructed (GObject* object)
+static gboolean lok_doc_view_initable_init (GInitable *initable, GCancellable* /*cancellable*/, GError **error)
 {
-    LOKDocView* pDocView = LOK_DOC_VIEW (object);
-    LOKDocViewPrivate* priv = pDocView->priv;
+    LOKDocView *pDocView = LOK_DOC_VIEW (initable);
+
+    if (pDocView->priv->m_pOffice != NULL)
+        return TRUE;
+
+    pDocView->priv->m_pOffice = lok_init (pDocView->priv->m_aLOPath);
+
+    if (pDocView->priv->m_pOffice == NULL)
+    {
+        g_set_error (error,
+                     g_quark_from_static_string ("LOK initialization error"), 0,
+                     "Failed to get LibreOfficeKit context. Make sure path (%s) is correct",
+                     pDocView->priv->m_aLOPath);
+        return FALSE;
+    }
 
-    G_OBJECT_CLASS (lok_doc_view_parent_class)->constructed (object);
+    return TRUE;
+}
 
-    pDocView->priv->m_pOffice = lok_init (priv->m_aLOPath);
+static void lok_doc_view_initable_iface_init (GInitableIface *iface)
+{
+    iface->init = lok_doc_view_initable_init;
 }
 
 static void lok_doc_view_class_init (LOKDocViewClass* pClass)
@@ -1121,7 +1141,6 @@ static void lok_doc_view_class_init (LOKDocViewClass* pClass)
     pGObjectClass->get_property = lok_doc_view_get_property;
     pGObjectClass->set_property = lok_doc_view_set_property;
     pGObjectClass->finalize = lok_doc_view_finalize;
-    pGObjectClass->constructed = lok_doc_view_constructed;
 
     pWidgetClass->draw = lok_doc_view_draw;
     pWidgetClass->button_press_event = lok_doc_view_signal_button;
@@ -1343,18 +1362,19 @@ static void lok_doc_view_class_init (LOKDocViewClass* pClass)
                      G_TYPE_STRING);
 }
 
-
-
 /**
  * lok_doc_view_new:
  * @pPath: LibreOffice install path.
+ * @cancellable: The cancellable object that you can use to cancel this
+ * operation.
+ * @error: The error that will be set if the object fails to initialize.
  *
- * Returns: The #LOKDocView widget instance.
+ * Returns: (transfer none): The #LOKDocView widget instance.
  */
 SAL_DLLPUBLIC_EXPORT GtkWidget*
-lok_doc_view_new (const char* pPath)
+lok_doc_view_new (const gchar* pPath, GCancellable *cancellable, GError **error)
 {
-    return GTK_WIDGET (g_object_new(LOK_TYPE_DOC_VIEW, "lopath", pPath, NULL));
+    return GTK_WIDGET (g_initable_new (LOK_TYPE_DOC_VIEW, cancellable, error, "lopath", pPath, NULL));
 }
 
 /**
@@ -1365,7 +1385,7 @@ lok_doc_view_new (const char* pPath)
  * Returns: %TRUE if the document is loaded succesfully, %FALSE otherwise
  */
 SAL_DLLPUBLIC_EXPORT gboolean
-lok_doc_view_open_document (LOKDocView* pDocView, char* pPath)
+lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
 {
     if ( pDocView->priv->m_pDocument )
     {
@@ -1374,8 +1394,7 @@ lok_doc_view_open_document (LOKDocView* pDocView, char* pPath)
     }
 
     pDocView->priv->m_pOffice->pClass->registerCallback(pDocView->priv->m_pOffice, globalCallbackWorker, pDocView);
-    pDocView->priv->m_pDocument = pDocView->priv->m_pOffice->pClass->documentLoad( pDocView->priv->m_pOffice,
-                                                                   pPath );
+    pDocView->priv->m_pDocument = pDocView->priv->m_pOffice->pClass->documentLoad( pDocView->priv->m_pOffice, pPath );
     if ( !pDocView->priv->m_pDocument )
     {
         // FIXME: should have a GError parameter and populate it.
-- 
2.12.0