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