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

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