Blame SOURCES/0392-lokdocview-Handle-password-protected-documents.patch

135360
From fe80bd09aa8bbe1e190d91c7f85c2a8b977c1dd1 Mon Sep 17 00:00:00 2001
135360
From: Pranav Kant <pranavk@collabora.co.uk>
135360
Date: Wed, 27 Jan 2016 16:56:14 +0530
135360
Subject: [PATCH 392/398] lokdocview: Handle password protected documents
135360
MIME-Version: 1.0
135360
Content-Type: text/plain; charset=UTF-8
135360
Content-Transfer-Encoding: 8bit
135360
135360
Reviewed-on: https://gerrit.libreoffice.org/21861
135360
Tested-by: Jenkins <ci@libreoffice.org>
135360
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
135360
Tested-by: Caolán McNamara <caolanm@redhat.com>
135360
(cherry picked from commit 18fbddcca569c109ca2f46f7d791187e672d4d83)
135360
Signed-off-by: Michael Stahl <mstahl@redhat.com>
135360
135360
Change-Id: I606a1112c8eb4c1cc4596d6947ce1223543cc87c
135360
(cherry picked from commit 89a3c1b4d69aac447606dc6f0e661b2ef4dddc8c)
135360
---
135360
 include/LibreOfficeKit/LibreOfficeKitGtk.h         |  12 +++
135360
 .../qa/gtktiledviewer/gtktiledviewer.cxx           |  50 ++++++++++
135360
 libreofficekit/source/gtk/lokdocview.cxx           | 109 ++++++++++++++++++++-
135360
 3 files changed, 166 insertions(+), 5 deletions(-)
135360
135360
diff --git a/include/LibreOfficeKit/LibreOfficeKitGtk.h b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
index e06d154f772e..1df27c106214 100644
135360
--- a/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
+++ b/include/LibreOfficeKit/LibreOfficeKitGtk.h
135360
@@ -275,6 +275,18 @@ gboolean                        lok_doc_view_paste                 (LOKDocView*
135360
                                                                     gsize nSize);
135360
 
135360
 /**
135360
+ * lok_doc_view_set_document_password:
135360
+ * @pDocView: The #LOKDocView instance
135360
+ * @pUrl: the URL of the document to set password for, as sent with signal `password-required`
135360
+ * @pPassword: (nullable): the password, NULL for no password
135360
+ *
135360
+ * Set the password for password protected documents
135360
+ */
135360
+void                            lok_doc_view_set_document_password (LOKDocView* pDocView,
135360
+                                                                    const gchar* pURL,
135360
+                                                                    const gchar* pPassword);
135360
+
135360
+/**
135360
  * lok_doc_view_pixel_to_twip:
135360
  * @pDocView: The #LOKDocView instance
135360
  * @fInput: The value in pixels to convert to twips
135360
diff --git a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
index a21daf199fa7..3dc9f246f18e 100644
135360
--- a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
+++ b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
135360
@@ -462,10 +462,20 @@ static void toggleFindbar(GtkWidget* pButton, gpointer /*pItem*/)
135360
     }
135360
 }
135360
 
135360
+static void
135360
+setLOKFeatures (GtkWidget* pDocView)
135360
+{
135360
+    g_object_set(G_OBJECT(pDocView),
135360
+                 "doc-password", TRUE,
135360
+                 "doc-password-to-modify", TRUE,
135360
+                 nullptr);
135360
+}
135360
+
135360
 /// Common initialization, regardless if it's just a new view or a full init.
135360
 static TiledWindow& setupWidgetAndCreateWindow(GtkWidget* pDocView)
135360
 {
135360
     setupDocView(pDocView);
135360
+    setLOKFeatures(pDocView);
135360
     TiledWindow aWindow;
135360
     aWindow.m_pDocView = pDocView;
135360
     GtkWidget* pWindow = createWindow(aWindow);
135360
@@ -797,6 +807,45 @@ static void formulaChanged(LOKDocView* pLOKDocView, char* pPayload, gpointer /*p
135360
     gtk_entry_set_text(GTK_ENTRY(rWindow.m_pFormulabarEntry), pPayload);
135360
 }
135360
 
135360
+/// LOKDocView password is requried to open the document
135360
+static void passwordRequired(LOKDocView* pLOKDocView, gchar* pUrl, gboolean bModify, gpointer /*pData*/)
135360
+{
135360
+    GtkWidget* pPasswordDialog = gtk_dialog_new_with_buttons ("Password required",
135360
+                                                              GTK_WINDOW (gtk_widget_get_toplevel(GTK_WIDGET(pLOKDocView))),
135360
+                                                              GTK_DIALOG_MODAL,
135360
+                                                              "OK",
135360
+                                                              GTK_RESPONSE_OK,
135360
+                                                              nullptr);
135360
+    g_object_set(G_OBJECT(pPasswordDialog), "resizable", FALSE, nullptr);
135360
+    GtkWidget* pDialogMessageArea = gtk_dialog_get_content_area (GTK_DIALOG (pPasswordDialog));
135360
+    GtkWidget* pPasswordEntry = gtk_entry_new ();
135360
+    gtk_entry_set_visibility (GTK_ENTRY(pPasswordEntry), FALSE);
135360
+    gtk_entry_set_invisible_char (GTK_ENTRY(pPasswordEntry), '*');
135360
+    gtk_box_pack_end(GTK_BOX(pDialogMessageArea), pPasswordEntry, TRUE, TRUE, 2);
135360
+    if (bModify)
135360
+    {
135360
+        GtkWidget* pSecondaryLabel = gtk_label_new ("Document requires password to edit");
135360
+        gtk_box_pack_end(GTK_BOX(pDialogMessageArea), pSecondaryLabel, TRUE, TRUE, 2);
135360
+        gtk_dialog_add_button (GTK_DIALOG (pPasswordDialog), "Open as read-only", GTK_RESPONSE_ACCEPT);
135360
+    }
135360
+    gtk_widget_show_all(pPasswordDialog);
135360
+
135360
+    gint res = gtk_dialog_run (GTK_DIALOG(pPasswordDialog));
135360
+    switch (res)
135360
+    {
135360
+    case GTK_RESPONSE_OK:
135360
+        lok_doc_view_set_document_password (pLOKDocView, pUrl, gtk_entry_get_text(GTK_ENTRY(pPasswordEntry)));
135360
+        break;
135360
+    case GTK_RESPONSE_ACCEPT:
135360
+        // User accepts to open this document as read-only
135360
+    case GTK_RESPONSE_DELETE_EVENT:
135360
+        lok_doc_view_set_document_password (pLOKDocView, pUrl, nullptr);
135360
+        break;
135360
+    }
135360
+
135360
+    gtk_widget_destroy(pPasswordDialog);
135360
+}
135360
+
135360
 static void toggleToolItem(GtkWidget* pWidget, gpointer /*pData*/)
135360
 {
135360
     TiledWindow& rWindow = lcl_getTiledWindow(pWidget);
135360
@@ -1254,6 +1303,7 @@ static void setupDocView(GtkWidget* pDocView)
135360
     g_signal_connect(pDocView, "hyperlink-clicked", G_CALLBACK(signalHyperlink), NULL);
135360
     g_signal_connect(pDocView, "cursor-changed", G_CALLBACK(cursorChanged), NULL);
135360
     g_signal_connect(pDocView, "formula-changed", G_CALLBACK(formulaChanged), NULL);
135360
+    g_signal_connect(pDocView, "password-required", G_CALLBACK(passwordRequired), nullptr);
135360
 }
135360
 
135360
 int main( int argc, char* argv[] )
135360
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
135360
index 960c8c1f2624..f0dd7342c1a3 100644
135360
--- a/libreofficekit/source/gtk/lokdocview.cxx
135360
+++ b/libreofficekit/source/gtk/lokdocview.cxx
135360
@@ -65,6 +65,8 @@ struct LOKDocViewPrivateImpl
135360
     glong m_nDocumentHeightTwips;
135360
     /// View or edit mode.
135360
     gboolean m_bEdit;
135360
+    /// LOK Features
135360
+    guint64 m_nLOKFeatures;
135360
     /// Position and size of the visible cursor.
135360
     GdkRectangle m_aVisibleCursor;
135360
     /// Cursor overlay is visible or hidden (for blinking).
135360
@@ -138,6 +140,7 @@ struct LOKDocViewPrivateImpl
135360
         m_nDocumentWidthTwips(0),
135360
         m_nDocumentHeightTwips(0),
135360
         m_bEdit(FALSE),
135360
+        m_nLOKFeatures(0),
135360
         m_aVisibleCursor({0, 0, 0, 0}),
135360
         m_bCursorOverlayVisible(false),
135360
         m_bCursorVisible(true),
135360
@@ -192,6 +195,7 @@ enum
135360
     COMMAND_RESULT,
135360
     FORMULA_CHANGED,
135360
     TEXT_SELECTION,
135360
+    PASSWORD_REQUIRED,
135360
 
135360
     LAST_SIGNAL
135360
 };
135360
@@ -212,6 +216,8 @@ enum
135360
     PROP_DOC_HEIGHT,
135360
     PROP_CAN_ZOOM_IN,
135360
     PROP_CAN_ZOOM_OUT,
135360
+    PROP_DOC_PASSWORD,
135360
+    PROP_DOC_PASSWORD_TO_MODIFY,
135360
 
135360
     PROP_LAST
135360
 };
135360
@@ -311,6 +317,10 @@ callbackTypeToString (int nType)
135360
         return "LOK_CALLBACK_SET_PART";
135360
     case LOK_CALLBACK_SEARCH_RESULT_SELECTION:
135360
         return "LOK_CALLBACK_SEARCH_RESULT_SELECTION";
135360
+    case LOK_CALLBACK_DOCUMENT_PASSWORD:
135360
+        return "LOK_CALLBACK_DOCUMENT_PASSWORD";
135360
+    case LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY:
135360
+        return "LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY";
135360
     }
135360
     return nullptr;
135360
 }
135360
@@ -783,6 +793,7 @@ globalCallback (gpointer pData)
135360
 {
135360
     CallbackData* pCallback = static_cast<CallbackData*>(pData);
135360
     LOKDocViewPrivate& priv = getPrivate(pCallback->m_pDocView);
135360
+    gboolean bModify = false;
135360
 
135360
     switch (pCallback->m_nType)
135360
     {
135360
@@ -804,12 +815,12 @@ globalCallback (gpointer pData)
135360
         g_signal_emit (pCallback->m_pDocView, doc_view_signals[LOAD_CHANGED], 0, 1.0);
135360
     }
135360
     break;
135360
-    case LOK_CALLBACK_DOCUMENT_PASSWORD:
135360
     case LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY:
135360
+        bModify = true;
135360
+    case LOK_CALLBACK_DOCUMENT_PASSWORD:
135360
     {
135360
         char const*const pURL(pCallback->m_aPayload.c_str());
135360
-        // TODO maybe allow more passwords
135360
-        priv->m_pOffice->pClass->setDocumentPassword(priv->m_pOffice, pURL, "1");
135360
+        g_signal_emit (pCallback->m_pDocView, doc_view_signals[PASSWORD_REQUIRED], 0, pURL, bModify);
135360
     }
135360
     break;
135360
     default:
135360
@@ -1910,6 +1921,8 @@ static void lok_doc_view_set_property (GObject* object, guint propId, const GVal
135360
 {
135360
     LOKDocView* pDocView = LOK_DOC_VIEW (object);
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
+    gboolean bDocPasswordEnabled = priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD;
135360
+    gboolean bDocPasswordToModifyEnabled = priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY;
135360
 
135360
     switch (propId)
135360
     {
135360
@@ -1937,6 +1950,20 @@ static void lok_doc_view_set_property (GObject* object, guint propId, const GVal
135360
     case PROP_DOC_HEIGHT:
135360
         priv->m_nDocumentHeightTwips = g_value_get_long (value);
135360
         break;
135360
+    case PROP_DOC_PASSWORD:
135360
+        if (g_value_get_boolean (value) != bDocPasswordEnabled)
135360
+        {
135360
+            priv->m_nLOKFeatures = priv->m_nLOKFeatures ^ LOK_FEATURE_DOCUMENT_PASSWORD;
135360
+            priv->m_pOffice->pClass->setOptionalFeatures(priv->m_pOffice, priv->m_nLOKFeatures);
135360
+        }
135360
+        break;
135360
+    case PROP_DOC_PASSWORD_TO_MODIFY:
135360
+        if ( g_value_get_boolean (value) != bDocPasswordToModifyEnabled)
135360
+        {
135360
+            priv->m_nLOKFeatures = priv->m_nLOKFeatures ^ LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY;
135360
+            priv->m_pOffice->pClass->setOptionalFeatures(priv->m_pOffice, priv->m_nLOKFeatures);
135360
+        }
135360
+        break;
135360
     default:
135360
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propId, pspec);
135360
     }
135360
@@ -1985,6 +2012,12 @@ static void lok_doc_view_get_property (GObject* object, guint propId, GValue *va
135360
     case PROP_CAN_ZOOM_OUT:
135360
         g_value_set_boolean (value, priv->m_bCanZoomOut);
135360
         break;
135360
+    case PROP_DOC_PASSWORD:
135360
+        g_value_set_boolean (value, priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD);
135360
+        break;
135360
+    case PROP_DOC_PASSWORD_TO_MODIFY:
135360
+        g_value_set_boolean (value, priv->m_nLOKFeatures & LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY);
135360
+        break;
135360
     default:
135360
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propId, pspec);
135360
     }
135360
@@ -2034,8 +2067,6 @@ static gboolean lok_doc_view_initable_init (GInitable *initable, GCancellable* /
135360
         return FALSE;
135360
     }
135360
 
135360
-//    priv->m_pOffice->pClass->setOptionalFeatures(priv->m_pOffice, LOK_FEATURE_DOCUMENT_PASSWORD|LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY);
135360
-
135360
     return TRUE;
135360
 }
135360
 
135360
@@ -2223,6 +2254,33 @@ static void lok_doc_view_class_init (LOKDocViewClass* pClass)
135360
                              static_cast<GParamFlags>(G_PARAM_READABLE
135360
                                                       | G_PARAM_STATIC_STRINGS));
135360
 
135360
+    /**
135360
+     * LOKDocView:doc-password:
135360
+     *
135360
+     * Set it to true if client supports providing password for viewing
135360
+     * password protected documents
135360
+     */
135360
+    properties[PROP_DOC_PASSWORD] =
135360
+        g_param_spec_boolean("doc-password",
135360
+                             "Document password capability",
135360
+                             "Whether client supports providing document passwords",
135360
+                             FALSE,
135360
+                             static_cast<GParamFlags>(G_PARAM_READWRITE
135360
+                                                      | G_PARAM_STATIC_STRINGS));
135360
+
135360
+    /**
135360
+     * LOKDocView:doc-password-to-modify:
135360
+     *
135360
+     * Set it to true if client supports providing password for edit-protected documents
135360
+     */
135360
+    properties[PROP_DOC_PASSWORD_TO_MODIFY] =
135360
+        g_param_spec_boolean("doc-password-to-modify",
135360
+                             "Edit document password capability",
135360
+                             "Whether the client supports providing passwords to edit documents",
135360
+                             FALSE,
135360
+                             static_cast<GParamFlags>(G_PARAM_READWRITE
135360
+                                                      | G_PARAM_STATIC_STRINGS));
135360
+
135360
     g_object_class_install_properties(pGObjectClass, PROP_LAST, properties);
135360
 
135360
     /**
135360
@@ -2409,6 +2467,37 @@ static void lok_doc_view_class_init (LOKDocViewClass* pClass)
135360
                      g_cclosure_marshal_VOID__BOOLEAN,
135360
                      G_TYPE_NONE, 1,
135360
                      G_TYPE_BOOLEAN);
135360
+
135360
+    /**
135360
+     * LOKDocView::password-required:
135360
+     * @pDocView: the #LOKDocView on which the signal is emitted
135360
+     * @pUrl: URL of the document for which password is required
135360
+     * @bModify: whether password id required to modify the document
135360
+     * This is true when password is required to edit the document,
135360
+     * while it can still be viewed without password. In such cases, provide a NULL
135360
+     * password for read-only access to the document.
135360
+     * If false, password is required for opening the document, and document
135360
+     * cannot be opened without providing a valid password.
135360
+     *
135360
+     * Password must be provided by calling lok_doc_view_set_document_password
135360
+     * function with pUrl as provided by the callback.
135360
+     *
135360
+     * Upon entering a invalid password, another `password-required` signal is
135360
+     * emitted.
135360
+     * Upon entering a valid password, document starts to load.
135360
+     * Upon entering a NULL password: if bModify is %TRUE, document starts to
135360
+     * open in view-only mode, else loading of document is aborted.
135360
+     */
135360
+    doc_view_signals[PASSWORD_REQUIRED] =
135360
+        g_signal_new("password-required",
135360
+                     G_TYPE_FROM_CLASS(pGObjectClass),
135360
+                     G_SIGNAL_RUN_FIRST,
135360
+                     0,
135360
+                     nullptr, nullptr,
135360
+                     g_cclosure_marshal_generic,
135360
+                     G_TYPE_NONE, 2,
135360
+                     G_TYPE_STRING,
135360
+                     G_TYPE_BOOLEAN);
135360
 }
135360
 
135360
 SAL_DLLPUBLIC_EXPORT GtkWidget*
135360
@@ -2786,6 +2875,16 @@ lok_doc_view_paste (LOKDocView* pDocView,
135360
     return ret;
135360
 }
135360
 
135360
+SAL_DLLPUBLIC_EXPORT void
135360
+lok_doc_view_set_document_password (LOKDocView* pDocView,
135360
+                                    const gchar* pURL,
135360
+                                    const gchar* pPassword)
135360
+{
135360
+    LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
+
135360
+    priv->m_pOffice->pClass->setDocumentPassword(priv->m_pOffice, pURL, pPassword);
135360
+}
135360
+
135360
 SAL_DLLPUBLIC_EXPORT gfloat
135360
 lok_doc_view_pixel_to_twip (LOKDocView* pDocView, float fInput)
135360
 {
135360
-- 
135360
2.12.0
135360