summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladimir Glazounov <vg@openoffice.org>2006-05-24 11:03:19 +0000
committerVladimir Glazounov <vg@openoffice.org>2006-05-24 11:03:19 +0000
commit04b481b393f11b45fa64a8f39d5fa57f0be0266b (patch)
treea77274259d321d493c2cab6f9281fab398b42338
parent4ea0946c97da217a784e5812b6f4062c6785e9dd (diff)
INTEGRATION: CWS macosxcups (1.34.6); FILE MERGED
2006/05/13 18:17:43 obr 1.34.6.1: #i63376# Enabled CUPS printing code for MACOSX
-rw-r--r--psprint/source/printergfx/printerjob.cxx400
1 files changed, 2 insertions, 398 deletions
diff --git a/psprint/source/printergfx/printerjob.cxx b/psprint/source/printergfx/printerjob.cxx
index 05a55eed621c..c73d6c47d550 100644
--- a/psprint/source/printergfx/printerjob.cxx
+++ b/psprint/source/printergfx/printerjob.cxx
@@ -4,9 +4,9 @@
*
* $RCSfile: printerjob.cxx,v $
*
- * $Revision: 1.34 $
+ * $Revision: 1.35 $
*
- * last change: $Author: rt $ $Date: 2006-05-04 08:41:55 $
+ * last change: $Author: vg $ $Date: 2006-05-24 12:03:19 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
@@ -82,19 +82,6 @@
using namespace psp;
using namespace rtl;
-#ifdef MACOSX
-// Prototype our MacOS X printing help function
-void macxp_ProcessAndPrintDocument( sal_Int32 applePrintSysType,
- char *spoolFileName,
- FILE *pDestFile,
- const rtl::OString psprintPrintCmd,
- const rtl::OString psprintDriverName,
- const JobData pJobData,
- const rtl::OUString *aJobName,
- sal_uInt32 jobDPI );
-int macxp_ConvertPSFileToPDF( char *psFileName, char *pdfFileName, int pdfFileNameBufferLen, sal_uInt32 jobDPI );
-#endif
-
// forward declaration
#define nBLOCKSIZE 0x2000
@@ -484,11 +471,6 @@ PrinterJob::StartJob (
sal_Bool
PrinterJob::EndJob ()
{
-#ifdef MACOSX
- sal_Int32 applePrintSysType;
- char *spoolFileName = NULL;
-#endif
-
// write document setup (done here because it
// includes the accumulated fonts
if( mpJobHeader )
@@ -515,11 +497,6 @@ PrinterJob::EndJob ()
FILE* pDestFILE = NULL;
-#ifdef MACOSX
- /* Ascertain the printing system */
- applePrintSysType = macxp_GetSystemPrintMethod();
-#endif
-
/* create a destination either as file or as a pipe */
sal_Bool bSpoolToFile = maFileName.getLength() > 0 ? sal_True : sal_False;
if (bSpoolToFile)
@@ -550,55 +527,10 @@ PrinterJob::EndJob ()
}
else
{
-#ifndef MACOSX
PrinterInfoManager& rPrinterInfoManager = PrinterInfoManager::get ();
pDestFILE = rPrinterInfoManager.startSpool( m_aLastJobData.m_aPrinterName );
if (pDestFILE == NULL)
return sal_False;
-#else
- const PrinterInfoManager& rPrinterInfoManager = PrinterInfoManager::get ();
- const rtl::OUString& rPrinter = m_aLastJobData.m_aPrinterName;
- const PrinterInfo& rPrinterInfo = rPrinterInfoManager.getPrinterInfo (rPrinter);
- const rtl::OUString& rCommand = rPrinterInfo.m_aCommand;
-
- const rtl::OString aShellCommand = rtl::OUStringToOString (rCommand,
- RTL_TEXTENCODING_ISO_8859_1);
-
- /* Mac OS X: open a pipe only if we are using (1) Darwin5 LPR printing.
- * All other OS X/Darwin cases use spool to file.
- * Other UNIX: always open a pipe.
- */
- if ( applePrintSysType == kApplePrintingLPR )
- {
- pDestFILE = popen (aShellCommand.getStr(), "w");
- if (pDestFILE == NULL)
- return sal_False;
- }
- else
- {
- /* Spool to file instead so we can convert it. Cases used here:
- * 1) kApplePrintingPrintCenter && kApplePrintingUsePDF
- * 2) kApplePrintingPrintCenter && kApplePrintingUsePS
- * 3) kApplePrintingCUPS && kApplePrintingUsePDF
- * 4) kApplePrintingCUPS && kApplePrintingUsePS
- */
-
- /* Get a temporary file name for the spool file.
- * This name must be free()ed later.
- */
- spoolFileName = tempnam( NULL, "ooopj" );
- if ( spoolFileName == NULL )
- return sal_False;
-
- /* Create temporary spool file */
- pDestFILE = fopen( spoolFileName, "w" );
- if ( pDestFILE == NULL )
- {
- free( spoolFileName );
- return sal_False;
- }
- }
-#endif
}
/* spool the document parts to the destination */
@@ -647,7 +579,6 @@ PrinterJob::EndJob ()
if (bSpoolToFile)
fclose (pDestFILE);
else
-#ifndef MACOSX
{
PrinterInfoManager& rPrinterInfoManager = PrinterInfoManager::get();
if (0 == rPrinterInfoManager.endSpool( m_aLastJobData.m_aPrinterName,
@@ -656,337 +587,10 @@ PrinterJob::EndJob ()
bSuccess = sal_False;
}
}
-#else
- {
- sal_uInt32 nXdpi;
- sal_uInt32 nYdpi;
-
- /* For Mac OS X/Darwin, process and spool the file to the printing subsystem
- * if required.
- */
- const rtl::OUString& rPrinter = m_aLastJobData.m_aPrinterName;
- const PrinterInfoManager& rPrinterInfoManager = PrinterInfoManager::get();
- const PrinterInfo& rPrinterInfo = rPrinterInfoManager.getPrinterInfo( rPrinter );
-
- // Grab printer's spool command
- const rtl::OUString& rCommand = rPrinterInfo.m_aCommand;
- const rtl::OString aShellCommand = rtl::OUStringToOString( rCommand, RTL_TEXTENCODING_ISO_8859_1 );
-
- // Grab the printer's PPD file name
- const rtl::OUString& rDriverName = rPrinterInfo.m_aDriverName;
- const rtl::OString aDriverName = rtl::OUStringToOString( rDriverName, RTL_TEXTENCODING_ISO_8859_1 );
-
- /* Grab the resolution for the job, for use in PS->PDF conversion */
-/* GetResolution( nXdpi, nYdpi );*/
-
- macxp_ProcessAndPrintDocument( applePrintSysType,
- spoolFileName,
- pDestFILE,
- aShellCommand,
- aDriverName,
- m_aLastJobData,
- &maJobTitle,
- mnResolution );
- }
- /* If we created a spool file for our Mac OS X/Darwin job,
- * delete it. Also free the variable, since it is malloc()ed in macxp_tempnam()
- * and otherwise we'd leak memory.
- */
- if ( spoolFileName )
- {
- unlink( spoolFileName );
- free( spoolFileName );
- }
-#endif
return bSuccess;
}
-#ifdef MACOSX
-// Because the printing on MacOS X and Darwin is a bit more complicated,
-// its got its own routine.
-
-#define kSysCommandBufferLen 1024
-#define kPDFFileNameBufferLen 1024
-
-void macxp_ProcessAndPrintDocument( sal_Int32 applePrintSysType,
- char *spoolFileName,
- FILE *pDestFile,
- const rtl::OString psprintPrintCmd,
- const rtl::OString psprintDriverName,
- const JobData pJobData,
- const rtl::OUString *aJobName,
- sal_uInt32 jobDPI )
-{
- char sysCommandBuffer[ kSysCommandBufferLen ];
- int printCmdErr;
- sal_Int32 printFormat;
-
- if ( applePrintSysType == kApplePrintingLPR )
- {
- /* Job is to be printed with Darwin5 lpr. It has already
- * been piped to lp/lpr above in EndJob(), so simply
- * close the pipe.
- */
- pclose( pDestFile );
- }
- else
- {
- /* Handle cases for MacOS X printing that don't pipe directly to lp/lpr */
-
- /* Close the intermediate file */
- fclose( pDestFile );
-
- /* Determine if we are going to print with PostScript, or with PS->PDF conversion.
- * We assume we are to use PS->PDF conversion unless proven otherwise. Most printers
- * will have a PPD, except Rendezvous-shared ones. If the PPD _doesn't_ have the
- * "cupsFilter" key, or if the "cupsFilter" key _doesn't_ contain "application/pdf",
- * then we print directly with PostScript.
- */
- printFormat = kApplePrintingUsePDF;
- if ( (sal_False == psprintDriverName.equals("SGENPRT")) && (sal_False == psprintDriverName.equals("MacShared")) )
- {
- /* The should exist a valid PPD for this printer */
- if ( pJobData.m_pParser )
- {
- const PPDKey *pCupsFilterKey;
- const PPDValue *pCupsFilterValue;
-
- pCupsFilterKey = pJobData.m_pParser->getKey( String(RTL_CONSTASCII_USTRINGPARAM("cupsFilter")) );
- pCupsFilterValue = pCupsFilterKey != NULL ? pJobData.m_aContext.getValue( pCupsFilterKey ) : NULL;
- if ( pCupsFilterValue )
- {
- ByteString aCupsFilterString( pCupsFilterValue->m_aOption, RTL_TEXTENCODING_ISO_8859_1 );
- if ( aCupsFilterString.Search("application/pdf") == 0 )
- {
- /* cupsFilter didn't have application/pdf */
- printFormat = kApplePrintingUsePS;
- }
- }
- else
- {
- /* No "cupsFilter" key */
- printFormat = kApplePrintingUsePS;
- }
- }
- }
-
- if ( (applePrintSysType==kApplePrintingPrintCenter) && (printFormat==kApplePrintingUsePS) )
- {
- /* Now pass the PS file to /usr/sbin/Print for printing. */
- snprintf( sysCommandBuffer, kSysCommandBufferLen-1, "%s -M ps \"%s\"", kApplePCPrintCommand, spoolFileName );
- #ifdef DEBUG
- fprintf( stderr, "printerjob.cxx: printing doc with command '%s'\n", sysCommandBuffer );
- #endif
-
- printCmdErr = system( sysCommandBuffer );
- }
- else if ( (applePrintSysType==kApplePrintingPrintCenter) && (printFormat==kApplePrintingUsePDF) )
- {
- char pdfFileName[ kPDFFileNameBufferLen ];
- char pdfFileNameMacFormat[ kPDFFileNameBufferLen ];
- char *c;
-
- /* Convert file to PDF using GhostScript */
- printCmdErr = macxp_ConvertPSFileToPDF( spoolFileName, pdfFileName, kPDFFileNameBufferLen, jobDPI );
- if ( printCmdErr == 0 )
- {
- /* AppleScript expects MacOS-style paths, so convert Unix path to Mac OS Style.
- * If the Unix path is absolute, get rid of the starting slash
- */
- strncpy( pdfFileNameMacFormat, pdfFileName, kPDFFileNameBufferLen-1 );
- if ( *pdfFileNameMacFormat == '/' )
- strncpy( pdfFileNameMacFormat, pdfFileName+1, kPDFFileNameBufferLen-2 );
- while ( (c=strchr(pdfFileNameMacFormat,'/')) != NULL )
- *c = ':';
-
- /* Construct and execute the actual printing command using AppleScript */
- snprintf( sysCommandBuffer, kSysCommandBufferLen-1, "/usr/bin/osascript -e 'tell application \"Finder\"' -e 'print {file \"%s\"}' -e 'end tell'", pdfFileNameMacFormat );
- #ifdef DEBUG
- fprintf( stderr, "printerjob.cxx: printing PDF with command '%s'\n", sysCommandBuffer );
- #endif
- printCmdErr = system( sysCommandBuffer );
- }
- unlink( pdfFileName );
- }
- else if ( (applePrintSysType==kApplePrintingCUPS) && ((printFormat==kApplePrintingUsePDF) || (printFormat==kApplePrintingUsePS)) )
- {
- char pdfFileName[ kPDFFileNameBufferLen ];
- char numCopiesSwitch[ 5 ];
- sal_uInt32 index;
-
- /* Convert file to PDF using GhostScript if using PS->PDF conversion */
- if ( printFormat == kApplePrintingUsePDF )
- printCmdErr = macxp_ConvertPSFileToPDF( spoolFileName, pdfFileName, kPDFFileNameBufferLen, jobDPI );
- else
- {
- strncpy( pdfFileName, spoolFileName, kPDFFileNameBufferLen );
- printCmdErr = 0;
- }
-
- if ( printCmdErr == 0 )
- {
- #define kJobOptionsStringSize 500
- char jobOptions[ kJobOptionsStringSize ];
-
- jobOptions[ 0 ] = NULL;
- /* Based on job options, find out what parameters to pass to the printer. */
- if ( pJobData.m_pParser )
- {
- const PPDKey *pSizeKey;
- const PPDValue *pSizeValue;
- const PPDKey *pSlotKey;
- const PPDValue *pSlotValue;
- BOOL needComma = FALSE;
-
- // Only CUPS PS->PDF printers require the page size options
- if ( printFormat == kApplePrintingUsePDF )
- {
- // Add page size option if necessary
- pSizeKey = pJobData.m_pParser->getKey( String(RTL_CONSTASCII_USTRINGPARAM("PageSize")) );
- pSizeValue = pSizeKey != NULL ? pJobData.m_aContext.getValue( pSizeKey ) : NULL;
- if ( pSizeValue )
- {
- ByteString aSizeString( pSizeValue->m_aOption, RTL_TEXTENCODING_ISO_8859_1 );
- snprintf( jobOptions, kJobOptionsStringSize, "-o \"media=" );
- strncat( jobOptions, aSizeString.GetBuffer(), kJobOptionsStringSize-strlen(jobOptions) );
- needComma = TRUE;
- }
- }
-
- // Add paper tray option if necessary
- pSlotKey = pJobData.m_pParser->getKey( String(RTL_CONSTASCII_USTRINGPARAM("InputSlot")) );
- pSlotValue = pSlotKey != NULL ? pJobData.m_aContext.getValue( pSlotKey ) : NULL;
- if ( pSlotValue )
- {
- ByteString aSlotString( pSlotValue->m_aOption, RTL_TEXTENCODING_ISO_8859_1 );
- // If the string wasn't created from PageSize, create it now
- if ( strlen(jobOptions) == 0 )
- snprintf( jobOptions, kJobOptionsStringSize, "-o \"media=" );
- if ( TRUE == needComma )
- strncat( jobOptions, ",", kJobOptionsStringSize - strlen(jobOptions) );
-
- strncat( jobOptions, aSlotString.GetBuffer(), kJobOptionsStringSize - strlen(jobOptions) );
- }
-
- // Add on the ending " to the media= part
- if ( strlen(jobOptions) > 0 )
- strncat( jobOptions, "\"", kJobOptionsStringSize - strlen(jobOptions) );
-
- /* Deal with landscape orientation */
- // Disable for the moment since testers say it has undesired effects
- // if ( pJobData.m_eOrientation == orientation::Landscape )
- // strncat( jobOptions, " -o landscape ", kJobOptionsStringSize - strlen(jobOptions) );
-
- /* Attach a job title */
- ByteString jobTitle( aJobName->getStr(), RTL_TEXTENCODING_UTF8 );
- if ( strstr(psprintPrintCmd.getStr(), "lpr ") > 0 )
- snprintf( jobOptions, kJobOptionsStringSize, "%s -T \"%s\"", jobOptions, jobTitle.GetBuffer() );
- else if ( strstr(psprintPrintCmd.getStr(), "lp ") > 0 )
- snprintf( jobOptions, kJobOptionsStringSize, "%s -t \"%s\"", jobOptions, jobTitle.GetBuffer() );
- }
-
- /* Construct and execute the printing command */
- snprintf( sysCommandBuffer, kSysCommandBufferLen-1, "cat \"%s\" | %s %s", pdfFileName, psprintPrintCmd.getStr(), jobOptions );
- #ifdef DEBUG
- fprintf( stderr, "printerjob.cxx: printing PDF with command '%s'\n", sysCommandBuffer );
- #endif
-
- /* Stupid lp and lpr on 10.2 don't actually honor their respective # copies arguments.
- * So we have to just keep printing the job over and over for multiple copies for PS->PDF
- * printers. Direct-to-PostScript printers can handle the # copies _inside_ the PostScript
- * document we are sending to them.
- */
- int numCopies = (printFormat == kApplePrintingUsePDF ) ? pJobData.m_nCopies : 1;
- for( index = 1; index <= numCopies; index++ )
- printCmdErr = system( sysCommandBuffer );
- }
- unlink( pdfFileName );
- }
-
- #ifdef DEBUG
- /* A printCmdErr of -1 will usually be an error in PS->PDF conversion,
- * otherwise its an error returned by system().
- */
- if ( printCmdErr != 0 )
- fprintf( stderr, "printerjob.cxx: printing error, got %d.\n", printCmdErr );
- else
- fprintf( stderr, "printerjob.cxx: printing successful, system() returned 0.\n" );
- #endif
- }
-}
-
-#define kSysPrintSetupString "sh -c 'PATH=$PATH:/usr/local/bin"
-#define kPDFFileExtension ".pdf"
-
-// Convert PostScript files to PDF for easy printing, using GhostScript.
-// Returns: 0 if no error
-// -1 if error
-int macxp_ConvertPSFileToPDF( char *psFileName, char *pdfFileName, int pdfFileNameBufferLen, sal_uInt32 jobDPI )
-{
- int returnVal = 0;
- int printCmdErr = 0;
-
- /* Make sure we've got the space to play with file names/paths */
- if ( (strlen(psFileName) + strlen(kPDFFileExtension)) >= pdfFileNameBufferLen-1 )
- {
- fprintf( stderr, "printerjob.cxx: Cannot convert print job to PDF because the file's path is too long.\n" );
- returnVal = -1;
- }
- else
- {
- char *sysCommandBuffer;
-
- /* Create the file name for the converted PDF and assemble the conversion command */
- snprintf( pdfFileName, pdfFileNameBufferLen-1, "%s%s", psFileName, kPDFFileExtension );
- int nBufferSize = sizeof(char) * ( strlen(kSysPrintSetupString) +
- strlen(kApplePS2PDFLocation) +
- 10 + /* For " -r<DPI>" */
- strlen(psFileName) +
- strlen(pdfFileName) +
- 10); /* 10 bytes fudge factor */
-
- sysCommandBuffer = (char *)malloc( nBufferSize );
- if ( sysCommandBuffer == NULL )
- returnVal = -1;
- else
- {
- int err;
- struct stat status;
-
- err = stat( kApplePsToPdfLocation, &status );
- if ( err == 0 )
- {
- snprintf( sysCommandBuffer, nBufferSize, "%s %s -o %s", kApplePsToPdfLocation, psFileName, pdfFileName );
- #ifdef DEBUG
- fprintf( stderr, "printerjob.cxx: converting document to PDF with command '%s'\n", sysCommandBuffer );
- #endif
- } else
- {
- snprintf( sysCommandBuffer, nBufferSize, "%s;%s -r%d \"%s\" \"%s\"'", kSysPrintSetupString, kApplePS2PDFLocation, jobDPI, psFileName, pdfFileName );
- #ifdef DEBUG
- fprintf( stderr, "printerjob.cxx: converting document to PDF with command '%s'\n", sysCommandBuffer );
- #endif
- }
-
- /* Convert the PS spool file to a PDF */
- printCmdErr = system( sysCommandBuffer );
- free( sysCommandBuffer );
-
- #ifdef DEBUG
- if ( printCmdErr != 0 )
- fprintf( stderr, "printerjob.cxx: pdf conversion error, system() returned %d.\n", printCmdErr );
- else
- fprintf( stderr, "printerjob.cxx: pdf conversion successful, system() returned 0.\n" );
- #endif
- returnVal = (printCmdErr != 0) ? -1 : 0;
- }
- }
-
- return( returnVal );
-}
-#endif /* MACOSX */
-
sal_Bool
PrinterJob::AbortJob ()
{