narsil/Tanshu.Accounts.Print/PrinterLinux.cs
tanshu 49faa9786d Moved to a new printer name format in PrintLocation
If printing from windows, then the printer name should be prefixed with smb.
If printing from linux, then the printer name should be prefixed with cups.
If printing directly, then the printer name should be prefixed with pdl.
2018-05-17 15:52:27 +05:30

95 lines
5.7 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace Tanshu.Accounts.Print
{
internal class PrinterLinux : PlatformPrinter
{
private string location;
// ReSharper disable InconsistentNaming
#region API Declarations
enum http_status_t /**** HTTP status codes ****/
{
HTTP_ERROR = -1, /* An error response from httpXxxx() */
HTTP_CONTINUE = 100, /* Everything OK, keep going... */
HTTP_SWITCHING_PROTOCOLS, /* HTTP upgrade to TLS/SSL */
HTTP_OK = 200, /* OPTIONS/GET/HEAD/POST/TRACE command was successful */
HTTP_CREATED, /* PUT command was successful */
HTTP_ACCEPTED, /* DELETE command was successful */
HTTP_NOT_AUTHORITATIVE, /* Information isn't authoritative */
HTTP_NO_CONTENT, /* Successful command, no new data */
HTTP_RESET_CONTENT, /* Content was reset/recreated */
HTTP_PARTIAL_CONTENT, /* Only a partial file was recieved/sent */
HTTP_MULTIPLE_CHOICES = 300, /* Multiple files match request */
HTTP_MOVED_PERMANENTLY, /* Document has moved permanently */
HTTP_MOVED_TEMPORARILY, /* Document has moved temporarily */
HTTP_SEE_OTHER, /* See this other link... */
HTTP_NOT_MODIFIED, /* File not modified */
HTTP_USE_PROXY, /* Must use a proxy to access this URI */
HTTP_BAD_REQUEST = 400, /* Bad request */
HTTP_UNAUTHORIZED, /* Unauthorized to access host */
HTTP_PAYMENT_REQUIRED, /* Payment required */
HTTP_FORBIDDEN, /* Forbidden to access this URI */
HTTP_NOT_FOUND, /* URI was not found */
HTTP_METHOD_NOT_ALLOWED, /* Method is not allowed */
HTTP_NOT_ACCEPTABLE, /* Not Acceptable */
HTTP_PROXY_AUTHENTICATION, /* Proxy Authentication is Required */
HTTP_REQUEST_TIMEOUT, /* Request timed out */
HTTP_CONFLICT, /* Request is self-conflicting */
HTTP_GONE, /* Server has gone away */
HTTP_LENGTH_REQUIRED, /* A content length or encoding is required */
HTTP_PRECONDITION, /* Precondition failed */
HTTP_REQUEST_TOO_LARGE, /* Request entity too large */
HTTP_URI_TOO_LONG, /* URI too long */
HTTP_UNSUPPORTED_MEDIATYPE, /* The requested media type is unsupported */
HTTP_REQUESTED_RANGE, /* The requested range is not satisfiable */
HTTP_EXPECTATION_FAILED, /* The expectation given in an Expect header field was not met */
HTTP_UPGRADE_REQUIRED = 426, /* Upgrade to SSL/TLS required */
HTTP_SERVER_ERROR = 500, /* Internal server error */
HTTP_NOT_IMPLEMENTED, /* Feature not implemented */
HTTP_BAD_GATEWAY, /* Bad gateway */
HTTP_SERVICE_UNAVAILABLE, /* Service is unavailable */
HTTP_GATEWAY_TIMEOUT, /* Gateway connection timed out */
HTTP_NOT_SUPPORTED, /* HTTP version not supported */
HTTP_AUTHORIZATION_CANCELED = 1000 /* User canceled authorization */
} ;
[DllImport("libcups", CharSet = CharSet.Ansi)]
static extern int cupsCreateJob(IntPtr 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(IntPtr 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(IntPtr 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(IntPtr http, string name);
// ipp_status_t cupsFinishDocument (http_t *http, const char *name);
const string CUPS_FORMAT_TEXT = "text/plain";
#endregion
internal PrinterLinux(string location)
{
this.location = location.Substring(5).Normalize();
}
internal override bool Print(string documentName, string printingText)
{
var cupsOption = new IntPtr(0);
var CUPS_HTTP_DEFAULT = new IntPtr(0);
var jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, location, documentName, 0, cupsOption);
if (jobId > 0)
{
cupsStartDocument(CUPS_HTTP_DEFAULT, location, jobId, documentName, CUPS_FORMAT_TEXT, 1);
cupsWriteRequestData(CUPS_HTTP_DEFAULT, printingText, printingText.Length);
cupsFinishDocument(CUPS_HTTP_DEFAULT, location);
}
return jobId > 0;
}
// ReSharper restore InconsistentNaming
}
}