tdf#156230: Drop freshly dead PspSalPrinter code

Change-Id: I272c8f3a4d70ef9d5d03eecc8c91ee8423c52ccc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154976
Tested-by: Jenkins
Reviewed-by: خالد حسني <khaled@libreoffice.org>
diff --git a/vcl/inc/unx/genprn.h b/vcl/inc/unx/genprn.h
index 0c92446..67bf42b 100644
--- a/vcl/inc/unx/genprn.h
+++ b/vcl/inc/unx/genprn.h
@@ -22,7 +22,6 @@

#include <jobdata.hxx>
#include <unx/printergfx.hxx>
#include <unx/printerjob.hxx>
#include <salprn.hxx>

class GenPspGraphics;
@@ -59,8 +58,6 @@ public:
    OUString                  m_aFileName;
    OUString                  m_aTmpFile;
    SalInfoPrinter*         m_pInfoPrinter;
    std::unique_ptr<GenPspGraphics> m_xGraphics;
    psp::PrinterJob         m_aPrintJob;
    psp::JobData            m_aJobData;
    psp::PrinterGfx         m_aPrinterGfx;
    sal_uInt32              m_nCopies;
diff --git a/vcl/unx/generic/print/genprnpsp.cxx b/vcl/unx/generic/print/genprnpsp.cxx
index 84de26e..560f0f4 100644
--- a/vcl/unx/generic/print/genprnpsp.cxx
+++ b/vcl/unx/generic/print/genprnpsp.cxx
@@ -243,93 +243,6 @@ static void copyJobDataToJobSetup( ImplJobSetup* pJobSetup, JobData& rData )
    pJobSetup->SetPapersizeFromSetup( rData.m_bPapersizeFromSetup );
}

// Needs a cleaner abstraction ...
static bool passFileToCommandLine( const OUString& rFilename, const OUString& rCommandLine )
{
    bool bSuccess = false;

    rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
    OString aCmdLine(OUStringToOString(rCommandLine, aEncoding));
    OString aFilename(OUStringToOString(rFilename, aEncoding));

    bool bPipe = aCmdLine.indexOf( "(TMP)" ) == -1;

    // setup command line for exec
    if( ! bPipe )
        aCmdLine = aCmdLine.replaceAll("(TMP)", aFilename);

#if OSL_DEBUG_LEVEL > 1
    SAL_INFO("vcl.unx.print", (bPipe ? "piping to" : "executing")
            << " commandline: \"" << aCmdLine << "\".");
    struct stat aStat;
    SAL_WARN_IF(stat( aFilename.getStr(), &aStat ),
            "vcl.unx.print", "stat( " << aFilename << " ) failed.");
    SAL_INFO("vcl.unx.print", "Tmp file " << aFilename
            << " has modes: "
            << std::showbase << std::oct
            << (long)aStat.st_mode);
#endif
    const char* argv[4];
    if( ! ( argv[ 0 ] = getenv( "SHELL" ) ) )
        argv[ 0 ] = "/bin/sh";
    argv[ 1 ] = "-c";
    argv[ 2 ] = aCmdLine.getStr();
    argv[ 3 ] = nullptr;

    bool bHavePipes = false;
    int pid, fd[2];

    if( bPipe )
        bHavePipes = pipe( fd ) == 0;
    if( ( pid = fork() ) > 0 )
    {
        if( bPipe && bHavePipes )
        {
            close( fd[0] );
            char aBuffer[ 2048 ];
            FILE* fp = fopen( aFilename.getStr(), "r" );
            while (fp && !feof(fp))
            {
                size_t nBytesRead = fread(aBuffer, 1, sizeof( aBuffer ), fp);
                if (nBytesRead )
                {
                    size_t nBytesWritten = write(fd[1], aBuffer, nBytesRead);
                    OSL_ENSURE(nBytesWritten == nBytesRead, "short write");
                    if (nBytesWritten != nBytesRead)
                        break;
                }
            }
            fclose( fp );
            close( fd[ 1 ] );
        }
        int status = 0;
        if(waitpid( pid, &status, 0 ) != -1)
        {
            if( ! status )
                bSuccess = true;
        }
    }
    else if( ! pid )
    {
        if( bPipe && bHavePipes )
        {
            close( fd[1] );
            if( fd[0] != STDIN_FILENO ) // not probable, but who knows :)
                dup2( fd[0], STDIN_FILENO );
        }
        execv( argv[0], const_cast<char**>(argv) );
        fprintf( stderr, "failed to execute \"%s\"\n", aCmdLine.getStr() );
        _exit( 1 );
    }
    else
        fprintf( stderr, "failed to fork\n" );

    // clean up the mess
    unlink( aFilename.getStr() );

    return bSuccess;
}

static std::vector<OUString> getFaxNumbers()
{
    std::vector<OUString> aFaxNumbers;
@@ -344,11 +257,6 @@ static std::vector<OUString> getFaxNumbers()
    return aFaxNumbers;
}

static bool createPdf( std::u16string_view rToFile, const OUString& rFromFile, const OUString& rCommandLine )
{
    return passFileToCommandLine( rFromFile, rCommandLine.replaceAll("(OUTFILE)", rToFile) );
}

/*
 *  SalInstance
 */
@@ -788,8 +696,6 @@ PspSalPrinter::PspSalPrinter( SalInfoPrinter* pInfoPrinter )
    : m_pInfoPrinter( pInfoPrinter )
    , m_nCopies( 1 )
    , m_bCollate( false )
    , m_bPdf( false )
    , m_bIsPDFWriterJob( false )
{
}

@@ -797,109 +703,34 @@ PspSalPrinter::~PspSalPrinter()
{
}

static OUString getTmpName()
{
    OUString aTmp, aSys;
    osl_createTempFile( nullptr, nullptr, &aTmp.pData );
    osl_getSystemPathFromFileURL( aTmp.pData, &aSys.pData );

    return aSys;
}

bool PspSalPrinter::StartJob(
    const OUString* pFileName,
    const OUString& rJobName,
    const OUString& rAppName,
    sal_uInt32 nCopies,
    bool bCollate,
    bool bDirect,
    ImplJobSetup* pJobSetup )
    const OUString* /*pFileName*/,
    const OUString& /*rJobName*/,
    const OUString& /*rAppName*/,
    sal_uInt32 /*nCopies*/,
    bool /*bCollate*/,
    bool /*bDirect*/,
    ImplJobSetup* /*pJobSetup*/ )
{
    SAL_INFO( "vcl.unx.print", "PspSalPrinter::StartJob");
    GetSalInstance()->jobStartedPrinterUpdate();
    m_bPdf      = false;
    if (pFileName)
        m_aFileName = *pFileName;
    else
        m_aFileName.clear();
    m_aTmpFile.clear();
    m_nCopies   = nCopies;
    m_bCollate  = bCollate;

    JobData::constructFromStreamBuffer( pJobSetup->GetDriverData(), pJobSetup->GetDriverDataLen(), m_aJobData );
    if( m_nCopies > 1 )
    {
        // in case user did not do anything (m_nCopies=1)
        // take the default from jobsetup
        m_aJobData.m_nCopies = m_nCopies;
        m_aJobData.setCollate( bCollate );
    }

    int nMode = 0;
    // check whether this printer is configured as fax
    const PrinterInfo& rInfo( PrinterInfoManager::get().getPrinterInfo( m_aJobData.m_aPrinterName ) );
    OUString sPdfDir;
    if (getPdfDir(rInfo, sPdfDir))
    {
        m_bPdf = true;
        m_aTmpFile = getTmpName();
        nMode = S_IRUSR | S_IWUSR;

        if( m_aFileName.isEmpty() )
            m_aFileName = sPdfDir + "/" + rJobName + ".pdf";
    }
    m_aPrinterGfx.Init( m_aJobData );

    return m_aPrintJob.StartJob( ! m_aTmpFile.isEmpty() ? m_aTmpFile : m_aFileName, nMode, rJobName, rAppName, m_aJobData, &m_aPrinterGfx, bDirect );
    OSL_FAIL( "should never be called" );
    return false;
}

bool PspSalPrinter::EndJob()
{
    bool bSuccess = false;
    if( m_bIsPDFWriterJob )
        bSuccess = true;
    else
    {
        bSuccess = m_aPrintJob.EndJob();
        SAL_INFO( "vcl.unx.print", "PspSalPrinter::EndJob " << bSuccess);

        if( bSuccess && m_bPdf )
        {
            const PrinterInfo& rInfo( PrinterInfoManager::get().getPrinterInfo( m_aJobData.m_aPrinterName ) );
            bSuccess = createPdf( m_aFileName, m_aTmpFile, rInfo.m_aCommand );
        }
    }
    GetSalInstance()->jobEndedPrinterUpdate();
    return bSuccess;
    return true;
}

SalGraphics* PspSalPrinter::StartPage( ImplJobSetup* pJobSetup, bool )
SalGraphics* PspSalPrinter::StartPage( ImplJobSetup*, bool )
{
    SAL_INFO( "vcl.unx.print", "PspSalPrinter::StartPage");

    JobData::constructFromStreamBuffer( pJobSetup->GetDriverData(), pJobSetup->GetDriverDataLen(), m_aJobData );
    m_xGraphics = GetGenericInstance()->CreatePrintGraphics();
    m_xGraphics->Init(&m_aJobData, &m_aPrinterGfx);

    if( m_nCopies > 1 )
    {
        // in case user did not do anything (m_nCopies=1)
        // take the default from jobsetup
        m_aJobData.m_nCopies = m_nCopies;
        m_aJobData.setCollate( m_nCopies > 1 && m_bCollate );
    }

    m_aPrintJob.StartPage( m_aJobData );
    m_aPrinterGfx.Init( m_aPrintJob );

    return m_xGraphics.get();
    OSL_FAIL( "should never be called" );
    return nullptr;
}

void PspSalPrinter::EndPage()
{
    m_aPrintJob.EndPage();
    m_aPrinterGfx.Clear();
    SAL_INFO( "vcl.unx.print", "PspSalPrinter::EndPage");
    OSL_FAIL( "should never be called" );
}

namespace {
@@ -946,8 +777,6 @@ bool PspSalPrinter::StartJob( const OUString* i_pFileName, const OUString& i_rJo
                              ImplJobSetup* i_pSetupData, vcl::PrinterController& i_rController )
{
    SAL_INFO( "vcl.unx.print", "StartJob with controller: pFilename = " << (i_pFileName ? *i_pFileName : "<nil>") );
    // mark for endjob
    m_bIsPDFWriterJob = true;
    // reset IsLastPage
    i_rController.setLastPage( false );
    // is this a fax device