xref: /aoo42x/main/vcl/unx/generic/printer/cupsmgr.cxx (revision 24c56ab9)
1c82f2877SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3c82f2877SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4c82f2877SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5c82f2877SAndrew Rist  * distributed with this work for additional information
6c82f2877SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7c82f2877SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8c82f2877SAndrew Rist  * "License"); you may not use this file except in compliance
9c82f2877SAndrew Rist  * with the License.  You may obtain a copy of the License at
10c82f2877SAndrew Rist  *
11c82f2877SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12c82f2877SAndrew Rist  *
13c82f2877SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14c82f2877SAndrew Rist  * software distributed under the License is distributed on an
15c82f2877SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c82f2877SAndrew Rist  * KIND, either express or implied.  See the License for the
17c82f2877SAndrew Rist  * specific language governing permissions and limitations
18c82f2877SAndrew Rist  * under the License.
19c82f2877SAndrew Rist  *
20c82f2877SAndrew Rist  *************************************************************/
21c82f2877SAndrew Rist 
22c82f2877SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifdef ENABLE_CUPS
28cdf0e10cSrcweir #include <cups/cups.h>
29cdf0e10cSrcweir #include <cups/ppd.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #else // !ENABLE_CUPS
32cdf0e10cSrcweir typedef void ppd_file_t;
33cdf0e10cSrcweir typedef void cups_dest_t;
34cdf0e10cSrcweir typedef void cups_option_t;
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <unistd.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "cupsmgr.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include "osl/thread.h"
42cdf0e10cSrcweir #include "osl/diagnose.h"
43cdf0e10cSrcweir #include "osl/conditn.hxx"
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <algorithm>
48cdf0e10cSrcweir #include <setjmp.h>
49cdf0e10cSrcweir #include <signal.h>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #define CUPS_LIB_NAME "libcups.so.2"
52cdf0e10cSrcweir 
53cdf0e10cSrcweir namespace psp
54cdf0e10cSrcweir {
55cdf0e10cSrcweir class CUPSWrapper
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     oslModule		m_pLib;
58cdf0e10cSrcweir     osl::Mutex		m_aGetPPDMutex;
59cdf0e10cSrcweir     bool            m_bPPDThreadRunning;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     int				(*m_pcupsPrintFile)(const char*, const char*, const char*, int, cups_option_t*);
62cdf0e10cSrcweir     int				(*m_pcupsGetDests)(cups_dest_t**);
63cdf0e10cSrcweir     void			(*m_pcupsSetDests)(int,cups_dest_t*);
64cdf0e10cSrcweir     void			(*m_pcupsFreeDests)(int,cups_dest_t*);
65cdf0e10cSrcweir     const char*		(*m_pcupsGetPPD)(const char*);
66cdf0e10cSrcweir     int				(*m_pcupsMarkOptions)(ppd_file_t*,int,cups_option_t*);
67cdf0e10cSrcweir     int				(*m_pcupsAddOption)(const char*,const char*,int,cups_option_t**);
68cdf0e10cSrcweir     void			(*m_pcupsFreeOptions)(int,cups_option_t*);
69cdf0e10cSrcweir     ppd_file_t*		(*m_pppdOpenFile)(const char* pFile);
70cdf0e10cSrcweir     void			(*m_pppdClose)(ppd_file_t*);
71cdf0e10cSrcweir     const char*		(*m_pcupsServer)();
72cdf0e10cSrcweir     void			(*m_pcupsSetPasswordCB)(const char*(cb)(const char*));
73cdf0e10cSrcweir     const char*		(*m_pcupsUser)();
74cdf0e10cSrcweir     void			(*m_pcupsSetUser)(const char*);
75cdf0e10cSrcweir     const char*     (*m_pcupsGetOption)(const char*,int,cups_option_t*);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     oslGenericFunction loadSymbol( const char* );
78cdf0e10cSrcweir public:
79cdf0e10cSrcweir     CUPSWrapper();
80cdf0e10cSrcweir     ~CUPSWrapper();
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     bool isValid();
83cdf0e10cSrcweir 
cupsGetDests(cups_dest_t ** pDests)84cdf0e10cSrcweir     int cupsGetDests(cups_dest_t** pDests)
85cdf0e10cSrcweir     { return m_pcupsGetDests(pDests); }
86cdf0e10cSrcweir 
cupsSetDests(int nDests,cups_dest_t * pDests)87cdf0e10cSrcweir     void cupsSetDests( int nDests, cups_dest_t* pDests )
88cdf0e10cSrcweir     { m_pcupsSetDests( nDests, pDests ); }
89cdf0e10cSrcweir 
cupsFreeDests(int nDests,cups_dest_t * pDests)90cdf0e10cSrcweir     void cupsFreeDests(int nDests, cups_dest_t* pDests)
91cdf0e10cSrcweir     { m_pcupsFreeDests(nDests, pDests); }
92cdf0e10cSrcweir 
cupsPrintFile(const char * pPrinter,const char * pFileName,const char * pTitle,int nOptions,cups_option_t * pOptions)93cdf0e10cSrcweir     int cupsPrintFile( const char* pPrinter,
94cdf0e10cSrcweir                        const char* pFileName,
95cdf0e10cSrcweir                        const char* pTitle,
96cdf0e10cSrcweir                        int nOptions,
97cdf0e10cSrcweir                    cups_option_t* pOptions )
98cdf0e10cSrcweir     { return m_pcupsPrintFile( pPrinter, pFileName, pTitle, nOptions, pOptions ); }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     rtl::OString cupsGetPPD( const char* pPrinter );
101cdf0e10cSrcweir 
cupsMarkOptions(ppd_file_t * pPPD,int nOptions,cups_option_t * pOptions)102cdf0e10cSrcweir     int cupsMarkOptions(ppd_file_t* pPPD, int nOptions, cups_option_t* pOptions )
103cdf0e10cSrcweir     { return m_pcupsMarkOptions(pPPD, nOptions, pOptions); }
104cdf0e10cSrcweir 
cupsAddOption(const char * pName,const char * pValue,int nOptions,cups_option_t ** pOptions)105cdf0e10cSrcweir     int cupsAddOption( const char* pName, const char* pValue, int nOptions, cups_option_t** pOptions )
106cdf0e10cSrcweir     { return m_pcupsAddOption( pName, pValue, nOptions, pOptions ); }
107cdf0e10cSrcweir 
cupsFreeOptions(int nOptions,cups_option_t * pOptions)108cdf0e10cSrcweir     void cupsFreeOptions( int nOptions, cups_option_t* pOptions )
109cdf0e10cSrcweir     { m_pcupsFreeOptions( nOptions, pOptions ); }
110cdf0e10cSrcweir 
ppdOpenFile(const char * pFileName)111cdf0e10cSrcweir     ppd_file_t* ppdOpenFile( const char* pFileName )
112cdf0e10cSrcweir     { return m_pppdOpenFile( pFileName ); }
113cdf0e10cSrcweir 
ppdClose(ppd_file_t * pPPD)114cdf0e10cSrcweir     void ppdClose( ppd_file_t* pPPD )
115cdf0e10cSrcweir     { m_pppdClose( pPPD ); }
116cdf0e10cSrcweir 
cupsServer(void)117cdf0e10cSrcweir     const char	*cupsServer(void)
118cdf0e10cSrcweir     { return m_pcupsServer(); }
119cdf0e10cSrcweir 
cupsUser(void)120cdf0e10cSrcweir     const char	*cupsUser(void)
121cdf0e10cSrcweir     { return m_pcupsUser(); }
122cdf0e10cSrcweir 
cupsSetPasswordCB(const char * (* cb)(const char *))123cdf0e10cSrcweir     void cupsSetPasswordCB(const char *(*cb)(const char *))
124cdf0e10cSrcweir     { m_pcupsSetPasswordCB( cb ); }
125cdf0e10cSrcweir 
cupsSetUser(const char * user)126cdf0e10cSrcweir     void cupsSetUser(const char *user)
127cdf0e10cSrcweir     { m_pcupsSetUser( user ); }
128cdf0e10cSrcweir 
cupsGetOption(const char * name,int num_options,cups_option_t * options)129cdf0e10cSrcweir     const char* cupsGetOption(const char* name, int num_options, cups_option_t* options)
130cdf0e10cSrcweir     { return m_pcupsGetOption( name, num_options, options ); }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir };
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir using namespace psp;
136cdf0e10cSrcweir using namespace osl;
137cdf0e10cSrcweir using namespace rtl;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir /*
140cdf0e10cSrcweir  *  CUPSWrapper class
141cdf0e10cSrcweir  */
142cdf0e10cSrcweir 
loadSymbol(const char * pSymbol)143cdf0e10cSrcweir oslGenericFunction CUPSWrapper::loadSymbol( const char* pSymbol )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     OUString aSym( OUString::createFromAscii( pSymbol ) );
146cdf0e10cSrcweir     oslGenericFunction pSym = osl_getFunctionSymbol( m_pLib, aSym.pData );
147cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
148cdf0e10cSrcweir     fprintf( stderr, "%s %s\n", pSymbol, pSym ? "found" : "not found" );
149cdf0e10cSrcweir #endif
150cdf0e10cSrcweir     return pSym;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
CUPSWrapper()153cdf0e10cSrcweir CUPSWrapper::CUPSWrapper()
154cdf0e10cSrcweir         : m_pLib( NULL ),
155cdf0e10cSrcweir           m_bPPDThreadRunning( false )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir #ifdef ENABLE_CUPS
158aa150a94SHerbert Dürr     m_pLib = osl_loadAsciiModule( CUPS_LIB_NAME, SAL_LOADMODULE_LAZY );
159cdf0e10cSrcweir     if( ! m_pLib )
160aa150a94SHerbert Dürr         m_pLib = osl_loadAsciiModule( "cups", SAL_LOADMODULE_LAZY );
161cdf0e10cSrcweir #endif
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     if( ! m_pLib )
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
166cdf0e10cSrcweir         fprintf( stderr, "no cups library found\n" );
167cdf0e10cSrcweir #endif
168cdf0e10cSrcweir         return;
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     m_pcupsPrintFile	 	= (int(*)(const char*,const char*,const char*,int,cups_option_t*))
172cdf0e10cSrcweir         loadSymbol( "cupsPrintFile" );
173cdf0e10cSrcweir     m_pcupsGetDests			= (int(*)(cups_dest_t**))
174cdf0e10cSrcweir         loadSymbol( "cupsGetDests" );
175cdf0e10cSrcweir     m_pcupsSetDests			= (void(*)(int,cups_dest_t*))
176cdf0e10cSrcweir         loadSymbol( "cupsSetDests" );
177cdf0e10cSrcweir     m_pcupsFreeDests		= (void(*)(int,cups_dest_t*))
178cdf0e10cSrcweir         loadSymbol( "cupsFreeDests" );
179cdf0e10cSrcweir     m_pcupsGetPPD			= (const char*(*)(const char*))
180cdf0e10cSrcweir         loadSymbol( "cupsGetPPD" );
181cdf0e10cSrcweir     m_pcupsMarkOptions		= (int(*)(ppd_file_t*,int,cups_option_t*))
182cdf0e10cSrcweir         loadSymbol( "cupsMarkOptions" );
183cdf0e10cSrcweir     m_pcupsAddOption		= (int(*)(const char*,const char*,int,cups_option_t**))
184cdf0e10cSrcweir         loadSymbol( "cupsAddOption" );
185cdf0e10cSrcweir     m_pcupsFreeOptions		= (void(*)(int,cups_option_t*))
186cdf0e10cSrcweir         loadSymbol( "cupsFreeOptions" );
187cdf0e10cSrcweir     m_pppdOpenFile			= (ppd_file_t*(*)(const char*))
188cdf0e10cSrcweir         loadSymbol( "ppdOpenFile" );
189cdf0e10cSrcweir     m_pppdClose				= (void(*)(ppd_file_t*))
190cdf0e10cSrcweir         loadSymbol( "ppdClose" );
191cdf0e10cSrcweir     m_pcupsServer			= (const char*(*)())
192cdf0e10cSrcweir         loadSymbol( "cupsServer" );
193cdf0e10cSrcweir     m_pcupsUser				= (const char*(*)())
194cdf0e10cSrcweir         loadSymbol( "cupsUser" );
195cdf0e10cSrcweir     m_pcupsSetPasswordCB	= (void(*)(const char*(*)(const char*)))
196cdf0e10cSrcweir         loadSymbol( "cupsSetPasswordCB" );
197cdf0e10cSrcweir     m_pcupsSetUser			= (void(*)(const char*))
198cdf0e10cSrcweir         loadSymbol( "cupsSetUser" );
199cdf0e10cSrcweir     m_pcupsGetOption        = (const char*(*)(const char*,int,cups_option_t*))
200cdf0e10cSrcweir         loadSymbol( "cupsGetOption" );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     if( ! (
203cdf0e10cSrcweir            m_pcupsPrintFile					&&
204cdf0e10cSrcweir            m_pcupsGetDests					&&
205cdf0e10cSrcweir            m_pcupsSetDests					&&
206cdf0e10cSrcweir            m_pcupsFreeDests					&&
207cdf0e10cSrcweir            m_pcupsGetPPD					&&
208cdf0e10cSrcweir            m_pcupsMarkOptions				&&
209cdf0e10cSrcweir            m_pcupsAddOption					&&
210cdf0e10cSrcweir            m_pcupsServer					&&
211cdf0e10cSrcweir            m_pcupsUser						&&
212cdf0e10cSrcweir            m_pcupsSetPasswordCB				&&
213cdf0e10cSrcweir            m_pcupsSetUser					&&
214cdf0e10cSrcweir            m_pcupsFreeOptions				&&
215cdf0e10cSrcweir            m_pppdOpenFile					&&
216cdf0e10cSrcweir            m_pppdClose                      &&
217cdf0e10cSrcweir            m_pcupsGetOption
218cdf0e10cSrcweir            ) )
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         osl_unloadModule( m_pLib );
221cdf0e10cSrcweir         m_pLib = NULL;
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
~CUPSWrapper()225cdf0e10cSrcweir CUPSWrapper::~CUPSWrapper()
226cdf0e10cSrcweir {
227cdf0e10cSrcweir     if( m_pLib )
228cdf0e10cSrcweir         osl_unloadModule( m_pLib );
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
isValid()231cdf0e10cSrcweir bool CUPSWrapper::isValid()
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     return m_pLib != NULL;
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir typedef const char*(*PPDFunction)(const char*);
237cdf0e10cSrcweir struct GetPPDAttribs
238cdf0e10cSrcweir {
239cdf0e10cSrcweir     PPDFunction         m_pFunction;
240cdf0e10cSrcweir     osl::Condition		m_aCondition;
241cdf0e10cSrcweir     OString			    m_aParameter;
242cdf0e10cSrcweir     OString			    m_aResult;
243cdf0e10cSrcweir     int                 m_nRefs;
244cdf0e10cSrcweir     bool*               m_pResetRunning;
245cdf0e10cSrcweir     osl::Mutex*         m_pSyncMutex;
246cdf0e10cSrcweir 
GetPPDAttribsGetPPDAttribs247cdf0e10cSrcweir     GetPPDAttribs( PPDFunction pFn, const char * m_pParameter,
248cdf0e10cSrcweir                    bool* pResetRunning, osl::Mutex* pSyncMutex )
249cdf0e10cSrcweir             : m_pFunction( pFn ),
250cdf0e10cSrcweir               m_aParameter( m_pParameter ),
251cdf0e10cSrcweir               m_pResetRunning( pResetRunning ),
252cdf0e10cSrcweir               m_pSyncMutex( pSyncMutex )
253cdf0e10cSrcweir     {
254cdf0e10cSrcweir         m_nRefs = 2;
255cdf0e10cSrcweir         m_aCondition.reset();
256cdf0e10cSrcweir     }
257cdf0e10cSrcweir 
~GetPPDAttribsGetPPDAttribs258cdf0e10cSrcweir     ~GetPPDAttribs()
259cdf0e10cSrcweir     {
260cdf0e10cSrcweir         if( m_aResult.getLength() )
261cdf0e10cSrcweir             unlink( m_aResult.getStr() );
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir 
unrefGetPPDAttribs264cdf0e10cSrcweir     void unref()
265cdf0e10cSrcweir     {
266cdf0e10cSrcweir         if( --m_nRefs == 0 )
267cdf0e10cSrcweir         {
268cdf0e10cSrcweir             *m_pResetRunning = false;
269cdf0e10cSrcweir             delete this;
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir 
executeCallGetPPDAttribs273cdf0e10cSrcweir     void executeCall()
274cdf0e10cSrcweir     {
275cdf0e10cSrcweir         // This CUPS method is not at all thread-safe we need
276cdf0e10cSrcweir         // to dup the pointer to a static buffer it returns ASAP
277*24c56ab9SHerbert Dürr         OString aResult = m_pFunction( m_aParameter.getStr() );
278cdf0e10cSrcweir         MutexGuard aGuard( *m_pSyncMutex );
279cdf0e10cSrcweir         m_aResult = aResult;
280cdf0e10cSrcweir         m_aCondition.set();
281cdf0e10cSrcweir         unref();
282cdf0e10cSrcweir     }
283cdf0e10cSrcweir 
waitResultGetPPDAttribs284cdf0e10cSrcweir     OString waitResult( TimeValue *pDelay )
285cdf0e10cSrcweir     {
286cdf0e10cSrcweir         m_pSyncMutex->release();
287cdf0e10cSrcweir 
288cdf0e10cSrcweir         if (m_aCondition.wait( pDelay ) != Condition::result_ok
289cdf0e10cSrcweir             )
290cdf0e10cSrcweir         {
291cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 1
292*24c56ab9SHerbert Dürr             fprintf( stderr, "cupsGetPPD %s timed out\n", m_aParameter.getStr() );
293cdf0e10cSrcweir             #endif
294cdf0e10cSrcweir         }
295cdf0e10cSrcweir         m_pSyncMutex->acquire();
296cdf0e10cSrcweir 
297cdf0e10cSrcweir         OString aRetval = m_aResult;
298cdf0e10cSrcweir         m_aResult = OString();
299cdf0e10cSrcweir         unref();
300cdf0e10cSrcweir 
301cdf0e10cSrcweir         return aRetval;
302cdf0e10cSrcweir     }
303cdf0e10cSrcweir };
304cdf0e10cSrcweir 
305cdf0e10cSrcweir extern "C" {
getPPDWorker(void * pData)306cdf0e10cSrcweir     static void getPPDWorker(void* pData)
307cdf0e10cSrcweir     {
308cdf0e10cSrcweir         GetPPDAttribs* pAttribs = (GetPPDAttribs*)pData;
309cdf0e10cSrcweir         pAttribs->executeCall();
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir }
312cdf0e10cSrcweir 
cupsGetPPD(const char * pPrinter)313cdf0e10cSrcweir OString CUPSWrapper::cupsGetPPD( const char* pPrinter )
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     OString aResult;
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     m_aGetPPDMutex.acquire();
318cdf0e10cSrcweir     // if one thread hangs in cupsGetPPD already, don't start another
319cdf0e10cSrcweir     if( ! m_bPPDThreadRunning )
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         m_bPPDThreadRunning = true;
322cdf0e10cSrcweir         GetPPDAttribs* pAttribs = new GetPPDAttribs( m_pcupsGetPPD,
323cdf0e10cSrcweir                                                      pPrinter,
324cdf0e10cSrcweir                                                      &m_bPPDThreadRunning,
325cdf0e10cSrcweir                                                      &m_aGetPPDMutex );
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         oslThread aThread = osl_createThread( getPPDWorker, pAttribs );
328cdf0e10cSrcweir 
329cdf0e10cSrcweir         TimeValue aValue;
330cdf0e10cSrcweir         aValue.Seconds = 5;
331cdf0e10cSrcweir         aValue.Nanosec = 0;
332cdf0e10cSrcweir 
333cdf0e10cSrcweir         // NOTE: waitResult release and acquires the GetPPD mutex
334cdf0e10cSrcweir         aResult = pAttribs->waitResult( &aValue );
335cdf0e10cSrcweir         osl_destroyThread( aThread );
336cdf0e10cSrcweir     }
337cdf0e10cSrcweir     m_aGetPPDMutex.release();
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     return aResult;
340cdf0e10cSrcweir }
341cdf0e10cSrcweir 
342cdf0e10cSrcweir #ifdef ENABLE_CUPS
setPasswordCallback(const char * pIn)343cdf0e10cSrcweir static const char* setPasswordCallback( const char* pIn )
344cdf0e10cSrcweir {
345cdf0e10cSrcweir     const char* pRet = NULL;
346cdf0e10cSrcweir 
347cdf0e10cSrcweir     PrinterInfoManager& rMgr = PrinterInfoManager::get();
348cdf0e10cSrcweir     if( rMgr.getType() == PrinterInfoManager::CUPS ) // sanity check
349cdf0e10cSrcweir         pRet = static_cast<CUPSManager&>(rMgr).authenticateUser( pIn );
350cdf0e10cSrcweir     return pRet;
351cdf0e10cSrcweir }
352cdf0e10cSrcweir #endif
353cdf0e10cSrcweir 
354cdf0e10cSrcweir /*
355cdf0e10cSrcweir  *  CUPSManager class
356cdf0e10cSrcweir  */
357cdf0e10cSrcweir 
tryLoadCUPS()358cdf0e10cSrcweir CUPSManager* CUPSManager::tryLoadCUPS()
359cdf0e10cSrcweir {
360cdf0e10cSrcweir     CUPSManager* pManager = NULL;
361cdf0e10cSrcweir #ifdef ENABLE_CUPS
362cdf0e10cSrcweir     static const char* pEnv = getenv( "SAL_DISABLE_CUPS" );
363cdf0e10cSrcweir 
364cdf0e10cSrcweir     if( ! pEnv || ! *pEnv )
365cdf0e10cSrcweir     {
366cdf0e10cSrcweir         // try to load CUPS
367cdf0e10cSrcweir         CUPSWrapper* pWrapper = new CUPSWrapper();
368cdf0e10cSrcweir         if( pWrapper->isValid() )
369cdf0e10cSrcweir             pManager = new CUPSManager( pWrapper );
370cdf0e10cSrcweir         else
371cdf0e10cSrcweir             delete pWrapper;
372cdf0e10cSrcweir     }
373cdf0e10cSrcweir #endif
374cdf0e10cSrcweir     return pManager;
375cdf0e10cSrcweir }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir extern "C"
378cdf0e10cSrcweir {
run_dest_thread_stub(void * pThis)379cdf0e10cSrcweir static void run_dest_thread_stub( void* pThis )
380cdf0e10cSrcweir {
381cdf0e10cSrcweir     CUPSManager::runDestThread( pThis );
382cdf0e10cSrcweir }
383cdf0e10cSrcweir }
384cdf0e10cSrcweir 
CUPSManager(CUPSWrapper * pWrapper)385cdf0e10cSrcweir CUPSManager::CUPSManager( CUPSWrapper* pWrapper ) :
386cdf0e10cSrcweir         PrinterInfoManager( CUPS ),
387cdf0e10cSrcweir         m_pCUPSWrapper( pWrapper ),
388cdf0e10cSrcweir         m_nDests( 0 ),
389cdf0e10cSrcweir         m_pDests( NULL ),
390cdf0e10cSrcweir         m_bNewDests( false )
391cdf0e10cSrcweir {
392cdf0e10cSrcweir     m_aDestThread = osl_createThread( run_dest_thread_stub, this );
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
~CUPSManager()395cdf0e10cSrcweir CUPSManager::~CUPSManager()
396cdf0e10cSrcweir {
397cdf0e10cSrcweir     if( m_aDestThread )
398cdf0e10cSrcweir     {
399cdf0e10cSrcweir         // if the thread is still running here, then
400cdf0e10cSrcweir         // cupsGetDests is hung; terminate the thread instead of joining
401cdf0e10cSrcweir         osl_terminateThread( m_aDestThread );
402cdf0e10cSrcweir         osl_destroyThread( m_aDestThread );
403cdf0e10cSrcweir     }
404cdf0e10cSrcweir 
405cdf0e10cSrcweir     if( m_nDests && m_pDests )
406cdf0e10cSrcweir         m_pCUPSWrapper->cupsFreeDests( m_nDests, (cups_dest_t*)m_pDests );
407cdf0e10cSrcweir     delete m_pCUPSWrapper;
408cdf0e10cSrcweir }
409cdf0e10cSrcweir 
runDestThread(void * pThis)410cdf0e10cSrcweir void CUPSManager::runDestThread( void* pThis )
411cdf0e10cSrcweir {
412cdf0e10cSrcweir     ((CUPSManager*)pThis)->runDests();
413cdf0e10cSrcweir }
414cdf0e10cSrcweir 
415cdf0e10cSrcweir static sigjmp_buf aViolationBuffer;
416cdf0e10cSrcweir 
417cdf0e10cSrcweir extern "C"
418cdf0e10cSrcweir {
lcl_signal_action(int nSignal)419cdf0e10cSrcweir     static void lcl_signal_action(int nSignal)
420cdf0e10cSrcweir     {
421cdf0e10cSrcweir         fprintf( stderr, "Signal %d during fontconfig initialization called, ignoring fontconfig\n", nSignal );
422cdf0e10cSrcweir         siglongjmp( aViolationBuffer, 1 );
423cdf0e10cSrcweir     }
424cdf0e10cSrcweir }
425cdf0e10cSrcweir 
runDests()426cdf0e10cSrcweir void CUPSManager::runDests()
427cdf0e10cSrcweir {
428cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
429cdf0e10cSrcweir     fprintf( stderr, "starting cupsGetDests\n" );
430cdf0e10cSrcweir #endif
431cdf0e10cSrcweir     int nDests = 0;
432cdf0e10cSrcweir     cups_dest_t* pDests = NULL;
433cdf0e10cSrcweir 
434cdf0e10cSrcweir     // #i86306# prepare against really broken CUPS installations / missing servers
435cdf0e10cSrcweir 
436cdf0e10cSrcweir     // install signal handler for SEGV, BUS and ABRT
437cdf0e10cSrcweir     struct sigaction act;
438cdf0e10cSrcweir 	struct sigaction oact[3];
439cdf0e10cSrcweir 
440cdf0e10cSrcweir     act.sa_handler = lcl_signal_action;
441cdf0e10cSrcweir     act.sa_flags   = 0;
442cdf0e10cSrcweir 	sigemptyset(&(act.sa_mask));
443cdf0e10cSrcweir 
444cdf0e10cSrcweir     int nSegvSignalInstalled = sigaction(SIGSEGV, &act, &oact[0]);
445cdf0e10cSrcweir     int nBusSignalInstalled = sigaction(SIGBUS, &act, &oact[1]);
446cdf0e10cSrcweir     int nAbortSignalInstalled = sigaction(SIGABRT, &act, &oact[2]);
447cdf0e10cSrcweir 
448cdf0e10cSrcweir     // prepare against a signal during FcInit or FcConfigGetCurrent
449cdf0e10cSrcweir     if( sigsetjmp( aViolationBuffer, ~0 ) == 0 )
450cdf0e10cSrcweir     {
451cdf0e10cSrcweir         nDests = m_pCUPSWrapper->cupsGetDests( &pDests );
452cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
453cdf0e10cSrcweir         fprintf( stderr, "came out of cupsGetDests\n" );
454cdf0e10cSrcweir         #endif
455cdf0e10cSrcweir 
456cdf0e10cSrcweir         osl::MutexGuard aGuard( m_aCUPSMutex );
457cdf0e10cSrcweir         m_nDests = nDests;
458cdf0e10cSrcweir         m_pDests = pDests;
459cdf0e10cSrcweir         m_bNewDests = true;
460cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
461cdf0e10cSrcweir         fprintf( stderr, "finished cupsGetDests\n" );
462cdf0e10cSrcweir         #endif
463cdf0e10cSrcweir     }
464cdf0e10cSrcweir     else
465cdf0e10cSrcweir     {
466cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
467cdf0e10cSrcweir         fprintf( stderr, "cupsGetDests crashed, not using CUPS\n" );
468cdf0e10cSrcweir         #endif
469cdf0e10cSrcweir     }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir     // restore old signal handlers
472cdf0e10cSrcweir     if( nSegvSignalInstalled == 0 )
473cdf0e10cSrcweir         sigaction( SIGSEGV, &oact[0], NULL );
474cdf0e10cSrcweir     if( nBusSignalInstalled == 0 )
475cdf0e10cSrcweir         sigaction( SIGBUS, &oact[1], NULL );
476cdf0e10cSrcweir     if( nAbortSignalInstalled == 0 )
477cdf0e10cSrcweir         sigaction( SIGABRT, &oact[2], NULL );
478cdf0e10cSrcweir }
479cdf0e10cSrcweir 
initialize()480cdf0e10cSrcweir void CUPSManager::initialize()
481cdf0e10cSrcweir {
482cdf0e10cSrcweir     // get normal printers, clear printer list
483cdf0e10cSrcweir     PrinterInfoManager::initialize();
484cdf0e10cSrcweir 
485cdf0e10cSrcweir #ifdef ENABLE_CUPS
486cdf0e10cSrcweir     // check whether thread has completed
487cdf0e10cSrcweir     // if not behave like old printing system
488cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aCUPSMutex );
489cdf0e10cSrcweir 
490cdf0e10cSrcweir     if( ! m_bNewDests )
491cdf0e10cSrcweir         return;
492cdf0e10cSrcweir 
493cdf0e10cSrcweir     // dest thread has run, clean up
494cdf0e10cSrcweir     if( m_aDestThread )
495cdf0e10cSrcweir     {
496cdf0e10cSrcweir         osl_joinWithThread( m_aDestThread );
497cdf0e10cSrcweir         osl_destroyThread( m_aDestThread );
498cdf0e10cSrcweir         m_aDestThread = NULL;
499cdf0e10cSrcweir     }
500cdf0e10cSrcweir     m_bNewDests = false;
501cdf0e10cSrcweir 
502cdf0e10cSrcweir     // clear old stuff
503cdf0e10cSrcweir     m_aCUPSDestMap.clear();
504cdf0e10cSrcweir 
505cdf0e10cSrcweir     if( ! (m_nDests && m_pDests ) )
506cdf0e10cSrcweir         return;
507cdf0e10cSrcweir 
508cdf0e10cSrcweir     if( isCUPSDisabled() )
509cdf0e10cSrcweir         return;
510cdf0e10cSrcweir 
511cdf0e10cSrcweir     // check for CUPS server(?) > 1.2
512cdf0e10cSrcweir     // since there is no API to query, check for options that were
513cdf0e10cSrcweir     // introduced in dests with 1.2
514cdf0e10cSrcweir     // this is needed to check for %%IncludeFeature support
515cdf0e10cSrcweir     // (#i65684#, #i65491#)
516cdf0e10cSrcweir     bool bUsePDF = false;
517cdf0e10cSrcweir     cups_dest_t* pDest = ((cups_dest_t*)m_pDests);
518cdf0e10cSrcweir     const char* pOpt = m_pCUPSWrapper->cupsGetOption( "printer-info",
519cdf0e10cSrcweir                                                       pDest->num_options,
520cdf0e10cSrcweir                                                       pDest->options );
521cdf0e10cSrcweir     if( pOpt )
522cdf0e10cSrcweir     {
523cdf0e10cSrcweir         m_bUseIncludeFeature = true;
524cdf0e10cSrcweir         bUsePDF = true;
525cdf0e10cSrcweir         if( m_aGlobalDefaults.m_nPSLevel == 0 && m_aGlobalDefaults.m_nPDFDevice == 0 )
526cdf0e10cSrcweir             m_aGlobalDefaults.m_nPDFDevice = 1;
527cdf0e10cSrcweir     }
528cdf0e10cSrcweir     // do not send include JobPatch; CUPS will insert that itself
529cdf0e10cSrcweir     // TODO: currently unknwon which versions of CUPS insert JobPatches
530cdf0e10cSrcweir     // so currently it is assumed CUPS = don't insert JobPatch files
531cdf0e10cSrcweir     m_bUseJobPatch = false;
532cdf0e10cSrcweir 
533cdf0e10cSrcweir     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
534cdf0e10cSrcweir     int nPrinter = m_nDests;
535cdf0e10cSrcweir 
536cdf0e10cSrcweir     // reset global default PPD options; these are queried on demand from CUPS
537cdf0e10cSrcweir     m_aGlobalDefaults.m_pParser = NULL;
538cdf0e10cSrcweir     m_aGlobalDefaults.m_aContext = PPDContext();
539cdf0e10cSrcweir 
540cdf0e10cSrcweir     // add CUPS printers, should there be a printer
541cdf0e10cSrcweir     // with the same name as a CUPS printer, overwrite it
542cdf0e10cSrcweir     while( nPrinter-- )
543cdf0e10cSrcweir     {
544cdf0e10cSrcweir         pDest = ((cups_dest_t*)m_pDests)+nPrinter;
545cdf0e10cSrcweir         OUString aPrinterName = OStringToOUString( pDest->name, aEncoding );
546cdf0e10cSrcweir         if( pDest->instance && *pDest->instance )
547cdf0e10cSrcweir         {
548cdf0e10cSrcweir             OUStringBuffer aBuf( 256 );
549cdf0e10cSrcweir             aBuf.append( aPrinterName );
550cdf0e10cSrcweir             aBuf.append( sal_Unicode( '/' ) );
551cdf0e10cSrcweir             aBuf.append( OStringToOUString( pDest->instance, aEncoding ) );
552cdf0e10cSrcweir             aPrinterName = aBuf.makeStringAndClear();
553cdf0e10cSrcweir         }
554cdf0e10cSrcweir 
555cdf0e10cSrcweir         // initialize printer with possible configuration from psprint.conf
556cdf0e10cSrcweir         bool bSetToGlobalDefaults = m_aPrinters.find( aPrinterName ) == m_aPrinters.end();
557cdf0e10cSrcweir         Printer aPrinter = m_aPrinters[ aPrinterName ];
558cdf0e10cSrcweir         if( bSetToGlobalDefaults )
559cdf0e10cSrcweir             aPrinter.m_aInfo = m_aGlobalDefaults;
560cdf0e10cSrcweir         aPrinter.m_aInfo.m_aPrinterName = aPrinterName;
561cdf0e10cSrcweir         if( pDest->is_default )
562cdf0e10cSrcweir             m_aDefaultPrinter = aPrinterName;
563cdf0e10cSrcweir 
564cdf0e10cSrcweir         for( int k = 0; k < pDest->num_options; k++ )
565cdf0e10cSrcweir         {
566cdf0e10cSrcweir             if(!strcmp(pDest->options[k].name, "printer-info"))
567cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aComment=OStringToOUString(pDest->options[k].value, aEncoding);
568cdf0e10cSrcweir             if(!strcmp(pDest->options[k].name, "printer-location"))
569cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aLocation=OStringToOUString(pDest->options[k].value, aEncoding);
570cdf0e10cSrcweir         }
571cdf0e10cSrcweir 
572cdf0e10cSrcweir 
573cdf0e10cSrcweir         OUStringBuffer aBuf( 256 );
574cdf0e10cSrcweir         aBuf.appendAscii( "CUPS:" );
575cdf0e10cSrcweir         aBuf.append( aPrinterName );
576cdf0e10cSrcweir         // note: the parser that goes with the PrinterInfo
577cdf0e10cSrcweir         // is created implicitly by the JobData::operator=()
578cdf0e10cSrcweir         // when it detects the NULL ptr m_pParser.
579cdf0e10cSrcweir         // if we wanted to fill in the parser here this
580cdf0e10cSrcweir         // would mean we'd have to download PPDs for each and
581cdf0e10cSrcweir         // every printer - which would be really bad runtime
582cdf0e10cSrcweir         // behaviour
583cdf0e10cSrcweir         aPrinter.m_aInfo.m_pParser = NULL;
584cdf0e10cSrcweir         aPrinter.m_aInfo.m_aContext.setParser( NULL );
585cdf0e10cSrcweir         std::hash_map< OUString, PPDContext, OUStringHash >::const_iterator c_it = m_aDefaultContexts.find( aPrinterName );
586cdf0e10cSrcweir         if( c_it != m_aDefaultContexts.end() )
587cdf0e10cSrcweir         {
588cdf0e10cSrcweir             aPrinter.m_aInfo.m_pParser = c_it->second.getParser();
589cdf0e10cSrcweir             aPrinter.m_aInfo.m_aContext = c_it->second;
590cdf0e10cSrcweir         }
591cdf0e10cSrcweir         if( bUsePDF && aPrinter.m_aInfo.m_nPSLevel == 0 && aPrinter.m_aInfo.m_nPDFDevice == 0 )
592cdf0e10cSrcweir             aPrinter.m_aInfo.m_nPDFDevice = 1;
593cdf0e10cSrcweir         aPrinter.m_aInfo.m_aDriverName = aBuf.makeStringAndClear();
594cdf0e10cSrcweir         aPrinter.m_bModified = false;
595cdf0e10cSrcweir 
596cdf0e10cSrcweir         m_aPrinters[ aPrinter.m_aInfo.m_aPrinterName ] = aPrinter;
597cdf0e10cSrcweir         m_aCUPSDestMap[ aPrinter.m_aInfo.m_aPrinterName ] = nPrinter;
598cdf0e10cSrcweir     }
599cdf0e10cSrcweir 
600cdf0e10cSrcweir     // remove everything that is not a CUPS printer and not
601cdf0e10cSrcweir     // a special purpose printer (PDF, Fax)
602cdf0e10cSrcweir     std::list< OUString > aRemovePrinters;
603cdf0e10cSrcweir     for( std::hash_map< OUString, Printer, OUStringHash >::iterator it = m_aPrinters.begin();
604cdf0e10cSrcweir          it != m_aPrinters.end(); ++it )
605cdf0e10cSrcweir     {
606cdf0e10cSrcweir         if( m_aCUPSDestMap.find( it->first ) != m_aCUPSDestMap.end() )
607cdf0e10cSrcweir             continue;
608cdf0e10cSrcweir 
609cdf0e10cSrcweir         if( it->second.m_aInfo.m_aFeatures.getLength() > 0 )
610cdf0e10cSrcweir             continue;
611cdf0e10cSrcweir         aRemovePrinters.push_back( it->first );
612cdf0e10cSrcweir     }
613cdf0e10cSrcweir     while( aRemovePrinters.begin() != aRemovePrinters.end() )
614cdf0e10cSrcweir     {
615cdf0e10cSrcweir         m_aPrinters.erase( aRemovePrinters.front() );
616cdf0e10cSrcweir         aRemovePrinters.pop_front();
617cdf0e10cSrcweir     }
618cdf0e10cSrcweir 
619cdf0e10cSrcweir     m_pCUPSWrapper->cupsSetPasswordCB( setPasswordCallback );
620cdf0e10cSrcweir #endif // ENABLE_CUPS
621cdf0e10cSrcweir }
622cdf0e10cSrcweir 
623cdf0e10cSrcweir #ifdef ENABLE_CUPS
updatePrinterContextInfo(ppd_group_t * pPPDGroup,PPDContext & rContext)624cdf0e10cSrcweir static void updatePrinterContextInfo( ppd_group_t* pPPDGroup, PPDContext& rContext )
625cdf0e10cSrcweir {
626cdf0e10cSrcweir     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
627cdf0e10cSrcweir     for( int i = 0; i < pPPDGroup->num_options; i++ )
628cdf0e10cSrcweir     {
629cdf0e10cSrcweir         ppd_option_t* pOption = pPPDGroup->options + i;
630cdf0e10cSrcweir         for( int n = 0; n < pOption->num_choices; n++ )
631cdf0e10cSrcweir         {
632cdf0e10cSrcweir             ppd_choice_t* pChoice = pOption->choices + n;
633cdf0e10cSrcweir             if( pChoice->marked )
634cdf0e10cSrcweir             {
635cdf0e10cSrcweir                 const PPDKey* pKey = rContext.getParser()->getKey( OStringToOUString( pOption->keyword, aEncoding ) );
636cdf0e10cSrcweir                 if( pKey )
637cdf0e10cSrcweir                 {
638cdf0e10cSrcweir                     const PPDValue* pValue = pKey->getValue( OStringToOUString( pChoice->choice, aEncoding ) );
639cdf0e10cSrcweir                     if( pValue )
640cdf0e10cSrcweir                     {
641cdf0e10cSrcweir                         if( pValue != pKey->getDefaultValue() )
642cdf0e10cSrcweir                         {
643cdf0e10cSrcweir                             rContext.setValue( pKey, pValue, true );
644cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
645cdf0e10cSrcweir                             fprintf( stderr, "key %s is set to %s\n", pOption->keyword, pChoice->choice );
646cdf0e10cSrcweir #endif
647cdf0e10cSrcweir 
648cdf0e10cSrcweir                         }
649cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
650cdf0e10cSrcweir                         else
651cdf0e10cSrcweir                             fprintf( stderr, "key %s is defaulted to %s\n", pOption->keyword, pChoice->choice );
652cdf0e10cSrcweir #endif
653cdf0e10cSrcweir                     }
654cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
655cdf0e10cSrcweir                     else
656cdf0e10cSrcweir                         fprintf( stderr, "caution: value %s not found in key %s\n", pChoice->choice, pOption->keyword );
657cdf0e10cSrcweir #endif
658cdf0e10cSrcweir                 }
659cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
660cdf0e10cSrcweir                 else
661cdf0e10cSrcweir                     fprintf( stderr, "caution: key %s not found in parser\n", pOption->keyword );
662cdf0e10cSrcweir #endif
663cdf0e10cSrcweir             }
664cdf0e10cSrcweir         }
665cdf0e10cSrcweir     }
666cdf0e10cSrcweir 
667cdf0e10cSrcweir     // recurse through subgroups
668cdf0e10cSrcweir     for( int g = 0; g < pPPDGroup->num_subgroups; g++ )
669cdf0e10cSrcweir     {
670cdf0e10cSrcweir         updatePrinterContextInfo( pPPDGroup->subgroups + g, rContext );
671cdf0e10cSrcweir     }
672cdf0e10cSrcweir }
673cdf0e10cSrcweir #endif // ENABLE_CUPS
674cdf0e10cSrcweir 
createCUPSParser(const OUString & rPrinter)675cdf0e10cSrcweir const PPDParser* CUPSManager::createCUPSParser( const OUString& rPrinter )
676cdf0e10cSrcweir {
677cdf0e10cSrcweir     const PPDParser* pNewParser = NULL;
678cdf0e10cSrcweir     OUString aPrinter;
679cdf0e10cSrcweir 
680cdf0e10cSrcweir     if( rPrinter.compareToAscii( "CUPS:", 5 ) == 0 )
681cdf0e10cSrcweir         aPrinter = rPrinter.copy( 5 );
682cdf0e10cSrcweir     else
683cdf0e10cSrcweir         aPrinter = rPrinter;
684cdf0e10cSrcweir 
685cdf0e10cSrcweir #ifdef ENABLE_CUPS
686cdf0e10cSrcweir     if( m_aCUPSMutex.tryToAcquire() )
687cdf0e10cSrcweir     {
688cdf0e10cSrcweir         if( m_nDests && m_pDests && ! isCUPSDisabled() )
689cdf0e10cSrcweir         {
690cdf0e10cSrcweir             std::hash_map< OUString, int, OUStringHash >::iterator dest_it =
691cdf0e10cSrcweir             m_aCUPSDestMap.find( aPrinter );
692cdf0e10cSrcweir             if( dest_it != m_aCUPSDestMap.end() )
693cdf0e10cSrcweir             {
694cdf0e10cSrcweir                 cups_dest_t* pDest = ((cups_dest_t*)m_pDests) + dest_it->second;
695cdf0e10cSrcweir                 OString aPPDFile = m_pCUPSWrapper->cupsGetPPD( pDest->name );
696cdf0e10cSrcweir                 #if OSL_DEBUG_LEVEL > 1
697cdf0e10cSrcweir                 fprintf( stderr, "PPD for %s is %s\n", OUStringToOString( aPrinter, osl_getThreadTextEncoding() ).getStr(), aPPDFile.getStr() );
698cdf0e10cSrcweir                 #endif
699cdf0e10cSrcweir                 if( aPPDFile.getLength() )
700cdf0e10cSrcweir                 {
701cdf0e10cSrcweir                     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
702cdf0e10cSrcweir                     OUString aFileName( OStringToOUString( aPPDFile, aEncoding ) );
703cdf0e10cSrcweir                     // update the printer info with context information
704cdf0e10cSrcweir                     ppd_file_t* pPPD = m_pCUPSWrapper->ppdOpenFile( aPPDFile.getStr() );
705cdf0e10cSrcweir                     if( pPPD )
706cdf0e10cSrcweir                     {
707cdf0e10cSrcweir                         // create the new parser
708cdf0e10cSrcweir                         PPDParser* pCUPSParser = new PPDParser( aFileName );
709cdf0e10cSrcweir                         pCUPSParser->m_aFile = rPrinter;
710cdf0e10cSrcweir                         pNewParser = pCUPSParser;
711cdf0e10cSrcweir 
712cdf0e10cSrcweir                         /*int nConflicts =*/ m_pCUPSWrapper->cupsMarkOptions( pPPD, pDest->num_options, pDest->options );
713cdf0e10cSrcweir                         #if OSL_DEBUG_LEVEL > 1
714cdf0e10cSrcweir                         fprintf( stderr, "processing the following options for printer %s (instance %s):\n",
715cdf0e10cSrcweir                         pDest->name, pDest->instance );
716cdf0e10cSrcweir                         for( int k = 0; k < pDest->num_options; k++ )
717cdf0e10cSrcweir                             fprintf( stderr, "   \"%s\" = \"%s\"\n",
718cdf0e10cSrcweir                         pDest->options[k].name,
719cdf0e10cSrcweir                         pDest->options[k].value );
720cdf0e10cSrcweir                         #endif
721cdf0e10cSrcweir                         PrinterInfo& rInfo = m_aPrinters[ aPrinter ].m_aInfo;
722cdf0e10cSrcweir 
723cdf0e10cSrcweir                         // remember the default context for later use
724cdf0e10cSrcweir                         PPDContext& rContext = m_aDefaultContexts[ aPrinter ];
725cdf0e10cSrcweir                         rContext.setParser( pNewParser );
726cdf0e10cSrcweir                         // set system default paper; printer CUPS PPD options
727cdf0e10cSrcweir                         // may overwrite it
728cdf0e10cSrcweir                         setDefaultPaper( rContext );
729cdf0e10cSrcweir                         for( int i = 0; i < pPPD->num_groups; i++ )
730cdf0e10cSrcweir                             updatePrinterContextInfo( pPPD->groups + i, rContext );
731cdf0e10cSrcweir 
732cdf0e10cSrcweir                         rInfo.m_pParser = pNewParser;
733cdf0e10cSrcweir                         rInfo.m_aContext = rContext;
734cdf0e10cSrcweir 
735cdf0e10cSrcweir                         // clean up the mess
736cdf0e10cSrcweir                         m_pCUPSWrapper->ppdClose( pPPD );
737cdf0e10cSrcweir                     }
738cdf0e10cSrcweir                     #if OSL_DEBUG_LEVEL > 1
739cdf0e10cSrcweir                     else
740cdf0e10cSrcweir                         fprintf( stderr, "ppdOpenFile failed, falling back to generic driver\n" );
741cdf0e10cSrcweir                     #endif
742cdf0e10cSrcweir 
743cdf0e10cSrcweir                     // remove temporary PPD file
744cdf0e10cSrcweir                     unlink( aPPDFile.getStr() );
745cdf0e10cSrcweir                 }
746cdf0e10cSrcweir                 #if OSL_DEBUG_LEVEL > 1
747cdf0e10cSrcweir                 else
748cdf0e10cSrcweir                     fprintf( stderr, "cupsGetPPD failed, falling back to generic driver\n" );
749cdf0e10cSrcweir                 #endif
750cdf0e10cSrcweir             }
751cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 1
752cdf0e10cSrcweir             else
753cdf0e10cSrcweir                 fprintf( stderr, "no dest found for printer %s\n", OUStringToOString( aPrinter, osl_getThreadTextEncoding() ).getStr() );
754cdf0e10cSrcweir             #endif
755cdf0e10cSrcweir         }
756cdf0e10cSrcweir         m_aCUPSMutex.release();
757cdf0e10cSrcweir     }
758cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL >1
759cdf0e10cSrcweir     else
760cdf0e10cSrcweir         fprintf( stderr, "could not acquire CUPS mutex !!!\n" );
761cdf0e10cSrcweir     #endif
762cdf0e10cSrcweir     #endif // ENABLE_CUPS
763cdf0e10cSrcweir 
764cdf0e10cSrcweir     if( ! pNewParser )
765cdf0e10cSrcweir     {
766cdf0e10cSrcweir         // get the default PPD
767cdf0e10cSrcweir         pNewParser = PPDParser::getParser( String( RTL_CONSTASCII_USTRINGPARAM( "SGENPRT" ) ) );
768cdf0e10cSrcweir 
769cdf0e10cSrcweir         PrinterInfo& rInfo = m_aPrinters[ aPrinter ].m_aInfo;
770cdf0e10cSrcweir 
771cdf0e10cSrcweir         rInfo.m_pParser = pNewParser;
772cdf0e10cSrcweir         rInfo.m_aContext.setParser( pNewParser );
773cdf0e10cSrcweir     }
774cdf0e10cSrcweir 
775cdf0e10cSrcweir     return pNewParser;
776cdf0e10cSrcweir }
777cdf0e10cSrcweir 
setupJobContextData(JobData & rData)778cdf0e10cSrcweir void CUPSManager::setupJobContextData(
779cdf0e10cSrcweir     JobData&
780cdf0e10cSrcweir #ifdef ENABLE_CUPS
781cdf0e10cSrcweir     rData
782cdf0e10cSrcweir #endif
783cdf0e10cSrcweir )
784cdf0e10cSrcweir {
785cdf0e10cSrcweir #ifdef ENABLE_CUPS
786cdf0e10cSrcweir     std::hash_map< OUString, int, OUStringHash >::iterator dest_it =
787cdf0e10cSrcweir         m_aCUPSDestMap.find( rData.m_aPrinterName );
788cdf0e10cSrcweir 
789cdf0e10cSrcweir     if( dest_it == m_aCUPSDestMap.end() )
790cdf0e10cSrcweir         return PrinterInfoManager::setupJobContextData( rData );
791cdf0e10cSrcweir 
792cdf0e10cSrcweir     std::hash_map< OUString, Printer, OUStringHash >::iterator p_it =
793cdf0e10cSrcweir         m_aPrinters.find( rData.m_aPrinterName );
794cdf0e10cSrcweir     if( p_it == m_aPrinters.end() ) // huh ?
795cdf0e10cSrcweir     {
796cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
797cdf0e10cSrcweir         fprintf( stderr, "CUPS printer list in disorder, no dest for printer %s !\n", OUStringToOString( rData.m_aPrinterName, osl_getThreadTextEncoding() ).getStr() );
798cdf0e10cSrcweir #endif
799cdf0e10cSrcweir         return;
800cdf0e10cSrcweir     }
801cdf0e10cSrcweir 
802cdf0e10cSrcweir     if( p_it->second.m_aInfo.m_pParser == NULL )
803cdf0e10cSrcweir     {
804cdf0e10cSrcweir         // in turn calls createCUPSParser
805cdf0e10cSrcweir         // which updates the printer info
806cdf0e10cSrcweir         p_it->second.m_aInfo.m_pParser = PPDParser::getParser( p_it->second.m_aInfo.m_aDriverName );
807cdf0e10cSrcweir     }
808cdf0e10cSrcweir     if( p_it->second.m_aInfo.m_aContext.getParser() == NULL )
809cdf0e10cSrcweir     {
810cdf0e10cSrcweir         OUString aPrinter;
811cdf0e10cSrcweir         if( p_it->second.m_aInfo.m_aDriverName.compareToAscii( "CUPS:", 5 ) == 0 )
812cdf0e10cSrcweir             aPrinter = p_it->second.m_aInfo.m_aDriverName.copy( 5 );
813cdf0e10cSrcweir         else
814cdf0e10cSrcweir             aPrinter = p_it->second.m_aInfo.m_aDriverName;
815cdf0e10cSrcweir 
816cdf0e10cSrcweir         p_it->second.m_aInfo.m_aContext = m_aDefaultContexts[ aPrinter ];
817cdf0e10cSrcweir     }
818cdf0e10cSrcweir 
819cdf0e10cSrcweir     rData.m_pParser		= p_it->second.m_aInfo.m_pParser;
820cdf0e10cSrcweir     rData.m_aContext	= p_it->second.m_aInfo.m_aContext;
821cdf0e10cSrcweir #endif
822cdf0e10cSrcweir }
823cdf0e10cSrcweir 
startSpool(const OUString & rPrintername,bool bQuickCommand)824cdf0e10cSrcweir FILE* CUPSManager::startSpool( const OUString& rPrintername, bool bQuickCommand )
825cdf0e10cSrcweir {
826cdf0e10cSrcweir     OSL_TRACE( "endSpool: %s, %s",
827cdf0e10cSrcweir                rtl::OUStringToOString( rPrintername, RTL_TEXTENCODING_UTF8 ).getStr(),
828cdf0e10cSrcweir               bQuickCommand ? "true" : "false" );
829cdf0e10cSrcweir 
830cdf0e10cSrcweir     if( m_aCUPSDestMap.find( rPrintername ) == m_aCUPSDestMap.end() )
831cdf0e10cSrcweir     {
832cdf0e10cSrcweir         OSL_TRACE( "defer to PrinterInfoManager::startSpool" );
833cdf0e10cSrcweir         return PrinterInfoManager::startSpool( rPrintername, bQuickCommand );
834cdf0e10cSrcweir     }
835cdf0e10cSrcweir 
836cdf0e10cSrcweir #ifdef ENABLE_CUPS
837cdf0e10cSrcweir     OUString aTmpURL, aTmpFile;
838cdf0e10cSrcweir     osl_createTempFile( NULL, NULL, &aTmpURL.pData );
839cdf0e10cSrcweir     osl_getSystemPathFromFileURL( aTmpURL.pData, &aTmpFile.pData );
840cdf0e10cSrcweir     OString aSysFile = OUStringToOString( aTmpFile, osl_getThreadTextEncoding() );
841cdf0e10cSrcweir     FILE* fp = fopen( aSysFile.getStr(), "w" );
842cdf0e10cSrcweir     if( fp )
843cdf0e10cSrcweir         m_aSpoolFiles[fp] = aSysFile;
844cdf0e10cSrcweir 
845cdf0e10cSrcweir     return fp;
846cdf0e10cSrcweir #else
847cdf0e10cSrcweir     return NULL;
848cdf0e10cSrcweir #endif
849cdf0e10cSrcweir }
850cdf0e10cSrcweir 
851cdf0e10cSrcweir struct less_ppd_key : public ::std::binary_function<double, double, bool>
852cdf0e10cSrcweir {
operator ()less_ppd_key853cdf0e10cSrcweir     bool operator()(const PPDKey* left, const PPDKey* right)
854cdf0e10cSrcweir     { return left->getOrderDependency() < right->getOrderDependency(); }
855cdf0e10cSrcweir };
856cdf0e10cSrcweir 
getOptionsFromDocumentSetup(const JobData & rJob,bool bBanner,int & rNumOptions,void ** rOptions) const857cdf0e10cSrcweir void CUPSManager::getOptionsFromDocumentSetup( const JobData& rJob, bool bBanner, int& rNumOptions, void** rOptions ) const
858cdf0e10cSrcweir {
859cdf0e10cSrcweir     rNumOptions = 0;
860cdf0e10cSrcweir     *rOptions = NULL;
861cdf0e10cSrcweir     int i;
862cdf0e10cSrcweir 
863cdf0e10cSrcweir     // emit features ordered to OrderDependency
864cdf0e10cSrcweir     // ignore features that are set to default
865cdf0e10cSrcweir 
866cdf0e10cSrcweir     // sanity check
867cdf0e10cSrcweir     if( rJob.m_pParser == rJob.m_aContext.getParser() && rJob.m_pParser )
868cdf0e10cSrcweir     {
869cdf0e10cSrcweir         int nKeys = rJob.m_aContext.countValuesModified();
870cdf0e10cSrcweir         ::std::vector< const PPDKey* > aKeys( nKeys );
871cdf0e10cSrcweir         for(  i = 0; i < nKeys; i++ )
872cdf0e10cSrcweir             aKeys[i] = rJob.m_aContext.getModifiedKey( i );
873cdf0e10cSrcweir         ::std::sort( aKeys.begin(), aKeys.end(), less_ppd_key() );
874cdf0e10cSrcweir 
875cdf0e10cSrcweir         for( i = 0; i < nKeys; i++ )
876cdf0e10cSrcweir         {
877cdf0e10cSrcweir             const PPDKey* pKey = aKeys[i];
878cdf0e10cSrcweir             const PPDValue* pValue = rJob.m_aContext.getValue( pKey );
879cdf0e10cSrcweir             if(pValue && pValue->m_eType == eInvocation && pValue->m_aValue.Len() )
880cdf0e10cSrcweir             {
881cdf0e10cSrcweir                 OString aKey = OUStringToOString( pKey->getKey(), RTL_TEXTENCODING_ASCII_US );
882cdf0e10cSrcweir                 OString aValue = OUStringToOString( pValue->m_aOption, RTL_TEXTENCODING_ASCII_US );
883cdf0e10cSrcweir                 rNumOptions = m_pCUPSWrapper->cupsAddOption( aKey.getStr(), aValue.getStr(), rNumOptions, (cups_option_t**)rOptions );
884cdf0e10cSrcweir             }
885cdf0e10cSrcweir         }
886cdf0e10cSrcweir     }
887cdf0e10cSrcweir 
888cdf0e10cSrcweir     if( rJob.m_nPDFDevice > 0 && rJob.m_nCopies > 1 )
889cdf0e10cSrcweir     {
890cdf0e10cSrcweir         rtl::OString aVal( rtl::OString::valueOf( sal_Int32( rJob.m_nCopies ) ) );
891cdf0e10cSrcweir         rNumOptions = m_pCUPSWrapper->cupsAddOption( "copies", aVal.getStr(), rNumOptions, (cups_option_t**)rOptions );
892cdf0e10cSrcweir     }
893cdf0e10cSrcweir     if( ! bBanner )
894cdf0e10cSrcweir     {
895cdf0e10cSrcweir         rNumOptions = m_pCUPSWrapper->cupsAddOption( "job-sheets", "none", rNumOptions, (cups_option_t**)rOptions );
896cdf0e10cSrcweir     }
897cdf0e10cSrcweir }
898cdf0e10cSrcweir 
endSpool(const OUString & rPrintername,const OUString & rJobTitle,FILE * pFile,const JobData & rDocumentJobData,bool bBanner)899cdf0e10cSrcweir int CUPSManager::endSpool( const OUString& rPrintername, const OUString& rJobTitle, FILE* pFile, const JobData& rDocumentJobData, bool bBanner )
900cdf0e10cSrcweir {
901cdf0e10cSrcweir     OSL_TRACE( "endSpool: %s, %s, copy count = %d",
902cdf0e10cSrcweir                rtl::OUStringToOString( rPrintername, RTL_TEXTENCODING_UTF8 ).getStr(),
903cdf0e10cSrcweir                rtl::OUStringToOString( rJobTitle, RTL_TEXTENCODING_UTF8 ).getStr(),
904cdf0e10cSrcweir                rDocumentJobData.m_nCopies
905cdf0e10cSrcweir                );
906cdf0e10cSrcweir 
907cdf0e10cSrcweir     int nJobID = 0;
908cdf0e10cSrcweir 
909cdf0e10cSrcweir     osl::MutexGuard aGuard( m_aCUPSMutex );
910cdf0e10cSrcweir 
911cdf0e10cSrcweir     std::hash_map< OUString, int, OUStringHash >::iterator dest_it =
912cdf0e10cSrcweir         m_aCUPSDestMap.find( rPrintername );
913cdf0e10cSrcweir     if( dest_it == m_aCUPSDestMap.end() )
914cdf0e10cSrcweir     {
915cdf0e10cSrcweir         OSL_TRACE( "defer to PrinterInfoManager::endSpool" );
916cdf0e10cSrcweir         return PrinterInfoManager::endSpool( rPrintername, rJobTitle, pFile, rDocumentJobData, bBanner );
917cdf0e10cSrcweir     }
918cdf0e10cSrcweir 
919cdf0e10cSrcweir     #ifdef ENABLE_CUPS
920cdf0e10cSrcweir     std::hash_map< FILE*, OString, FPtrHash >::const_iterator it = m_aSpoolFiles.find( pFile );
921cdf0e10cSrcweir     if( it != m_aSpoolFiles.end() )
922cdf0e10cSrcweir     {
923cdf0e10cSrcweir         fclose( pFile );
924cdf0e10cSrcweir         rtl_TextEncoding aEnc = osl_getThreadTextEncoding();
925cdf0e10cSrcweir 
926cdf0e10cSrcweir         // setup cups options
927cdf0e10cSrcweir         int nNumOptions = 0;
928cdf0e10cSrcweir         cups_option_t* pOptions = NULL;
929cdf0e10cSrcweir         getOptionsFromDocumentSetup( rDocumentJobData, bBanner, nNumOptions, (void**)&pOptions );
930cdf0e10cSrcweir 
931cdf0e10cSrcweir         cups_dest_t* pDest = ((cups_dest_t*)m_pDests) + dest_it->second;
932cdf0e10cSrcweir         nJobID = m_pCUPSWrapper->cupsPrintFile( pDest->name,
933cdf0e10cSrcweir         it->second.getStr(),
934cdf0e10cSrcweir         OUStringToOString( rJobTitle, aEnc ).getStr(),
935cdf0e10cSrcweir         nNumOptions, pOptions );
936cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
937cdf0e10cSrcweir         fprintf( stderr, "cupsPrintFile( %s, %s, %s, %d, %p ) returns %d\n",
938cdf0e10cSrcweir                     pDest->name,
939cdf0e10cSrcweir                     it->second.getStr(),
940cdf0e10cSrcweir                     OUStringToOString( rJobTitle, aEnc ).getStr(),
941cdf0e10cSrcweir                     nNumOptions,
942cdf0e10cSrcweir                     pOptions,
943cdf0e10cSrcweir                     nJobID
944cdf0e10cSrcweir                     );
945cdf0e10cSrcweir         for( int n = 0; n < nNumOptions; n++ )
946cdf0e10cSrcweir             fprintf( stderr, "    option %s=%s\n", pOptions[n].name, pOptions[n].value );
947cdf0e10cSrcweir         OString aCmd( "cp " );
948cdf0e10cSrcweir         aCmd = aCmd + it->second;
949cdf0e10cSrcweir         aCmd = aCmd + OString( " $HOME/cupsprint.ps" );
950cdf0e10cSrcweir         system( aCmd.getStr() );
951cdf0e10cSrcweir #endif
952cdf0e10cSrcweir 
953cdf0e10cSrcweir         unlink( it->second.getStr() );
954cdf0e10cSrcweir         m_aSpoolFiles.erase( pFile );
955cdf0e10cSrcweir         if( pOptions )
956cdf0e10cSrcweir             m_pCUPSWrapper->cupsFreeOptions( nNumOptions, pOptions );
957cdf0e10cSrcweir     }
958cdf0e10cSrcweir #endif // ENABLE_CUPS
959cdf0e10cSrcweir 
960cdf0e10cSrcweir     return nJobID;
961cdf0e10cSrcweir }
962cdf0e10cSrcweir 
963cdf0e10cSrcweir 
changePrinterInfo(const OUString & rPrinter,const PrinterInfo & rNewInfo)964cdf0e10cSrcweir void CUPSManager::changePrinterInfo( const OUString& rPrinter, const PrinterInfo& rNewInfo )
965cdf0e10cSrcweir {
966cdf0e10cSrcweir     PrinterInfoManager::changePrinterInfo( rPrinter, rNewInfo );
967cdf0e10cSrcweir }
968cdf0e10cSrcweir 
checkPrintersChanged(bool bWait)969cdf0e10cSrcweir bool CUPSManager::checkPrintersChanged( bool bWait )
970cdf0e10cSrcweir {
971cdf0e10cSrcweir     bool bChanged = false;
972cdf0e10cSrcweir     if( bWait )
973cdf0e10cSrcweir     {
974cdf0e10cSrcweir         if(  m_aDestThread )
975cdf0e10cSrcweir         {
976cdf0e10cSrcweir             // initial asynchronous detection still running
977cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 1
978cdf0e10cSrcweir             fprintf( stderr, "syncing cups discovery thread\n" );
979cdf0e10cSrcweir             #endif
980cdf0e10cSrcweir             osl_joinWithThread( m_aDestThread );
981cdf0e10cSrcweir             osl_destroyThread( m_aDestThread );
982cdf0e10cSrcweir             m_aDestThread = NULL;
983cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 1
984cdf0e10cSrcweir             fprintf( stderr, "done: syncing cups discovery thread\n" );
985cdf0e10cSrcweir             #endif
986cdf0e10cSrcweir         }
987cdf0e10cSrcweir         else
988cdf0e10cSrcweir         {
989cdf0e10cSrcweir             // #i82321# check for cups printer updates
990cdf0e10cSrcweir             // with this change the whole asynchronous detection in a thread is
991cdf0e10cSrcweir             // almost useless. The only relevance left is for some stalled systems
992cdf0e10cSrcweir             // where the user can set SAL_DISABLE_SYNCHRONOUS_PRINTER_DETECTION
993cdf0e10cSrcweir             // (see vcl/unx/source/gdi/salprnpsp.cxx)
994cdf0e10cSrcweir             // so that checkPrintersChanged( true ) will never be called
995cdf0e10cSrcweir 
996cdf0e10cSrcweir             // there is no way to query CUPS whether the printer list has changed
997cdf0e10cSrcweir             // so get the dest list anew
998cdf0e10cSrcweir             if( m_nDests && m_pDests )
999cdf0e10cSrcweir                 m_pCUPSWrapper->cupsFreeDests( m_nDests, (cups_dest_t*)m_pDests );
1000cdf0e10cSrcweir             m_nDests = 0;
1001cdf0e10cSrcweir             m_pDests = NULL;
1002cdf0e10cSrcweir             runDests();
1003cdf0e10cSrcweir         }
1004cdf0e10cSrcweir     }
1005cdf0e10cSrcweir     if( m_aCUPSMutex.tryToAcquire() )
1006cdf0e10cSrcweir     {
1007cdf0e10cSrcweir         bChanged = m_bNewDests;
1008cdf0e10cSrcweir         m_aCUPSMutex.release();
1009cdf0e10cSrcweir     }
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir     if( ! bChanged )
1012cdf0e10cSrcweir     {
1013cdf0e10cSrcweir         bChanged = PrinterInfoManager::checkPrintersChanged( bWait );
1014cdf0e10cSrcweir         // #i54375# ensure new merging with CUPS list in :initialize
1015cdf0e10cSrcweir         if( bChanged )
1016cdf0e10cSrcweir             m_bNewDests = true;
1017cdf0e10cSrcweir     }
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir     if( bChanged )
1020cdf0e10cSrcweir         initialize();
1021cdf0e10cSrcweir 
1022cdf0e10cSrcweir     return bChanged;
1023cdf0e10cSrcweir }
1024cdf0e10cSrcweir 
addPrinter(const OUString & rName,const OUString & rDriver)1025cdf0e10cSrcweir bool CUPSManager::addPrinter( const OUString& rName, const OUString& rDriver )
1026cdf0e10cSrcweir {
1027cdf0e10cSrcweir     // don't touch the CUPS printers
1028cdf0e10cSrcweir     if( m_aCUPSDestMap.find( rName ) != m_aCUPSDestMap.end() ||
1029cdf0e10cSrcweir         rDriver.compareToAscii( "CUPS:", 5 ) == 0
1030cdf0e10cSrcweir         )
1031cdf0e10cSrcweir         return false;
1032cdf0e10cSrcweir     return PrinterInfoManager::addPrinter( rName, rDriver );
1033cdf0e10cSrcweir }
1034cdf0e10cSrcweir 
removePrinter(const OUString & rName,bool bCheck)1035cdf0e10cSrcweir bool CUPSManager::removePrinter( const OUString& rName, bool bCheck )
1036cdf0e10cSrcweir {
1037cdf0e10cSrcweir     // don't touch the CUPS printers
1038cdf0e10cSrcweir     if( m_aCUPSDestMap.find( rName ) != m_aCUPSDestMap.end() )
1039cdf0e10cSrcweir         return false;
1040cdf0e10cSrcweir     return PrinterInfoManager::removePrinter( rName, bCheck );
1041cdf0e10cSrcweir }
1042cdf0e10cSrcweir 
setDefaultPrinter(const OUString & rName)1043cdf0e10cSrcweir bool CUPSManager::setDefaultPrinter( const OUString& rName )
1044cdf0e10cSrcweir {
1045cdf0e10cSrcweir     bool bSuccess = false;
1046cdf0e10cSrcweir #ifdef ENABLE_CUPS
1047cdf0e10cSrcweir     std::hash_map< OUString, int, OUStringHash >::iterator nit =
1048cdf0e10cSrcweir         m_aCUPSDestMap.find( rName );
1049cdf0e10cSrcweir     if( nit != m_aCUPSDestMap.end() && m_aCUPSMutex.tryToAcquire() )
1050cdf0e10cSrcweir     {
1051cdf0e10cSrcweir         cups_dest_t* pDests = (cups_dest_t*)m_pDests;
1052cdf0e10cSrcweir         for( int i = 0; i < m_nDests; i++ )
1053cdf0e10cSrcweir             pDests[i].is_default = 0;
1054cdf0e10cSrcweir         pDests[ nit->second ].is_default = 1;
1055cdf0e10cSrcweir         m_pCUPSWrapper->cupsSetDests( m_nDests, (cups_dest_t*)m_pDests );
1056cdf0e10cSrcweir         m_aDefaultPrinter = rName;
1057cdf0e10cSrcweir         m_aCUPSMutex.release();
1058cdf0e10cSrcweir         bSuccess = true;
1059cdf0e10cSrcweir     }
1060cdf0e10cSrcweir     else
1061cdf0e10cSrcweir #endif
1062cdf0e10cSrcweir         bSuccess = PrinterInfoManager::setDefaultPrinter( rName );
1063cdf0e10cSrcweir 
1064cdf0e10cSrcweir     return bSuccess;
1065cdf0e10cSrcweir }
1066cdf0e10cSrcweir 
writePrinterConfig()1067cdf0e10cSrcweir bool CUPSManager::writePrinterConfig()
1068cdf0e10cSrcweir {
1069cdf0e10cSrcweir #ifdef ENABLE_CUPS
1070cdf0e10cSrcweir     bool bDestModified = false;
1071cdf0e10cSrcweir     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
1072cdf0e10cSrcweir 
1073cdf0e10cSrcweir     for( std::hash_map< OUString, Printer, OUStringHash >::iterator prt =
1074cdf0e10cSrcweir              m_aPrinters.begin(); prt != m_aPrinters.end(); ++prt )
1075cdf0e10cSrcweir     {
1076cdf0e10cSrcweir         std::hash_map< OUString, int, OUStringHash >::iterator nit =
1077cdf0e10cSrcweir             m_aCUPSDestMap.find( prt->first );
1078cdf0e10cSrcweir         if( nit == m_aCUPSDestMap.end() )
1079cdf0e10cSrcweir             continue;
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir         if( ! prt->second.m_bModified )
1082cdf0e10cSrcweir             continue;
1083cdf0e10cSrcweir 
1084cdf0e10cSrcweir         if( m_aCUPSMutex.tryToAcquire() )
1085cdf0e10cSrcweir         {
1086cdf0e10cSrcweir             bDestModified = true;
1087cdf0e10cSrcweir             cups_dest_t* pDest = ((cups_dest_t*)m_pDests) + nit->second;
1088cdf0e10cSrcweir             PrinterInfo& rInfo = prt->second.m_aInfo;
1089cdf0e10cSrcweir 
1090cdf0e10cSrcweir             // create new option list
1091cdf0e10cSrcweir             int nNewOptions = 0;
1092cdf0e10cSrcweir             cups_option_t* pNewOptions = NULL;
1093cdf0e10cSrcweir             int nValues = rInfo.m_aContext.countValuesModified();
1094cdf0e10cSrcweir             for( int i = 0; i < nValues; i++ )
1095cdf0e10cSrcweir             {
1096cdf0e10cSrcweir                 const PPDKey* pKey = rInfo.m_aContext.getModifiedKey( i );
1097cdf0e10cSrcweir                 const PPDValue* pValue = rInfo.m_aContext.getValue( pKey );
1098cdf0e10cSrcweir                 if( pKey && pValue ) // sanity check
1099cdf0e10cSrcweir                 {
1100cdf0e10cSrcweir                     OString aName = OUStringToOString( pKey->getKey(), aEncoding );
1101cdf0e10cSrcweir                     OString aValue = OUStringToOString( pValue->m_aOption, aEncoding );
1102cdf0e10cSrcweir                     nNewOptions = m_pCUPSWrapper->cupsAddOption( aName.getStr(), aValue.getStr(), nNewOptions, &pNewOptions );
1103cdf0e10cSrcweir                 }
1104cdf0e10cSrcweir             }
1105cdf0e10cSrcweir             // set PPD options on CUPS dest
1106cdf0e10cSrcweir             m_pCUPSWrapper->cupsFreeOptions( pDest->num_options, pDest->options );
1107cdf0e10cSrcweir             pDest->num_options = nNewOptions;
1108cdf0e10cSrcweir             pDest->options = pNewOptions;
1109cdf0e10cSrcweir             m_aCUPSMutex.release();
1110cdf0e10cSrcweir         }
1111cdf0e10cSrcweir     }
1112cdf0e10cSrcweir     if( bDestModified && m_aCUPSMutex.tryToAcquire() )
1113cdf0e10cSrcweir     {
1114cdf0e10cSrcweir         m_pCUPSWrapper->cupsSetDests( m_nDests, (cups_dest_t*)m_pDests );
1115cdf0e10cSrcweir         m_aCUPSMutex.release();
1116cdf0e10cSrcweir     }
1117cdf0e10cSrcweir #endif // ENABLE_CUPS
1118cdf0e10cSrcweir 
1119cdf0e10cSrcweir     return PrinterInfoManager::writePrinterConfig();
1120cdf0e10cSrcweir }
1121cdf0e10cSrcweir 
addOrRemovePossible() const1122cdf0e10cSrcweir bool CUPSManager::addOrRemovePossible() const
1123cdf0e10cSrcweir {
1124cdf0e10cSrcweir     return (m_nDests && m_pDests && ! isCUPSDisabled())? false : PrinterInfoManager::addOrRemovePossible();
1125cdf0e10cSrcweir }
1126cdf0e10cSrcweir 
authenticateUser(const char *)1127cdf0e10cSrcweir const char* CUPSManager::authenticateUser( const char* /*pIn*/ )
1128cdf0e10cSrcweir {
1129cdf0e10cSrcweir     const char* pRet = NULL;
1130cdf0e10cSrcweir 
1131cdf0e10cSrcweir #ifdef ENABLE_CUPS
1132aa150a94SHerbert Dürr     oslModule pLib = osl_loadAsciiModule( _XSALSET_LIBNAME, SAL_LOADMODULE_LAZY );
1133cdf0e10cSrcweir     if( pLib )
1134cdf0e10cSrcweir     {
1135cdf0e10cSrcweir         bool (*getpw)( const OString& rServer, OString& rUser, OString& rPw) =
1136aa150a94SHerbert Dürr             (bool(*)(const OString&,OString&,OString&))osl_getAsciiFunctionSymbol( pLib, "Sal_authenticateQuery" );
1137cdf0e10cSrcweir         if( getpw )
1138cdf0e10cSrcweir         {
1139cdf0e10cSrcweir             osl::MutexGuard aGuard( m_aCUPSMutex );
1140cdf0e10cSrcweir 
1141cdf0e10cSrcweir             OString aUser = m_pCUPSWrapper->cupsUser();
1142cdf0e10cSrcweir             OString aServer = m_pCUPSWrapper->cupsServer();
1143cdf0e10cSrcweir             OString aPassword;
1144cdf0e10cSrcweir             if( getpw( aServer, aUser, aPassword ) )
1145cdf0e10cSrcweir             {
1146cdf0e10cSrcweir                 m_aPassword = aPassword;
1147cdf0e10cSrcweir                 m_aUser = aUser;
1148cdf0e10cSrcweir                 m_pCUPSWrapper->cupsSetUser( m_aUser.getStr() );
1149cdf0e10cSrcweir                 pRet = m_aPassword.getStr();
1150cdf0e10cSrcweir             }
1151cdf0e10cSrcweir         }
1152cdf0e10cSrcweir         osl_unloadModule( pLib );
1153cdf0e10cSrcweir     }
1154cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
1155aa150a94SHerbert Dürr     else fprintf( stderr, "loading of module %s failed\n", _XSALSET_LIBNAME );
1156cdf0e10cSrcweir #endif
1157cdf0e10cSrcweir #endif // ENABLE_CUPS
1158cdf0e10cSrcweir 
1159cdf0e10cSrcweir     return pRet;
1160cdf0e10cSrcweir }
1161