[pdfmod] Support loading password-protected PDFs



commit 9cfd00339ced83e995d90a5832003d7c8693b3d1
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Thu Aug 6 19:48:03 2009 -0700

    Support loading password-protected PDFs

 src/PdfMod/PdfMod/Document.cs |   15 +++++++--------
 src/PdfMod/PdfMod/PdfMod.cs   |   39 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 10 deletions(-)
---
diff --git a/src/PdfMod/PdfMod/Document.cs b/src/PdfMod/PdfMod/Document.cs
index 5cda2fd..26343de 100644
--- a/src/PdfMod/PdfMod/Document.cs
+++ b/src/PdfMod/PdfMod/Document.cs
@@ -14,7 +14,6 @@ namespace PdfMod
     {
         private PdfDocument pdf_document;
         private List<Page> pages = new List<Page> ();
-        private string password;
         private string tmp_path;
         private string tmp_uri;
         internal string CurrentStateUri { get { return tmp_uri ?? Uri; } }
@@ -99,17 +98,19 @@ namespace PdfMod
             get { return System.IO.Path.GetFileName (SuggestedSavePath); }
         }
 
+        public string Password { get; set; }
+
         public event System.Action Changed;
         public event System.Action PagesMoved;
         public event Action<Page []> PagesRemoved;
         public event Action<int, Page []> PagesAdded;
         public event Action<Page []> PagesChanged;
 
-        public Document (string uri, string password) : this (uri, password, false)
+        public Document ()
         {
         }
 
-        public Document (string uri, string password, bool isAlreadyTmp)
+        public void Load (string uri, PdfPasswordProvider passwordProvider, bool isAlreadyTmp)
         {
             if (isAlreadyTmp) {
                 tmp_uri = new Uri (uri).AbsoluteUri;
@@ -120,9 +121,7 @@ namespace PdfMod
             Uri = uri_obj.AbsoluteUri;
             SuggestedSavePath = Path = uri_obj.LocalPath;
 
-            this.password = password;
-
-            pdf_document = PdfSharp.Pdf.IO.PdfReader.Open (Path, password, PdfDocumentOpenMode.Modify | PdfDocumentOpenMode.Import);
+            pdf_document = PdfSharp.Pdf.IO.PdfReader.Open (Path, PdfDocumentOpenMode.Modify | PdfDocumentOpenMode.Import, passwordProvider);
             for (int i = 0; i < pdf_document.PageCount; i++) {
                 var page = new Page (pdf_document.Pages[i]) {
                     Document = this,
@@ -158,7 +157,7 @@ namespace PdfMod
 
         public IEnumerable<Page> FindPagesMatching (string text)
         {
-            using (var doc = Poppler.Document.NewFromFile (tmp_uri ?? Uri, password ?? "")) {
+            using (var doc = Poppler.Document.NewFromFile (tmp_uri ?? Uri, Password ?? "")) {
                 for (int i = 0; i < doc.NPages; i++) {
                     using (var page = doc.GetPage (i)) {
                         var list = page.FindText (text);
@@ -299,7 +298,7 @@ namespace PdfMod
         private Poppler.Document PopplerDoc {
             get {
                 if (poppler_doc == null) {
-                    poppler_doc = Poppler.Document.NewFromFile (tmp_uri ?? Uri, password ?? "");
+                    poppler_doc = Poppler.Document.NewFromFile (tmp_uri ?? Uri, Password ?? "");
                 }
                 return poppler_doc;
             }
diff --git a/src/PdfMod/PdfMod/PdfMod.cs b/src/PdfMod/PdfMod/PdfMod.cs
index 6402619..b43244f 100644
--- a/src/PdfMod/PdfMod/PdfMod.cs
+++ b/src/PdfMod/PdfMod/PdfMod.cs
@@ -237,8 +237,8 @@ namespace PdfMod
 
             ThreadAssist.SpawnFromMain (delegate {
                 try {
-
-                    Document = new Document (path, null, suggestedFilename != null);
+                    Document = new Document ();
+                    Document.Load (path, PasswordProvider, suggestedFilename != null);
                     if (suggestedFilename != null) {
                         Document.SuggestedSavePath = suggestedFilename;
                     }
@@ -302,6 +302,41 @@ namespace PdfMod
             Window.Title = title == null ? filename : String.Format ("{0} ({1})", title, filename);
         }
 
+        public void PasswordProvider (PdfPasswordProviderArgs args)
+        {
+            var reset_event = new System.Threading.ManualResetEvent (false);
+            ThreadAssist.ProxyToMain (delegate {
+                Log.Debug ("Password requested to open document");
+                var dialog = new Hyena.Widgets.HigMessageDialog (
+                    Window, DialogFlags.Modal, MessageType.Question, ButtonsType.None,
+                    Catalog.GetString ("Document is Encrypted"),
+                    Catalog.GetString ("Enter the document's password to open it:")
+                );
+                dialog.Image = Gtk.IconTheme.Default.LoadIcon ("dialog-password", 48, 0);
+
+                var password_entry = new Entry ();
+                password_entry.Visibility = false;
+                password_entry.Show ();
+                dialog.LabelVBox.PackStart (password_entry, false, false, 12);
+
+                dialog.AddButton (Stock.Cancel, ResponseType.Cancel, false);
+                dialog.AddButton (Stock.Ok, ResponseType.Ok, true);
+
+                var response = (ResponseType)dialog.Run ();
+                string password = password_entry.Text;
+                dialog.Destroy ();
+
+                if (response == ResponseType.Ok) {
+                    args.Password = Document.Password = password;
+                } else {
+                    Log.Information ("Password dialog cancelled");
+                    args.Abort = true;
+                }
+                reset_event.Set ();
+            });
+            reset_event.WaitOne ();
+        }
+
         private static void OnLogNotify (LogNotifyArgs args)
         {
             ThreadAssist.ProxyToMain (delegate {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]