135360
From 9a1d485f937bf92ae860078b94a5372b3a4ce718 Mon Sep 17 00:00:00 2001
135360
From: Pranav Kant <pranavk@gnome.org>
135360
Date: Thu, 18 Jun 2015 21:52:22 +0530
135360
Subject: [PATCH 055/398] lokdocview: Use GInitable
135360
135360
The construction of LokDocView widget can fail because it
135360
involves initializing the lok context via lok_init.
135360
135360
Having lok_init calls in constructed virtual method is a bad idea
135360
since it assumes that construction will never fail. So, implement
135360
GInitable for this class, and move the object initialization from
135360
constructed to initable.
135360
135360
Change-Id: Idf18a054cf8ef2e946392458ec52cb0107bd7454
135360
(cherry picked from commit a2aaf911e2e7b63af920186acb2a5e03bc58bd54)
135360
---
135360
 include/LibreOfficeKit/LibreOfficeKitGtk.h         |  6 ++-
135360
 .../qa/gtktiledviewer/gtktiledviewer.cxx           |  2 +-
135360
 libreofficekit/source/gtk/lokdocview.cxx           | 49 +++++++++++++++-------
135360
 3 files changed, 39 insertions(+), 18 deletions(-)
135360
135360
diff --git a/include/LibreOfficeKit/LibreOfficeKitGtk.h b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
index 7048dbefc0a1..3eaf28352a11 100644
135360
--- a/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
+++ b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
@@ -42,10 +42,12 @@ struct _LOKDocViewClass
135360
 
135360
 GType                          lok_doc_view_get_type               (void) G_GNUC_CONST;
135360
 
135360
-GtkWidget*                     lok_doc_view_new                    (const char* pPath);
135360
+GtkWidget*                     lok_doc_view_new                    (const gchar* pPath,
135360
+                                                                    GCancellable *cancellable,
135360
+                                                                    GError **error);
135360
 
135360
 gboolean                       lok_doc_view_open_document          (LOKDocView* pDocView,
135360
-                                                                    char* pPath);
135360
+                                                                    const gchar* pPath);
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 57556616d803..fedd6c9fdf13 100644
135360
--- a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
+++ b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
@@ -520,7 +520,7 @@ int main( int argc, char* argv[] )
135360
     gtk_box_pack_end(GTK_BOX(pVBox), pFindbar, FALSE, FALSE, 0);
135360
 
135360
     // Docview
135360
-    pDocView = lok_doc_view_new(argv[1]);
135360
+    pDocView = lok_doc_view_new (argv[1], NULL, NULL);
135360
     if (pDocView == NULL)
135360
         g_error ("Error while creating LOKDocView widget");
135360
     g_signal_connect(pDocView, "edit-changed", G_CALLBACK(signalEdit), NULL);
135360
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
135360
index 20b1aa6b837e..144f17efba94 100644
135360
--- a/libreofficekit/source/gtk/lokdocview.cxx
135360
+++ b/libreofficekit/source/gtk/lokdocview.cxx
135360
@@ -137,12 +137,16 @@ enum
135360
 
135360
 static guint doc_view_signals[LAST_SIGNAL] = { 0 };
135360
 
135360
+static void lok_doc_view_initable_iface_init (GInitableIface *iface);
135360
+
135360
 SAL_DLLPUBLIC_EXPORT GType lok_doc_view_get_type();
135360
 #ifdef __GNUC__
135360
 #pragma GCC diagnostic push
135360
 #pragma GCC diagnostic ignored "-Wunused-function"
135360
 #endif
135360
-G_DEFINE_TYPE_WITH_PRIVATE (LOKDocView, lok_doc_view, GTK_TYPE_DRAWING_AREA)
135360
+G_DEFINE_TYPE_WITH_CODE (LOKDocView, lok_doc_view, GTK_TYPE_DRAWING_AREA,
135360
+                         G_ADD_PRIVATE (LOKDocView)
135360
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, lok_doc_view_initable_iface_init));
135360
 #ifdef __GNUC__
135360
 #pragma GCC diagnostic pop
135360
 #endif
135360
@@ -1103,14 +1107,30 @@ static void lok_doc_view_finalize (GObject* object)
135360
     G_OBJECT_CLASS (lok_doc_view_parent_class)->finalize (object);
135360
 }
135360
 
135360
-static void lok_doc_view_constructed (GObject* object)
135360
+static gboolean lok_doc_view_initable_init (GInitable *initable, GCancellable* /*cancellable*/, GError **error)
135360
 {
135360
-    LOKDocView* pDocView = LOK_DOC_VIEW (object);
135360
-    LOKDocViewPrivate* priv = pDocView->priv;
135360
+    LOKDocView *pDocView = LOK_DOC_VIEW (initable);
135360
+
135360
+    if (pDocView->priv->m_pOffice != NULL)
135360
+        return TRUE;
135360
+
135360
+    pDocView->priv->m_pOffice = lok_init (pDocView->priv->m_aLOPath);
135360
+
135360
+    if (pDocView->priv->m_pOffice == NULL)
135360
+    {
135360
+        g_set_error (error,
135360
+                     g_quark_from_static_string ("LOK initialization error"), 0,
135360
+                     "Failed to get LibreOfficeKit context. Make sure path (%s) is correct",
135360
+                     pDocView->priv->m_aLOPath);
135360
+        return FALSE;
135360
+    }
135360
 
135360
-    G_OBJECT_CLASS (lok_doc_view_parent_class)->constructed (object);
135360
+    return TRUE;
135360
+}
135360
 
135360
-    pDocView->priv->m_pOffice = lok_init (priv->m_aLOPath);
135360
+static void lok_doc_view_initable_iface_init (GInitableIface *iface)
135360
+{
135360
+    iface->init = lok_doc_view_initable_init;
135360
 }
135360
 
135360
 static void lok_doc_view_class_init (LOKDocViewClass* pClass)
135360
@@ -1121,7 +1141,6 @@ static void lok_doc_view_class_init (LOKDocViewClass* pClass)
135360
     pGObjectClass->get_property = lok_doc_view_get_property;
135360
     pGObjectClass->set_property = lok_doc_view_set_property;
135360
     pGObjectClass->finalize = lok_doc_view_finalize;
135360
-    pGObjectClass->constructed = lok_doc_view_constructed;
135360
 
135360
     pWidgetClass->draw = lok_doc_view_draw;
135360
     pWidgetClass->button_press_event = lok_doc_view_signal_button;
135360
@@ -1343,18 +1362,19 @@ static void lok_doc_view_class_init (LOKDocViewClass* pClass)
135360
                      G_TYPE_STRING);
135360
 }
135360
 
135360
-
135360
-
135360
 /**
135360
  * lok_doc_view_new:
135360
  * @pPath: LibreOffice install path.
135360
+ * @cancellable: The cancellable object that you can use to cancel this
135360
+ * operation.
135360
+ * @error: The error that will be set if the object fails to initialize.
135360
  *
135360
- * Returns: The #LOKDocView widget instance.
135360
+ * Returns: (transfer none): The #LOKDocView widget instance.
135360
  */
135360
 SAL_DLLPUBLIC_EXPORT GtkWidget*
135360
-lok_doc_view_new (const char* pPath)
135360
+lok_doc_view_new (const gchar* pPath, GCancellable *cancellable, GError **error)
135360
 {
135360
-    return GTK_WIDGET (g_object_new(LOK_TYPE_DOC_VIEW, "lopath", pPath, NULL));
135360
+    return GTK_WIDGET (g_initable_new (LOK_TYPE_DOC_VIEW, cancellable, error, "lopath", pPath, NULL));
135360
 }
135360
 
135360
 /**
135360
@@ -1365,7 +1385,7 @@ lok_doc_view_new (const char* pPath)
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, char* pPath)
135360
+lok_doc_view_open_document (LOKDocView* pDocView, const gchar* pPath)
135360
 {
135360
     if ( pDocView->priv->m_pDocument )
135360
     {
135360
@@ -1374,8 +1394,7 @@ lok_doc_view_open_document (LOKDocView* pDocView, char* pPath)
135360
     }
135360
 
135360
     pDocView->priv->m_pOffice->pClass->registerCallback(pDocView->priv->m_pOffice, globalCallbackWorker, pDocView);
135360
-    pDocView->priv->m_pDocument = pDocView->priv->m_pOffice->pClass->documentLoad( pDocView->priv->m_pOffice,
135360
-                                                                   pPath );
135360
+    pDocView->priv->m_pDocument = pDocView->priv->m_pOffice->pClass->documentLoad( pDocView->priv->m_pOffice, pPath );
135360
     if ( !pDocView->priv->m_pDocument )
135360
     {
135360
         // FIXME: should have a GError parameter and populate it.
135360
-- 
135360
2.12.0
135360