Fixes #79835, implements PrinterSettings.IsValid, throws proper
authorAndreia Gaita <avidigal@novell.com>
Tue, 14 Nov 2006 23:03:43 +0000 (23:03 -0000)
committerAndreia Gaita <avidigal@novell.com>
Tue, 14 Nov 2006 23:03:43 +0000 (23:03 -0000)
InvalidPrinterException with differentiated messages.

* PrintingServices.cs: Declares IsPrinterValid to enable easy checking
if printer name is a valid printer

* PrintingServicesWin32.cs: Implements IsPrinterValid, caches
last assigned printer name and if it is valid to speed up next calls.

* PrintingServicesUnix.cs: Implements IsPrinterValid, caches
last assigned printer name and if it is valid to speed up next calls.

* PageSettings.cs:
- throw InvalidPrinterException in the properties,
according to the docs: Color, Landscape, PaperSize, PaperSource,
PrinterResolution
- change the private fields above to internal, so that in the
constructor we can access them without triggering the exceptions
if there are no printers installed

* PrinterSettings.cs: Implement IsValid to call IsPrinterValid
implemented above

* PreviewPrintController.cs: throw InvalidPrinterException on
StartPrint() if there's no valid printer configured

* InvalidPrinterException.cs: New private GetMessage() to customize
printer error message, used by constructor

2006-11-14 Andreia Gaita <avidigal@novell.com>

svn path=/trunk/mcs/; revision=67880

mcs/class/System.Drawing/System.Drawing.Printing/ChangeLog
mcs/class/System.Drawing/System.Drawing.Printing/InvalidPrinterException.cs
mcs/class/System.Drawing/System.Drawing.Printing/PageSettings.cs
mcs/class/System.Drawing/System.Drawing.Printing/PreviewPrintController.cs
mcs/class/System.Drawing/System.Drawing.Printing/PrinterSettings.cs
mcs/class/System.Drawing/System.Drawing.Printing/PrintingServices.cs
mcs/class/System.Drawing/System.Drawing.Printing/PrintingServicesUnix.cs
mcs/class/System.Drawing/System.Drawing.Printing/PrintingServicesWin32.cs

index f0f63900f3f8ead515dc2791cab207c5beb60b9e..b606b0cf45ef97a115c587c8cfba5044d15f30d1 100644 (file)
@@ -1,3 +1,33 @@
+2006-11-14 Andreia Gaita <avidigal@novell.com>
+       Fixes #79835, implements PrinterSettings.IsValid, throws proper
+       InvalidPrinterException with differentiated messages.
+       
+       * PrintingServices.cs: Declares IsPrinterValid to enable easy checking
+       if printer name is a valid printer
+       
+       * PrintingServicesWin32.cs: Implements IsPrinterValid, caches 
+       last assigned printer name and if it is valid to speed up next calls.
+       
+       * PrintingServicesUnix.cs: Implements IsPrinterValid, caches 
+       last assigned printer name and if it is valid to speed up next calls.
+       
+       * PageSettings.cs: 
+               - throw InvalidPrinterException in the properties,
+               according to the docs: Color, Landscape, PaperSize, PaperSource, 
+               PrinterResolution
+               - change the private fields above to internal, so that in the
+               constructor we can access them without triggering the exceptions
+               if there are no printers installed
+               
+       * PrinterSettings.cs: Implement IsValid to call IsPrinterValid
+       implemented above
+       
+       * PreviewPrintController.cs: throw InvalidPrinterException on 
+       StartPrint() if there's no valid printer configured
+       
+       * InvalidPrinterException.cs: New private GetMessage() to customize
+       printer error message, used by constructor
+
 2006-09-27 Jordi Mas i Hernandez <jordimash@gmail.com>
 
        * PrintingServicesUnix.cs: Implements LoadPrinterPaperSources member,
index 9ee8b733b7fe4af6c3d6896862c95b30c5a9f7a9..4134f39c7e3dfda83eb05231c989e8ae43461768 100644 (file)
@@ -42,7 +42,7 @@ namespace System.Drawing.Printing {
 
                private PrinterSettings settings;
 
-               public InvalidPrinterException (PrinterSettings settings)
+               public InvalidPrinterException (PrinterSettings settings): base(InvalidPrinterException.GetMessage(settings))
                {
                        this.settings = settings;
                }
@@ -60,5 +60,12 @@ namespace System.Drawing.Printing {
 
                        base.GetObjectData (info, context);
                }
+               
+               private static string GetMessage(PrinterSettings settings)
+               {
+                       if (settings.PrinterName == null || settings.PrinterName == String.Empty)
+                               return "No Printers Installed";
+                       return String.Format("Tried to access printer '{0}' with invalid settings.", settings.PrinterName);                             
+               }
        }
 }
index c962f00d8bb7d7c4c53422bef0ef38e4e08ecf3d..8032033f820da68a40bfd852908907dafece9db6 100644 (file)
@@ -44,16 +44,16 @@ namespace System.Drawing.Printing
 #endif
        public class PageSettings : ICloneable
        {
-               bool _Color;
-               bool _Landscape;
+               internal bool _Color;
+               internal bool _Landscape;
+               internal PaperSize _PaperSize;
+               internal PaperSource _PaperSource;
+               internal PrinterResolution _PrinterResolution;
                float _HardMarginX;\r
                float _HardMarginY;
                RectangleF _PrintableArea;
                // create a new default Margins object (is 1 inch for all margins)
                Margins _Margins = new Margins();
-               PaperSize _PaperSize;
-               PaperSource _PaperSource;
-               PrinterResolution _PrinterResolution;
                PrinterSettings _PrinterSettings;
                
                public PageSettings() : this(new PrinterSettings())
@@ -64,11 +64,11 @@ namespace System.Drawing.Printing
                {
                        PrinterSettings = printerSettings;
                        
-                       Color = printerSettings.DefaultPageSettings.Color;
-                       Landscape = printerSettings.DefaultPageSettings.Landscape;
-                       PaperSize = printerSettings.DefaultPageSettings.PaperSize;
-                       PaperSource = printerSettings.DefaultPageSettings.PaperSource;
-                       PrinterResolution = printerSettings.DefaultPageSettings.PrinterResolution;
+                       Color = printerSettings.DefaultPageSettings._Color;
+                       Landscape = printerSettings.DefaultPageSettings._Landscape;
+                       PaperSize = printerSettings.DefaultPageSettings._PaperSize;
+                       PaperSource = printerSettings.DefaultPageSettings._PaperSource;
+                       PrinterResolution = printerSettings.DefaultPageSettings._PrinterResolution;
                }
                
                // used by PrinterSettings.DefaultPageSettings
@@ -76,11 +76,11 @@ namespace System.Drawing.Printing
                {
                        PrinterSettings = printerSettings;
                        
-                       Color = color;
-                       Landscape = landscape;
-                       PaperSize = paperSize;
-                       PaperSource = paperSource;
-                       PrinterResolution = printerResolution;
+                       _Color = color;
+                       _Landscape = landscape;
+                       _PaperSize = paperSize;
+                       _PaperSource = paperSource;
+                       _PrinterResolution = printerResolution;
                }
 
                //props
@@ -104,6 +104,8 @@ namespace System.Drawing.Printing
                
                public bool Color{
                        get{
+                               if (!this._PrinterSettings.IsValid)
+                                       throw new InvalidPrinterException(this._PrinterSettings);
                                return _Color;
                        }
                        set{
@@ -113,6 +115,8 @@ namespace System.Drawing.Printing
                
                public bool Landscape {
                        get{
+                               if (!this._PrinterSettings.IsValid)
+                                       throw new InvalidPrinterException(this._PrinterSettings);
                                return _Landscape;
                        }
                        set{
@@ -122,6 +126,8 @@ namespace System.Drawing.Printing
                
                public Margins Margins{
                        get{
+                               if (!this._PrinterSettings.IsValid)
+                                       throw new InvalidPrinterException(this._PrinterSettings);
                                return _Margins;
                        }
                        set{
@@ -131,6 +137,8 @@ namespace System.Drawing.Printing
                
                public PaperSize PaperSize{
                        get{
+                               if (!this._PrinterSettings.IsValid)
+                                       throw new InvalidPrinterException(this._PrinterSettings);
                                return _PaperSize;
                        }
                        set{
@@ -140,6 +148,8 @@ namespace System.Drawing.Printing
                
                public PaperSource PaperSource{
                        get{
+                               if (!this._PrinterSettings.IsValid)
+                                       throw new InvalidPrinterException(this._PrinterSettings);
                                return _PaperSource;
                        }
                        set{
@@ -149,6 +159,8 @@ namespace System.Drawing.Printing
                
                public PrinterResolution PrinterResolution{
                        get{
+                               if (!this._PrinterSettings.IsValid)
+                                       throw new InvalidPrinterException(this._PrinterSettings);
                                return _PrinterResolution;
                        }
                        set{
index 8b5735fce918dde9908b1d6aa88c280cb25603df..2d8e312ec72e3036971a46b03abb22e77fcb8366 100644 (file)
@@ -60,6 +60,9 @@ namespace System.Drawing.Printing
                [MonoTODO]
                public override void OnStartPrint(PrintDocument document, PrintEventArgs e)
                {
+                       if (document.PrinterSettings.IsValid)
+                               throw new InvalidPrinterException(document.PrinterSettings);
+               
                        /* maybe we should reuse the images, and clear them? */
                        foreach (PreviewPageInfo pi in pageInfoList)
                                pi.Image.Dispose ();
index 5011d147a16261fbfaa2be4fe1961b3575818d0e..daaa97f69b01dcde55ad7849baa3d24f8e7d7e0f 100644 (file)
@@ -340,10 +340,9 @@ namespace System.Drawing.Printing
                        get { return false; }
                }
 
-               [MonoTODO("PrinterSettings.IsValid")]
                public bool IsValid
                {
-                       get { return true; }
+                       get { return SysPrn.Service.IsPrinterValid(this.printer_name, false); }
                }
                
                public int LandscapeAngle
@@ -382,6 +381,8 @@ namespace System.Drawing.Printing
                public PrinterSettings.PaperSizeCollection PaperSizes
                {
                        get {
+                               if (!this.IsValid)
+                                       throw new InvalidPrinterException(this);
                                if (paper_sizes == null) {
                                        paper_sizes = new PrinterSettings.PaperSizeCollection (new PaperSize [] {});
                                        SysPrn.Service.LoadPrinterPaperSizes (printer_name, this);
@@ -393,6 +394,8 @@ namespace System.Drawing.Printing
                public PrinterSettings.PaperSourceCollection PaperSources
                {
                        get {
+                               if (!this.IsValid)
+                                       throw new InvalidPrinterException(this);
                                if (paper_sources == null) {
                                        paper_sources = new PrinterSettings.PaperSourceCollection (new PaperSource [] {});
                                        SysPrn.Service.LoadPrinterPaperSources (printer_name, this);
@@ -425,6 +428,8 @@ namespace System.Drawing.Printing
                public PrinterSettings.PrinterResolutionCollection PrinterResolutions
                {
                        get {
+                               if (!this.IsValid)
+                                       throw new InvalidPrinterException(this);
                                if (printer_resolutions == null) {
                                        printer_resolutions = new PrinterSettings.PrinterResolutionCollection (new PrinterResolution[] {});
                                        SysPrn.Service.LoadPrinterResolutions (printer_name, this);
index c8c51120dbf535237837c7c14b4fc0dfc1b7dfc2..d809f15767d31921ff5f14fd5294dd481413bb77 100644 (file)
@@ -51,6 +51,7 @@ namespace System.Drawing.Printing
                internal abstract void LoadPrinterResolutions (string printer, PrinterSettings settings);
                internal abstract void LoadPrinterPaperSizes (string printer, PrinterSettings settings);
                internal abstract void LoadPrinterPaperSources (string printer, PrinterSettings settings);
+               internal abstract bool IsPrinterValid(string printer, bool force);
 
                //Used from SWF
                internal abstract void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment);
index 09ec7d2a63a8df626b9c4195bb425001c5497ca2..3fbdc918139c3cdac7a1dae131e8ba00ca4e68ba 100644 (file)
@@ -39,6 +39,8 @@ namespace System.Drawing.Printing
        {
                private Hashtable doc_info = new Hashtable ();
                private bool cups_installed;
+               private string printer_name;
+               private bool is_printer_valid;
 
                internal PrintingServicesUnix ()
                {
@@ -58,6 +60,21 @@ namespace System.Drawing.Printing
 
                        cups_installed = true;
                }
+               
+               internal override bool IsPrinterValid(string printer, bool force)
+               {
+                       if (!cups_installed || printer == null | printer == String.Empty)
+                               return false;
+
+                       if (!force && this.printer_name != null && String.Intern(this.printer_name).Equals(printer))
+                               return is_printer_valid;
+
+                       IntPtr ptr = cupsGetPPD (printer);
+                       string ppd_filename = Marshal.PtrToStringAnsi (ptr);
+                       is_printer_valid = ppd_filename != null;
+                       this.printer_name = printer; 
+                       return is_printer_valid;
+               }
 
                // Methods
                internal override void LoadPrinterSettings (string printer, PrinterSettings settings)
index 172e22eaba76c5aa6e50034351dbb12cd27ed6ce..ed5b045653a8090b13017db696bbe0e8e74b50b0 100644 (file)
@@ -35,11 +35,28 @@ namespace System.Drawing.Printing
 {
        internal class PrintingServicesWin32 : PrintingServices
        {
+               private string printer_name;
+               private bool is_printer_valid;
+
                internal PrintingServicesWin32 ()
                {
 
                }
 
+               internal override bool IsPrinterValid(string printer, bool force)
+               {
+                       if (printer == null | printer == String.Empty)
+                               return false;
+
+                       if (!force && this.printer_name != null && String.Intern(this.printer_name).Equals(printer))
+                               return is_printer_valid;
+
+                       int ret = Win32DocumentProperties (IntPtr.Zero, IntPtr.Zero, printer, IntPtr.Zero, IntPtr.Zero, 0);
+                       is_printer_valid = (ret < 1);
+                       this.printer_name = printer; 
+                       return is_printer_valid;
+               }
+
                internal override void LoadPrinterSettings (string printer, PrinterSettings settings)
                {
                        int ret;