<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: printerinfomanager.cxx,v $
 *
 *  $Revision: 1.44 $
 *
 *  last change: $Author: obo $ $Date: 2007-06-11 14:20:51 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_psprint.hxx"

#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>

#include <tools/urlobj.hxx>
#include <tools/stream.hxx>
#include <tools/debug.hxx>
#include <tools/config.hxx>
#include <cupsmgr.hxx>
#include <psprint/fontmanager.hxx>
#include <psprint/strhelper.hxx>
#include <rtl/strbuf.hxx>

#include <osl/thread.hxx>
#include <osl/mutex.hxx>
#include <osl/process.h>

#ifdef MACOSX
#include <sys/stat.h>
#endif

// filename of configuration files
#define PRINT_FILENAME  "psprint.conf"
// the group of the global defaults
#define GLOBAL_DEFAULTS_GROUP "__Global_Printer_Defaults__"

#include <hash_set>

using namespace psp;
using namespace rtl;
using namespace osl;

namespace psp
{
    class SystemQueueInfo : public Thread
    {
        mutable Mutex               m_aMutex;
        bool                        m_bChanged;
        std::list< PrinterInfoManager::SystemPrintQueue >
                                    m_aQueues;
        OUString                    m_aCommand;

        virtual void run();

        public:
        SystemQueueInfo();
        ~SystemQueueInfo();

        bool hasChanged() const;
        OUString getCommand() const;

        // sets changed status to false; therefore not const
        void getSystemQueues( std::list< PrinterInfoManager::SystemPrintQueue >& rQueues );
    };
} // namespace

/*
*  class PrinterInfoManager
*/

// -----------------------------------------------------------------

PrinterInfoManager& PrinterInfoManager::get()
{
    static PrinterInfoManager* pManager = NULL;

    if( ! pManager )
    {
        pManager = CUPSManager::tryLoadCUPS();
        if( ! pManager )
            pManager = new PrinterInfoManager();

        if( pManager )
            pManager->initialize();
        #if OSL_DEBUG_LEVEL > 1
        fprintf( stderr, "PrinterInfoManager::get create Manager of type %d\n", pManager->getType() );
        #endif
    }

    return *pManager;
}

// -----------------------------------------------------------------

PrinterInfoManager::PrinterInfoManager( Type eType ) :
    m_pQueueInfo( NULL ),
    m_eType( eType ),
    m_bUseIncludeFeature( false ),
    m_aSystemDefaultPaper( RTL_CONSTASCII_USTRINGPARAM( "A4" ) )
{
    if( eType == Default )
        m_pQueueInfo = new SystemQueueInfo();
    initSystemDefaultPaper();
}

// -----------------------------------------------------------------

PrinterInfoManager::~PrinterInfoManager()
{
    delete m_pQueueInfo;
}

// -----------------------------------------------------------------

void PrinterInfoManager::initSystemDefaultPaper()
{
    bool bSuccess = false;

    // try libpaper
    FILE* pPipe = popen( "paperconf", "r" );
    if( pPipe )
    {
        char pBuffer[ 1024 ];
    *pBuffer = 0;
        fgets( pBuffer, sizeof(pBuffer)-1, pPipe );
    pclose( pPipe );

    ByteString aPaper( pBuffer );
    aPaper = WhitespaceToSpace( aPaper );
    if( aPaper.Len() )
    {
            m_aSystemDefaultPaper = OUString( OStringToOUString( aPaper, osl_getThreadTextEncoding() ) );
            bSuccess = true;
            #if OSL_DEBUG_LEVEL > 1
            fprintf( stderr, "paper from paperconf = %s\n", aPaper.GetBuffer() );
            #endif
        }
        if( bSuccess )
            return;
    }

    // default value is Letter for US (en_US), Cannada (en_CA, fr_CA); else A4
    // en will be interpreted as en_US

    // note: at this point m_aSystemDefaultPaper is set to "A4" from the constructor

    // check for LC_PAPER
    const char* pPaperLang = getenv( "LC_PAPER" );<commit_msg>INTEGRATION: CWS vcl79 (1.44.2); FILE MERGED 2007/06/19 08:53:21 pl 1.44.2.2: #i78617# work around stderr of shell 2007/06/18 17:21:45 pl 1.44.2.1: #i78617# silence paperconf<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: printerinfomanager.cxx,v $
 *
 *  $Revision: 1.45 $
 *
 *  last change: $Author: rt $ $Date: 2007-07-03 13:59:32 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_psprint.hxx"

#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>

#include <tools/urlobj.hxx>
#include <tools/stream.hxx>
#include <tools/debug.hxx>
#include <tools/config.hxx>
#include <cupsmgr.hxx>
#include <psprint/fontmanager.hxx>
#include <psprint/strhelper.hxx>
#include <rtl/strbuf.hxx>

#include <osl/thread.hxx>
#include <osl/mutex.hxx>
#include <osl/process.h>

#ifdef MACOSX
#include <sys/stat.h>
#endif

// filename of configuration files
#define PRINT_FILENAME  "psprint.conf"
// the group of the global defaults
#define GLOBAL_DEFAULTS_GROUP "__Global_Printer_Defaults__"

#include <hash_set>

using namespace psp;
using namespace rtl;
using namespace osl;

namespace psp
{
    class SystemQueueInfo : public Thread
    {
        mutable Mutex               m_aMutex;
        bool                        m_bChanged;
        std::list< PrinterInfoManager::SystemPrintQueue >
                                    m_aQueues;
        OUString                    m_aCommand;

        virtual void run();

        public:
        SystemQueueInfo();
        ~SystemQueueInfo();

        bool hasChanged() const;
        OUString getCommand() const;

        // sets changed status to false; therefore not const
        void getSystemQueues( std::list< PrinterInfoManager::SystemPrintQueue >& rQueues );
    };
} // namespace

/*
*  class PrinterInfoManager
*/

// -----------------------------------------------------------------

PrinterInfoManager& PrinterInfoManager::get()
{
    static PrinterInfoManager* pManager = NULL;

    if( ! pManager )
    {
        pManager = CUPSManager::tryLoadCUPS();
        if( ! pManager )
            pManager = new PrinterInfoManager();

        if( pManager )
            pManager->initialize();
        #if OSL_DEBUG_LEVEL > 1
        fprintf( stderr, "PrinterInfoManager::get create Manager of type %d\n", pManager->getType() );
        #endif
    }

    return *pManager;
}

// -----------------------------------------------------------------

PrinterInfoManager::PrinterInfoManager( Type eType ) :
    m_pQueueInfo( NULL ),
    m_eType( eType ),
    m_bUseIncludeFeature( false ),
    m_aSystemDefaultPaper( RTL_CONSTASCII_USTRINGPARAM( "A4" ) )
{
    if( eType == Default )
        m_pQueueInfo = new SystemQueueInfo();
    initSystemDefaultPaper();
}

// -----------------------------------------------------------------

PrinterInfoManager::~PrinterInfoManager()
{
    delete m_pQueueInfo;
}

// -----------------------------------------------------------------

void PrinterInfoManager::initSystemDefaultPaper()
{
    bool bSuccess = false;

    // try libpaper
    #ifdef SOLARIS
    // #i78617# workaround missing paperconf command; on e.g. Linux
    // the 2>/dev/null works on the started shell also
    FILE* pPipe = popen( "sh -c paperconf 2>/dev/null", "r" );
    #else
    FILE* pPipe = popen( "paperconf 2>/dev/null", "r" );
    #endif
    if( pPipe )
    {
        char pBuffer[ 1024 ];
        *pBuffer = 0;
        fgets( pBuffer, sizeof(pBuffer)-1, pPipe );
        pclose( pPipe );

        ByteString aPaper( pBuffer );
        aPaper = WhitespaceToSpace( aPaper );
        if( aPaper.Len() )
        {
            m_aSystemDefaultPaper = OUString( OStringToOUString( aPaper, osl_getThreadTextEncoding() ) );
            bSuccess = true;
            #if OSL_DEBUG_LEVEL > 1
            fprintf( stderr, "paper from paperconf = %s\n", aPaper.GetBuffer() );
            #endif
        }
        if( bSuccess )
            return;
    }

    // default value is Letter for US (en_US), Cannada (en_CA, fr_CA); else A4
    // en will be interpreted as en_US

    // note: at this point m_aSystemDefaultPaper is set to "A4" from the constructor

    // check for LC_PAPER
    const char* pPaperLang = getenv( "LC_PAPER" );
{'fraction_non_alphanumeric': 0.17710706150341685, 'fraction_numerical': 0.01727410782080486, 'mean_word_length': 2.3397781299524563, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2131353', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3039, 'n_words': 901}