From 0eb5c7d9390a66a3dac7dae6ce6dfaf7d331edae Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 12 Jul 2011 12:30:48 +0530 Subject: [PATCH] Experimental Support for CUPS Printing, not working as of now. --- Tanshu.Accounts.PointOfSale/App.config | 2 +- .../Controllers/BillController.cs | 4 +- Tanshu.Accounts.Print/PlatformPrinter.cs | 8 ++++ Tanshu.Accounts.Print/PrinterLinux.cs | 47 +++++++++++++++++++ ...{RawPrinterHelper.cs => PrinterWindows.cs} | 37 +++++---------- .../Tanshu.Accounts.Print.csproj | 5 +- Tanshu.Accounts.Print/Thermal.cs | 16 ++----- Tanshu.Accounts.Print/ThermalPrinter.cs | 30 ++++++++++++ 8 files changed, 108 insertions(+), 41 deletions(-) create mode 100644 Tanshu.Accounts.Print/PlatformPrinter.cs create mode 100644 Tanshu.Accounts.Print/PrinterLinux.cs rename Tanshu.Accounts.Print/{RawPrinterHelper.cs => PrinterWindows.cs} (76%) create mode 100644 Tanshu.Accounts.Print/ThermalPrinter.cs diff --git a/Tanshu.Accounts.PointOfSale/App.config b/Tanshu.Accounts.PointOfSale/App.config index cce9d7f..73db7bc 100644 --- a/Tanshu.Accounts.PointOfSale/App.config +++ b/Tanshu.Accounts.PointOfSale/App.config @@ -1,7 +1,7 @@  - + diff --git a/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs b/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs index 4ed230b..4aaced5 100644 --- a/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs +++ b/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs @@ -456,13 +456,15 @@ namespace Tanshu.Accounts.PointOfSale _saleForm.ClearBill(_bill); } - public void FormLoad() + public SaleFormState FormLoad() { ClearBill(); if (_editVoucherID.HasValue) { LoadBill(_editVoucherID.Value); + return SaleFormState.Billing; } + return SaleFormState.Waiting; } internal void SettleBill() diff --git a/Tanshu.Accounts.Print/PlatformPrinter.cs b/Tanshu.Accounts.Print/PlatformPrinter.cs new file mode 100644 index 0000000..f1522f7 --- /dev/null +++ b/Tanshu.Accounts.Print/PlatformPrinter.cs @@ -0,0 +1,8 @@ +namespace Tanshu.Accounts.Print +{ + // Cut Code for the printer Alt-29, Alt-86, Alt-49 + internal abstract class PlatformPrinter + { + internal abstract bool Print(string printerName, string documentName, string printingText); + } +} diff --git a/Tanshu.Accounts.Print/PrinterLinux.cs b/Tanshu.Accounts.Print/PrinterLinux.cs new file mode 100644 index 0000000..c94f02d --- /dev/null +++ b/Tanshu.Accounts.Print/PrinterLinux.cs @@ -0,0 +1,47 @@ +using System; +using System.Runtime.InteropServices; + +namespace Tanshu.Accounts.Print +{ + internal class PrinterLinux : PlatformPrinter + { + #region API Declarations + // ReSharper disable InconsistentNaming + [DllImport("libcups", CharSet = CharSet.Ansi)] + static extern int cupsPrintFile(string printer, string filename, string title, int num_options, IntPtr options); + // int cupsPrintFile (const char *name, const char *filename, const char *title, int num_options, cups_option_t *options); + + [DllImport("libcups", CharSet = CharSet.Ansi)] + static extern int cupsCreateJob(string http, string name, string title, int num_options, IntPtr options); + // int cupsCreateJob (http_t *http, const char *name, const char *title, int num_options, cups_option_t *options); + + [DllImport("libcups", CharSet = CharSet.Ansi)] + static extern int cupsStartDocument(string http, string name, int job_id, string docname, string format, int last_document); + // http_status_t cupsStartDocument (http_t *http, const char *name, int job_id, const char *docname, const char *format, int last_document); + + [DllImport("libcups", CharSet = CharSet.Ansi)] + static extern int cupsWriteRequestData(string http, string buffer, int length); + // http_status_t cupsWriteRequestData (http_t *http, const char *buffer, size_t length); + + [DllImport("libcups", CharSet = CharSet.Ansi)] + static extern int cupsFinishDocument(string http, string name); + // ipp_status_t cupsFinishDocument (http_t *http, const char *name); + // ReSharper restore InconsistentNaming + #endregion + + internal override bool Print(string printerName, string documentName, string printingText) + { + var cupsOption = new IntPtr(0); + const string CUPS_HTTP_DEFAULT = "CUPS_HTTP_DEFAULT"; + var jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, printerName, documentName, 0, cupsOption); + if (jobId > 0) + { + const string format = "CUPS_FORMAT_TEXT"; + cupsStartDocument(CUPS_HTTP_DEFAULT, printerName, jobId, documentName, format, 1); + cupsWriteRequestData(CUPS_HTTP_DEFAULT, printingText, printingText.Length); + cupsFinishDocument(CUPS_HTTP_DEFAULT, printerName); + } + return jobId > 0; + } + } +} diff --git a/Tanshu.Accounts.Print/RawPrinterHelper.cs b/Tanshu.Accounts.Print/PrinterWindows.cs similarity index 76% rename from Tanshu.Accounts.Print/RawPrinterHelper.cs rename to Tanshu.Accounts.Print/PrinterWindows.cs index f74f64e..e635825 100644 --- a/Tanshu.Accounts.Print/RawPrinterHelper.cs +++ b/Tanshu.Accounts.Print/PrinterWindows.cs @@ -1,12 +1,9 @@ using System; -using System.IO; using System.Runtime.InteropServices; -using System.Collections.Generic; -using System.Text; namespace Tanshu.Accounts.Print { - internal class RawPrinterHelper + internal class PrinterWindows : PlatformPrinter { // Cut Code for the printer Alt-29, Alt-86, Alt-49 #region API Declarations @@ -43,35 +40,28 @@ namespace Tanshu.Accounts.Print internal static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten); #endregion - internal static bool PrintString(string printerName, string documentName, string printingText, char[] lineBreakChars) + internal override bool Print(string printerName, string documentName, string printingText) { - string[] PageText; IntPtr hPrinter = new IntPtr(0); // The printer handle. - Int32 dwError; // Last error -incase there was trouble. DOCINFOA di = new DOCINFOA(); // Describes your document(name, port, data type). - bool bSuccess = false; // Your success code. + var bSuccess = false; // Your success code. // Set up the DOCINFO structure. di.pDocName = documentName; di.pDataType = "RAW"; - PageText = printingText.Split(lineBreakChars ); - if (OpenPrinter(printerName.Normalize(), out hPrinter, System.IntPtr.Zero)) + if (OpenPrinter(printerName.Normalize(), out hPrinter, IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, di)) { - for (int i = 0; i < PageText.Length; i++) - { - if (StartPagePrinter(hPrinter)) - { - //Write your printer - specific bytes to the printer. - bSuccess = SendLine(PageText[i], hPrinter); - EndPagePrinter(hPrinter); - } - } + if (!StartPagePrinter(hPrinter)) + return false; + //Write your printer - specific bytes to the printer. + bSuccess = SendLine(printingText, hPrinter); + EndPagePrinter(hPrinter); EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); @@ -79,14 +69,13 @@ namespace Tanshu.Accounts.Print // If you did not succeed, GetLastError may give more information about why not. if (bSuccess == false) { - dwError = Marshal.GetLastWin32Error(); + Marshal.GetLastWin32Error(); } return bSuccess; }// SendBytesToPrinter() - + internal static bool SendLine(string szString, IntPtr hPrinter) { - bool bSuccess = false; Int32 dwWritten = 0; Int32 dwCount = szString.Length; // How many characters are in the string? @@ -95,9 +84,7 @@ namespace Tanshu.Accounts.Print // the string to ANSI text. // Write your bytes. - bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); - - return bSuccess; + return WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); } } } diff --git a/Tanshu.Accounts.Print/Tanshu.Accounts.Print.csproj b/Tanshu.Accounts.Print/Tanshu.Accounts.Print.csproj index 86829a8..7b378f6 100644 --- a/Tanshu.Accounts.Print/Tanshu.Accounts.Print.csproj +++ b/Tanshu.Accounts.Print/Tanshu.Accounts.Print.csproj @@ -78,7 +78,10 @@ - + + + + diff --git a/Tanshu.Accounts.Print/Thermal.cs b/Tanshu.Accounts.Print/Thermal.cs index c9531ab..0d30a27 100644 --- a/Tanshu.Accounts.Print/Thermal.cs +++ b/Tanshu.Accounts.Print/Thermal.cs @@ -239,19 +239,9 @@ namespace Tanshu.Accounts.Print MessageBox.Show(text); return true; #else - try - { - text += printer.CutCode; - if (!RawPrinterHelper.PrintString(printer.Printer, documentName, text, new[] { ';' })) - { - GC.Collect(); - MessageBox.Show("Error in PrintRAW Function. Please Report immediately"); - } - } - catch (Exception ex) - { - throw ex; - } + text += printer.CutCode; + if (!ThermalPrinter.Printer.Print(printer.Printer, documentName, text)) + MessageBox.Show("Error in PrintRAW Function. Please Report immediately"); return true; #endif } diff --git a/Tanshu.Accounts.Print/ThermalPrinter.cs b/Tanshu.Accounts.Print/ThermalPrinter.cs new file mode 100644 index 0000000..9384481 --- /dev/null +++ b/Tanshu.Accounts.Print/ThermalPrinter.cs @@ -0,0 +1,30 @@ +using System; +using System.Runtime.InteropServices; + +namespace Tanshu.Accounts.Print +{ + internal class ThermalPrinter + { + private static readonly bool _runningOnLinux; + internal static PlatformPrinter Printer; + static ThermalPrinter() + { + _runningOnLinux = RunningOnLinux(); + Printer = _runningOnLinux ? (PlatformPrinter)new PrinterLinux() : new PrinterWindows(); + } + + internal static bool RunningOnLinux() + { + var platform = (int)Environment.OSVersion.Platform; + if ((platform == 4) || (platform == 6) || (platform == 128)) + { + if (Environment.GetEnvironmentVariable("not_supported_MONO_MWF_USE_NEW_X11_BACKEND") != null || + Environment.GetEnvironmentVariable("MONO_MWF_MAC_FORCE_X11") != null) + { + return true; + } + } + return false; + } + } +}