[pdfmod] Refactor file chooser code



commit 67573f5d091e1f0a847848c774a0de5986a58d60
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Tue Mar 30 13:42:21 2010 -0700

    Refactor file chooser code
    
    Also, add Documents and Download XDG directories to the shortcuts list
    in the chooser dialogs.

 src/PdfMod/Gui/Actions.cs |   35 +++++++++++------------------------
 src/PdfMod/Gui/Client.cs  |   24 +++++++++++++++++++++---
 2 files changed, 32 insertions(+), 27 deletions(-)
---
diff --git a/src/PdfMod/Gui/Actions.cs b/src/PdfMod/Gui/Actions.cs
index 3922680..fb8a8b1 100644
--- a/src/PdfMod/Gui/Actions.cs
+++ b/src/PdfMod/Gui/Actions.cs
@@ -1,4 +1,4 @@
-// Copyright (C) 2009 Novell, Inc.
+// Copyright (C) 2009-2010 Novell, Inc.
 // Copyright (C) 2009 Julien Rebetez
 // Copyright (C) 2009 Å?ukasz JernaÅ?
 // Copyright (C) 2009 Andreu Correa Casablanca
@@ -200,13 +200,8 @@ namespace PdfMod.Gui
 
         void OnOpen (object o, EventArgs args)
         {
-            var chooser = new Gtk.FileChooserDialog (Catalog.GetString ("Select PDF"), app.Window, FileChooserAction.Open) {
-                SelectMultiple = true,
-                DefaultResponse = ResponseType.Ok
-            };
-            chooser.AddFilter (GtkUtilities.GetFileFilter (Catalog.GetString ("PDF Documents"), new string [] {"pdf"}));
-            chooser.AddFilter (GtkUtilities.GetFileFilter (Catalog.GetString ("All Files"), new string [] {"*"}));
-            chooser.AddButton (Stock.Cancel, ResponseType.Cancel);
+            var chooser = app.CreateChooser (Catalog.GetString ("Select PDF"), FileChooserAction.Open);
+            chooser.SelectMultiple = true;
             chooser.AddButton (Stock.Open, ResponseType.Ok);
 
             if (app.Document != null) {
@@ -246,15 +241,12 @@ namespace PdfMod.Gui
 
         void OnInsertFrom (object o, EventArgs args)
         {
-            var chooser = new Gtk.FileChooserDialog (Catalog.GetString ("Select PDF"), app.Window, FileChooserAction.Open) {
-                SelectMultiple = false,
-                DefaultResponse = ResponseType.Ok
-            };
-            chooser.AddFilter (GtkUtilities.GetFileFilter ("PDF Documents", new string [] {"pdf"}));
-            chooser.AddFilter (GtkUtilities.GetFileFilter (Catalog.GetString ("All Files"), new string [] {"*"}));
+            var chooser = app.CreateChooser (Catalog.GetString ("Select PDF"), FileChooserAction.Open);
+            chooser.SelectMultiple = false;
             chooser.SetCurrentFolder (System.IO.Path.GetDirectoryName (app.Document.SuggestedSavePath));
-            chooser.AddButton (Stock.Cancel, ResponseType.Cancel);
             chooser.AddButton (Stock.Open, ResponseType.Ok);
+            // TODO will uncomment this later; don't want to break string freeze now
+            //chooser.AddButton (Catalog.GetString ("_Insert"), ResponseType.Ok);
 
             var response = chooser.Run ();
             string filename = chooser.Filename;
@@ -281,16 +273,11 @@ namespace PdfMod.Gui
 
         void OnSaveAs (object o, EventArgs args)
         {
-            var chooser = new Gtk.FileChooserDialog (Catalog.GetString ("Save as..."), app.Window, FileChooserAction.Save) {
-                SelectMultiple = false,
-                DoOverwriteConfirmation = true,
-                CurrentName = System.IO.Path.GetFileName (app.Document.SuggestedSavePath),
-                DefaultResponse = ResponseType.Ok
-            };
-            chooser.AddButton (Stock.Cancel, ResponseType.Cancel);
+            var chooser = app.CreateChooser (Catalog.GetString ("Save as..."), FileChooserAction.Save);
+            chooser.SelectMultiple = false;
+            chooser.DoOverwriteConfirmation = true;
+            chooser.CurrentName = System.IO.Path.GetFileName (app.Document.SuggestedSavePath);
             chooser.AddButton (Stock.SaveAs, ResponseType.Ok);
-            chooser.AddFilter (GtkUtilities.GetFileFilter ("PDF Documents", new string [] {"pdf"}));
-            chooser.AddFilter (GtkUtilities.GetFileFilter (Catalog.GetString ("All Files"), new string [] {"*"}));
             chooser.SetCurrentFolder (System.IO.Path.GetDirectoryName (app.Document.SuggestedSavePath));
 
             var response = chooser.Run ();
diff --git a/src/PdfMod/Gui/Client.cs b/src/PdfMod/Gui/Client.cs
index 20edc07..77feaa2 100644
--- a/src/PdfMod/Gui/Client.cs
+++ b/src/PdfMod/Gui/Client.cs
@@ -1,4 +1,4 @@
-// Copyright (C) 2009 Novell, Inc.
+// Copyright (C) 2009-2010 Novell, Inc.
 // Copyright (C) 2009 Robert Dyer
 //
 // This program is free software; you can redistribute it and/or
@@ -16,6 +16,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 using System;
+using System.Linq;
 using System.Collections.Generic;
 using System.IO;
 
@@ -249,11 +250,11 @@ namespace PdfMod.Gui
 
                 loading = true;
             }
-            
+
             if (!path.StartsWith ("file://")) {
                 path = System.IO.Path.GetFullPath (path);
             }
-            
+
             Configuration.LastOpenFolder = System.IO.Path.GetDirectoryName (suggestedFilename ?? path);
             status_label.Text = Catalog.GetString ("Loading document...");
 
@@ -367,6 +368,23 @@ namespace PdfMod.Gui
             reset_event.WaitOne ();
         }
 
+        public Gtk.FileChooserDialog CreateChooser (string title, FileChooserAction action)
+        {
+            var chooser = new Gtk.FileChooserDialog (title, this.Window, action) {
+                DefaultResponse = ResponseType.Ok
+            };
+            chooser.AddButton (Stock.Cancel, ResponseType.Cancel);
+            chooser.AddFilter (GtkUtilities.GetFileFilter ("PDF Documents", new string [] {"pdf"}));
+            chooser.AddFilter (GtkUtilities.GetFileFilter (Catalog.GetString ("All Files"), new string [] {"*"}));
+
+            var dirs = new string [] { "DOWNLOAD", "DOCUMENTS" }.Select (s =>
+                XdgBaseDirectorySpec.GetXdgDirectoryUnderHome (String.Format ("XDG_{0}_DIR", s), null)
+            ).Where (d => d != null).ToArray ();
+            Hyena.Gui.GtkUtilities.SetChooserShortcuts (chooser, dirs);
+
+            return chooser;
+        }
+
         static void OnLogNotify (LogNotifyArgs args)
         {
             ThreadAssist.ProxyToMain (delegate {



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