2011-07-12 07:00:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Print
|
|
|
|
|
{
|
|
|
|
|
internal class PrinterLinux : PlatformPrinter
|
|
|
|
|
{
|
|
|
|
|
// ReSharper disable InconsistentNaming
|
2011-07-12 08:47:55 +00:00
|
|
|
|
#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 */
|
|
|
|
|
} ;
|
2011-07-12 07:00:48 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
2011-07-12 08:47:55 +00:00
|
|
|
|
static extern int cupsCreateJob(IntPtr http, string name, string title, int num_options, IntPtr options);
|
2011-07-12 07:00:48 +00:00
|
|
|
|
// int cupsCreateJob (http_t *http, const char *name, const char *title, int num_options, cups_option_t *options);
|
|
|
|
|
|
|
|
|
|
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
2011-07-12 08:47:55 +00:00
|
|
|
|
static extern int cupsStartDocument(IntPtr http, string name, int job_id, string docname, string format, int last_document);
|
2011-07-12 07:00:48 +00:00
|
|
|
|
// 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)]
|
2011-07-12 08:47:55 +00:00
|
|
|
|
static extern int cupsWriteRequestData(IntPtr http, string buffer, int length);
|
2011-07-12 07:00:48 +00:00
|
|
|
|
// http_status_t cupsWriteRequestData (http_t *http, const char *buffer, size_t length);
|
|
|
|
|
|
|
|
|
|
[DllImport("libcups", CharSet = CharSet.Ansi)]
|
2011-07-12 08:47:55 +00:00
|
|
|
|
static extern int cupsFinishDocument(IntPtr http, string name);
|
2011-07-12 07:00:48 +00:00
|
|
|
|
// ipp_status_t cupsFinishDocument (http_t *http, const char *name);
|
2011-07-12 08:47:55 +00:00
|
|
|
|
|
|
|
|
|
const string CUPS_FORMAT_TEXT = "text/plain";
|
2011-07-12 07:00:48 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
internal override bool Print(string printerName, string documentName, string printingText)
|
|
|
|
|
{
|
|
|
|
|
var cupsOption = new IntPtr(0);
|
2011-07-12 08:47:55 +00:00
|
|
|
|
var CUPS_HTTP_DEFAULT = new IntPtr(0);
|
2011-07-12 07:00:48 +00:00
|
|
|
|
var jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, printerName, documentName, 0, cupsOption);
|
|
|
|
|
if (jobId > 0)
|
|
|
|
|
{
|
2011-07-12 08:47:55 +00:00
|
|
|
|
cupsStartDocument(CUPS_HTTP_DEFAULT, printerName, jobId, documentName, CUPS_FORMAT_TEXT, 1);
|
2011-07-12 07:00:48 +00:00
|
|
|
|
cupsWriteRequestData(CUPS_HTTP_DEFAULT, printingText, printingText.Length);
|
|
|
|
|
cupsFinishDocument(CUPS_HTTP_DEFAULT, printerName);
|
|
|
|
|
}
|
|
|
|
|
return jobId > 0;
|
|
|
|
|
}
|
2011-07-12 08:47:55 +00:00
|
|
|
|
// ReSharper restore InconsistentNaming
|
2011-07-12 07:00:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|