1*c82f2877SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c82f2877SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c82f2877SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c82f2877SAndrew Rist  * distributed with this work for additional information
6*c82f2877SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c82f2877SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c82f2877SAndrew Rist  * "License"); you may not use this file except in compliance
9*c82f2877SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c82f2877SAndrew Rist  *
11*c82f2877SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c82f2877SAndrew Rist  *
13*c82f2877SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c82f2877SAndrew Rist  * software distributed under the License is distributed on an
15*c82f2877SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c82f2877SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c82f2877SAndrew Rist  * specific language governing permissions and limitations
18*c82f2877SAndrew Rist  * under the License.
19*c82f2877SAndrew Rist  *
20*c82f2877SAndrew Rist  *************************************************************/
21*c82f2877SAndrew Rist 
22*c82f2877SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <unistd.h>
28cdf0e10cSrcweir #include <sys/wait.h>
29cdf0e10cSrcweir #include <signal.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "cupsmgr.hxx"
32cdf0e10cSrcweir #include "vcl/fontmanager.hxx"
33cdf0e10cSrcweir #include "vcl/strhelper.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "unx/saldata.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "tools/urlobj.hxx"
38cdf0e10cSrcweir #include "tools/stream.hxx"
39cdf0e10cSrcweir #include "tools/debug.hxx"
40cdf0e10cSrcweir #include "tools/config.hxx"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include "i18npool/paper.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include "rtl/strbuf.hxx"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "osl/thread.hxx"
47cdf0e10cSrcweir #include "osl/mutex.hxx"
48cdf0e10cSrcweir #include "osl/process.h"
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // filename of configuration files
51cdf0e10cSrcweir #define PRINT_FILENAME  "psprint.conf"
52cdf0e10cSrcweir // the group of the global defaults
53cdf0e10cSrcweir #define GLOBAL_DEFAULTS_GROUP "__Global_Printer_Defaults__"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #include <hash_set>
56cdf0e10cSrcweir 
57cdf0e10cSrcweir using namespace psp;
58cdf0e10cSrcweir using namespace rtl;
59cdf0e10cSrcweir using namespace osl;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir namespace psp
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     class SystemQueueInfo : public Thread
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         mutable Mutex               m_aMutex;
66cdf0e10cSrcweir         bool                        m_bChanged;
67cdf0e10cSrcweir         std::list< PrinterInfoManager::SystemPrintQueue >
68cdf0e10cSrcweir                                     m_aQueues;
69cdf0e10cSrcweir         OUString                    m_aCommand;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         virtual void run();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         public:
74cdf0e10cSrcweir         SystemQueueInfo();
75cdf0e10cSrcweir         ~SystemQueueInfo();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         bool hasChanged() const;
78cdf0e10cSrcweir         OUString getCommand() const;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         // sets changed status to false; therefore not const
81cdf0e10cSrcweir         void getSystemQueues( std::list< PrinterInfoManager::SystemPrintQueue >& rQueues );
82cdf0e10cSrcweir     };
83cdf0e10cSrcweir } // namespace
84cdf0e10cSrcweir 
85cdf0e10cSrcweir /*
86cdf0e10cSrcweir *  class PrinterInfoManager
87cdf0e10cSrcweir */
88cdf0e10cSrcweir 
89cdf0e10cSrcweir // -----------------------------------------------------------------
90cdf0e10cSrcweir 
get()91cdf0e10cSrcweir PrinterInfoManager& PrinterInfoManager::get()
92cdf0e10cSrcweir {
93cdf0e10cSrcweir     SalData* pSalData = GetSalData();
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     if( ! pSalData->m_pPIManager )
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         pSalData->m_pPIManager = CUPSManager::tryLoadCUPS();
98cdf0e10cSrcweir         if( ! pSalData->m_pPIManager )
99cdf0e10cSrcweir             pSalData->m_pPIManager = new PrinterInfoManager();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         pSalData->m_pPIManager->initialize();
102cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
103cdf0e10cSrcweir         fprintf( stderr, "PrinterInfoManager::get create Manager of type %d\n", pSalData->m_pPIManager->getType() );
104cdf0e10cSrcweir         #endif
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     return *pSalData->m_pPIManager;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
release()110cdf0e10cSrcweir void PrinterInfoManager::release()
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     SalData* pSalData = GetSalData();
113cdf0e10cSrcweir     delete pSalData->m_pPIManager;
114cdf0e10cSrcweir     pSalData->m_pPIManager = NULL;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir // -----------------------------------------------------------------
118cdf0e10cSrcweir 
PrinterInfoManager(Type eType)119cdf0e10cSrcweir PrinterInfoManager::PrinterInfoManager( Type eType ) :
120cdf0e10cSrcweir     m_pQueueInfo( NULL ),
121cdf0e10cSrcweir     m_eType( eType ),
122cdf0e10cSrcweir     m_bUseIncludeFeature( false ),
123cdf0e10cSrcweir     m_bUseJobPatch( true ),
124cdf0e10cSrcweir     m_aSystemDefaultPaper( RTL_CONSTASCII_USTRINGPARAM( "A4" ) ),
125cdf0e10cSrcweir     m_bDisableCUPS( false )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     if( eType == Default )
128cdf0e10cSrcweir         m_pQueueInfo = new SystemQueueInfo();
129cdf0e10cSrcweir     initSystemDefaultPaper();
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir // -----------------------------------------------------------------
133cdf0e10cSrcweir 
~PrinterInfoManager()134cdf0e10cSrcweir PrinterInfoManager::~PrinterInfoManager()
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     delete m_pQueueInfo;
137cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 1
138cdf0e10cSrcweir     fprintf( stderr, "PrinterInfoManager: destroyed Manager of type %d\n", getType() );
139cdf0e10cSrcweir     #endif
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir // -----------------------------------------------------------------
143cdf0e10cSrcweir 
isCUPSDisabled() const144cdf0e10cSrcweir bool PrinterInfoManager::isCUPSDisabled() const
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     return m_bDisableCUPS;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir // -----------------------------------------------------------------
150cdf0e10cSrcweir 
setCUPSDisabled(bool bDisable)151cdf0e10cSrcweir void PrinterInfoManager::setCUPSDisabled( bool bDisable )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir     m_bDisableCUPS = bDisable;
154cdf0e10cSrcweir     writePrinterConfig();
155cdf0e10cSrcweir     // actually we know the printers changed
156cdf0e10cSrcweir     // however this triggers reinitialization the right way
157cdf0e10cSrcweir     checkPrintersChanged( true );
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir // -----------------------------------------------------------------
161cdf0e10cSrcweir 
initSystemDefaultPaper()162cdf0e10cSrcweir void PrinterInfoManager::initSystemDefaultPaper()
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     m_aSystemDefaultPaper = rtl::OStringToOUString(
165cdf0e10cSrcweir         PaperInfo::toPSName(PaperInfo::getSystemDefaultPaper().getPaper()),
166cdf0e10cSrcweir         RTL_TEXTENCODING_UTF8);
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir // -----------------------------------------------------------------
170cdf0e10cSrcweir 
checkPrintersChanged(bool bWait)171cdf0e10cSrcweir bool PrinterInfoManager::checkPrintersChanged( bool bWait )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     // check if files were created, deleted or modified since initialize()
174cdf0e10cSrcweir     ::std::list< WatchFile >::const_iterator it;
175cdf0e10cSrcweir     bool bChanged = false;
176cdf0e10cSrcweir     for( it = m_aWatchFiles.begin(); it != m_aWatchFiles.end() && ! bChanged; ++it )
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         DirectoryItem aItem;
179cdf0e10cSrcweir         if( DirectoryItem::get( it->m_aFilePath, aItem ) )
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir             if( it->m_aModified.Seconds != 0 )
182cdf0e10cSrcweir                 bChanged = true; // file probably has vanished
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir         else
185cdf0e10cSrcweir         {
186cdf0e10cSrcweir             FileStatus aStatus( FileStatusMask_ModifyTime );
187cdf0e10cSrcweir             if( aItem.getFileStatus( aStatus ) )
188cdf0e10cSrcweir                 bChanged = true; // unlikely but not impossible
189cdf0e10cSrcweir             else
190cdf0e10cSrcweir             {
191cdf0e10cSrcweir                 TimeValue aModified = aStatus.getModifyTime();
192cdf0e10cSrcweir                 if( aModified.Seconds != it->m_aModified.Seconds )
193cdf0e10cSrcweir                     bChanged = true;
194cdf0e10cSrcweir             }
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     if( bWait && m_pQueueInfo )
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
201cdf0e10cSrcweir         fprintf( stderr, "syncing printer discovery thread\n" );
202cdf0e10cSrcweir         #endif
203cdf0e10cSrcweir         m_pQueueInfo->join();
204cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
205cdf0e10cSrcweir         fprintf( stderr, "done: syncing printer discovery thread\n" );
206cdf0e10cSrcweir         #endif
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     if( ! bChanged && m_pQueueInfo )
210cdf0e10cSrcweir         bChanged = m_pQueueInfo->hasChanged();
211cdf0e10cSrcweir     if( bChanged )
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         initialize();
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     return bChanged;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir // -----------------------------------------------------------------
220cdf0e10cSrcweir 
initialize()221cdf0e10cSrcweir void PrinterInfoManager::initialize()
222cdf0e10cSrcweir {
223cdf0e10cSrcweir     m_bUseIncludeFeature = false;
224cdf0e10cSrcweir     rtl_TextEncoding aEncoding = gsl_getSystemTextEncoding();
225cdf0e10cSrcweir     m_aPrinters.clear();
226cdf0e10cSrcweir     m_aWatchFiles.clear();
227cdf0e10cSrcweir     OUString aDefaultPrinter;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     // first initialize the global defaults
230cdf0e10cSrcweir     // have to iterate over all possible files
231cdf0e10cSrcweir     // there should be only one global setup section in all
232cdf0e10cSrcweir     // available config files
233cdf0e10cSrcweir     m_aGlobalDefaults = PrinterInfo();
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     // need a parser for the PPDContext. generic printer should do.
236cdf0e10cSrcweir     m_aGlobalDefaults.m_pParser = PPDParser::getParser( String( RTL_CONSTASCII_USTRINGPARAM( "SGENPRT" ) ) );
237cdf0e10cSrcweir     m_aGlobalDefaults.m_aContext.setParser( m_aGlobalDefaults.m_pParser );
238cdf0e10cSrcweir     m_aGlobalDefaults.m_bPerformFontSubstitution = true;
239cdf0e10cSrcweir     m_bDisableCUPS = false;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     if( ! m_aGlobalDefaults.m_pParser )
242cdf0e10cSrcweir     {
243cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
244cdf0e10cSrcweir         fprintf( stderr, "Error: no default PPD file SGENPRT available, shutting down psprint...\n" );
245cdf0e10cSrcweir         #endif
246cdf0e10cSrcweir         return;
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir     std::list< OUString > aDirList;
250cdf0e10cSrcweir     psp::getPrinterPathList( aDirList, NULL );
251cdf0e10cSrcweir     std::list< OUString >::const_iterator print_dir_it;
252cdf0e10cSrcweir     for( print_dir_it = aDirList.begin(); print_dir_it != aDirList.end(); ++print_dir_it )
253cdf0e10cSrcweir     {
254cdf0e10cSrcweir         INetURLObject aFile( *print_dir_it, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
255cdf0e10cSrcweir         aFile.Append( String( RTL_CONSTASCII_USTRINGPARAM( PRINT_FILENAME ) ) );
256cdf0e10cSrcweir         Config aConfig( aFile.PathToFileName() );
257cdf0e10cSrcweir         if( aConfig.HasGroup( GLOBAL_DEFAULTS_GROUP ) )
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 1
260cdf0e10cSrcweir             fprintf( stderr, "found global defaults in %s\n", OUStringToOString( aFile.PathToFileName(), RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
261cdf0e10cSrcweir             #endif
262cdf0e10cSrcweir             aConfig.SetGroup( GLOBAL_DEFAULTS_GROUP );
263cdf0e10cSrcweir 
264cdf0e10cSrcweir             ByteString aValue( aConfig.ReadKey( "Copies" ) );
265cdf0e10cSrcweir             if( aValue.Len() )
266cdf0e10cSrcweir                 m_aGlobalDefaults.m_nCopies = aValue.ToInt32();
267cdf0e10cSrcweir 
268cdf0e10cSrcweir             aValue = aConfig.ReadKey( "Orientation" );
269cdf0e10cSrcweir             if( aValue.Len() )
270cdf0e10cSrcweir                 m_aGlobalDefaults.m_eOrientation = aValue.EqualsIgnoreCaseAscii( "Landscape" ) ? orientation::Landscape : orientation::Portrait;
271cdf0e10cSrcweir 
272cdf0e10cSrcweir             aValue = aConfig.ReadKey( "MarginAdjust" );
273cdf0e10cSrcweir             if( aValue.Len() )
274cdf0e10cSrcweir             {
275cdf0e10cSrcweir                 m_aGlobalDefaults.m_nLeftMarginAdjust   = aValue.GetToken( 0, ',' ).ToInt32();
276cdf0e10cSrcweir                 m_aGlobalDefaults.m_nRightMarginAdjust  = aValue.GetToken( 1, ',' ).ToInt32();
277cdf0e10cSrcweir                 m_aGlobalDefaults.m_nTopMarginAdjust    = aValue.GetToken( 2, ',' ).ToInt32();
278cdf0e10cSrcweir                 m_aGlobalDefaults.m_nBottomMarginAdjust = aValue.GetToken( 3, ',' ).ToInt32();
279cdf0e10cSrcweir             }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir             aValue = aConfig.ReadKey( "ColorDepth", "24" );
282cdf0e10cSrcweir             if( aValue.Len() )
283cdf0e10cSrcweir                 m_aGlobalDefaults.m_nColorDepth = aValue.ToInt32();
284cdf0e10cSrcweir 
285cdf0e10cSrcweir             aValue = aConfig.ReadKey( "ColorDevice" );
286cdf0e10cSrcweir             if( aValue.Len() )
287cdf0e10cSrcweir                 m_aGlobalDefaults.m_nColorDevice = aValue.ToInt32();
288cdf0e10cSrcweir 
289cdf0e10cSrcweir             aValue = aConfig.ReadKey( "PSLevel" );
290cdf0e10cSrcweir             if( aValue.Len() )
291cdf0e10cSrcweir                 m_aGlobalDefaults.m_nPSLevel = aValue.ToInt32();
292cdf0e10cSrcweir 
293cdf0e10cSrcweir             aValue = aConfig.ReadKey( "PDFDevice" );
294cdf0e10cSrcweir             if( aValue.Len() )
295cdf0e10cSrcweir                 m_aGlobalDefaults.m_nPDFDevice = aValue.ToInt32();
296cdf0e10cSrcweir 
297cdf0e10cSrcweir             aValue = aConfig.ReadKey( "PerformFontSubstitution" );
298cdf0e10cSrcweir             if( aValue.Len() )
299cdf0e10cSrcweir             {
300cdf0e10cSrcweir                 if( ! aValue.Equals( "0" ) && ! aValue.EqualsIgnoreCaseAscii( "false" ) )
301cdf0e10cSrcweir                     m_aGlobalDefaults.m_bPerformFontSubstitution = true;
302cdf0e10cSrcweir                 else
303cdf0e10cSrcweir                     m_aGlobalDefaults.m_bPerformFontSubstitution = false;
304cdf0e10cSrcweir             }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir             aValue = aConfig.ReadKey( "DisableCUPS" );
307cdf0e10cSrcweir             if( aValue.Len() )
308cdf0e10cSrcweir             {
309cdf0e10cSrcweir                 if( aValue.Equals( "1" ) || aValue.EqualsIgnoreCaseAscii( "true" ) )
310cdf0e10cSrcweir                     m_bDisableCUPS = true;
311cdf0e10cSrcweir                 else
312cdf0e10cSrcweir                     m_bDisableCUPS = false;
313cdf0e10cSrcweir             }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir             // get the PPDContext of global JobData
316cdf0e10cSrcweir             for( int nKey = 0; nKey < aConfig.GetKeyCount(); nKey++ )
317cdf0e10cSrcweir             {
318cdf0e10cSrcweir                 ByteString aKey( aConfig.GetKeyName( nKey ) );
319cdf0e10cSrcweir                 if( aKey.CompareTo( "PPD_", 4 ) == COMPARE_EQUAL )
320cdf0e10cSrcweir                 {
321cdf0e10cSrcweir                     aValue = aConfig.ReadKey( aKey );
322cdf0e10cSrcweir                     const PPDKey* pKey = m_aGlobalDefaults.m_pParser->getKey( String( aKey.Copy( 4 ), RTL_TEXTENCODING_ISO_8859_1 ) );
323cdf0e10cSrcweir                     if( pKey )
324cdf0e10cSrcweir                     {
325cdf0e10cSrcweir                         m_aGlobalDefaults.m_aContext.
326cdf0e10cSrcweir                         setValue( pKey,
327cdf0e10cSrcweir                         aValue.Equals( "*nil" ) ? NULL : pKey->getValue( String( aValue, RTL_TEXTENCODING_ISO_8859_1 ) ),
328cdf0e10cSrcweir                         sal_True );
329cdf0e10cSrcweir                     }
330cdf0e10cSrcweir                 }
331cdf0e10cSrcweir                 else if( aKey.Len() > 10 && aKey.CompareTo("SubstFont_", 10 ) == COMPARE_EQUAL )
332cdf0e10cSrcweir                 {
333cdf0e10cSrcweir                     aValue = aConfig.ReadKey( aKey );
334cdf0e10cSrcweir                     m_aGlobalDefaults.m_aFontSubstitutes[ OStringToOUString( aKey.Copy( 10 ), RTL_TEXTENCODING_ISO_8859_1 ) ] = OStringToOUString( aValue, RTL_TEXTENCODING_ISO_8859_1 );
335cdf0e10cSrcweir                 }
336cdf0e10cSrcweir             }
337cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 1
338cdf0e10cSrcweir             fprintf( stderr, "global settings: fontsubst = %s, %d substitutes\n", m_aGlobalDefaults.m_bPerformFontSubstitution ? "true" : "false", (int)m_aGlobalDefaults.m_aFontSubstitutes.size() );
339cdf0e10cSrcweir             #endif
340cdf0e10cSrcweir         }
341cdf0e10cSrcweir     }
342cdf0e10cSrcweir     setDefaultPaper( m_aGlobalDefaults.m_aContext );
343cdf0e10cSrcweir     fillFontSubstitutions( m_aGlobalDefaults );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir     // now collect all available printers
346cdf0e10cSrcweir     for( print_dir_it = aDirList.begin(); print_dir_it != aDirList.end(); ++print_dir_it )
347cdf0e10cSrcweir     {
348cdf0e10cSrcweir         INetURLObject aDir( *print_dir_it, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
349cdf0e10cSrcweir         INetURLObject aFile( aDir );
350cdf0e10cSrcweir         aFile.Append( String( RTL_CONSTASCII_USTRINGPARAM( PRINT_FILENAME ) ) );
351cdf0e10cSrcweir 
352cdf0e10cSrcweir         // check directory validity
353cdf0e10cSrcweir         OUString aUniPath;
354cdf0e10cSrcweir         FileBase::getFileURLFromSystemPath( aDir.PathToFileName(), aUniPath );
355cdf0e10cSrcweir         Directory aDirectory( aUniPath );
356cdf0e10cSrcweir         if( aDirectory.open() )
357cdf0e10cSrcweir             continue;
358cdf0e10cSrcweir         aDirectory.close();
359cdf0e10cSrcweir 
360cdf0e10cSrcweir 
361cdf0e10cSrcweir         FileBase::getFileURLFromSystemPath( aFile.PathToFileName(), aUniPath );
362cdf0e10cSrcweir         FileStatus aStatus( FileStatusMask_ModifyTime );
363cdf0e10cSrcweir         DirectoryItem aItem;
364cdf0e10cSrcweir 
365cdf0e10cSrcweir         // setup WatchFile list
366cdf0e10cSrcweir         WatchFile aWatchFile;
367cdf0e10cSrcweir         aWatchFile.m_aFilePath = aUniPath;
368cdf0e10cSrcweir         if( ! DirectoryItem::get( aUniPath, aItem ) &&
369cdf0e10cSrcweir             ! aItem.getFileStatus( aStatus ) )
370cdf0e10cSrcweir         {
371cdf0e10cSrcweir             aWatchFile.m_aModified = aStatus.getModifyTime();
372cdf0e10cSrcweir         }
373cdf0e10cSrcweir         else
374cdf0e10cSrcweir         {
375cdf0e10cSrcweir             aWatchFile.m_aModified.Seconds = 0;
376cdf0e10cSrcweir             aWatchFile.m_aModified.Nanosec = 0;
377cdf0e10cSrcweir         }
378cdf0e10cSrcweir         m_aWatchFiles.push_back( aWatchFile );
379cdf0e10cSrcweir 
380cdf0e10cSrcweir         Config aConfig( aFile.PathToFileName() );
381cdf0e10cSrcweir         for( int nGroup = 0; nGroup < aConfig.GetGroupCount(); nGroup++ )
382cdf0e10cSrcweir         {
383cdf0e10cSrcweir             aConfig.SetGroup( aConfig.GetGroupName( nGroup ) );
384cdf0e10cSrcweir             ByteString aValue = aConfig.ReadKey( "Printer" );
385cdf0e10cSrcweir             if( aValue.Len() )
386cdf0e10cSrcweir             {
387cdf0e10cSrcweir                 OUString aPrinterName;
388cdf0e10cSrcweir 
389cdf0e10cSrcweir                 int nNamePos = aValue.Search( '/' );
390cdf0e10cSrcweir                 // check for valid value of "Printer"
391cdf0e10cSrcweir                 if( nNamePos == STRING_NOTFOUND )
392cdf0e10cSrcweir                     continue;
393cdf0e10cSrcweir 
394cdf0e10cSrcweir                 Printer aPrinter;
395cdf0e10cSrcweir                 // initialize to global defaults
396cdf0e10cSrcweir                 aPrinter.m_aInfo = m_aGlobalDefaults;
397cdf0e10cSrcweir                 // global settings do not default the printer substitution
398cdf0e10cSrcweir                 // list ! the substitution list in there is only used for
399cdf0e10cSrcweir                 // newly created printers
400cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aFontSubstitutes.clear();
401cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aFontSubstitutions.clear();
402cdf0e10cSrcweir 
403cdf0e10cSrcweir                 aPrinterName = String( aValue.Copy( nNamePos+1 ), RTL_TEXTENCODING_UTF8 );
404cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aPrinterName     = aPrinterName;
405cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aDriverName      = String( aValue.Copy( 0, nNamePos ), RTL_TEXTENCODING_UTF8 );
406cdf0e10cSrcweir 
407cdf0e10cSrcweir                 // set parser, merge settings
408cdf0e10cSrcweir                 // don't do this for CUPS printers as this is done
409cdf0e10cSrcweir                 // by the CUPS system itself
410cdf0e10cSrcweir                 if( aPrinter.m_aInfo.m_aDriverName.compareToAscii( "CUPS:", 5 ) != 0 )
411cdf0e10cSrcweir                 {
412cdf0e10cSrcweir                     aPrinter.m_aInfo.m_pParser          = PPDParser::getParser( aPrinter.m_aInfo.m_aDriverName );
413cdf0e10cSrcweir                     aPrinter.m_aInfo.m_aContext.setParser( aPrinter.m_aInfo.m_pParser );
414cdf0e10cSrcweir                     // note: setParser also purges the context
415cdf0e10cSrcweir 
416cdf0e10cSrcweir                     // ignore this printer if its driver is not found
417cdf0e10cSrcweir                     if( ! aPrinter.m_aInfo.m_pParser )
418cdf0e10cSrcweir                         continue;
419cdf0e10cSrcweir 
420cdf0e10cSrcweir                     // merge the ppd context keys if the printer has the same keys and values
421cdf0e10cSrcweir                     // this is a bit tricky, since it involves mixing two PPDs
422cdf0e10cSrcweir                     // without constraints which might end up badly
423cdf0e10cSrcweir                     // this feature should be use with caution
424cdf0e10cSrcweir                     // it is mainly to select default paper sizes for new printers
425cdf0e10cSrcweir                     for( int nPPDValueModified = 0; nPPDValueModified < m_aGlobalDefaults.m_aContext.countValuesModified(); nPPDValueModified++ )
426cdf0e10cSrcweir                     {
427cdf0e10cSrcweir                         const PPDKey* pDefKey = m_aGlobalDefaults.m_aContext.getModifiedKey( nPPDValueModified );
428cdf0e10cSrcweir                         const PPDValue* pDefValue = m_aGlobalDefaults.m_aContext.getValue( pDefKey );
429cdf0e10cSrcweir                         const PPDKey* pPrinterKey = pDefKey ? aPrinter.m_aInfo.m_pParser->getKey( pDefKey->getKey() ) : NULL;
430cdf0e10cSrcweir                         if( pDefKey && pPrinterKey )
431cdf0e10cSrcweir                             // at least the options exist in both PPDs
432cdf0e10cSrcweir                         {
433cdf0e10cSrcweir                             if( pDefValue )
434cdf0e10cSrcweir                             {
435cdf0e10cSrcweir                                 const PPDValue* pPrinterValue = pPrinterKey->getValue( pDefValue->m_aOption );
436cdf0e10cSrcweir                                 if( pPrinterValue )
437cdf0e10cSrcweir                                     // the printer has a corresponding option for the key
438cdf0e10cSrcweir                                 aPrinter.m_aInfo.m_aContext.setValue( pPrinterKey, pPrinterValue );
439cdf0e10cSrcweir                             }
440cdf0e10cSrcweir                             else
441cdf0e10cSrcweir                                 aPrinter.m_aInfo.m_aContext.setValue( pPrinterKey, NULL );
442cdf0e10cSrcweir                         }
443cdf0e10cSrcweir                     }
444cdf0e10cSrcweir 
445cdf0e10cSrcweir                     aValue = aConfig.ReadKey( "Command" );
446cdf0e10cSrcweir                     // no printer without a command
447cdf0e10cSrcweir                     if( ! aValue.Len() )
448cdf0e10cSrcweir                     {
449cdf0e10cSrcweir                         /*  TODO:
450cdf0e10cSrcweir                         *  porters: please append your platform to the Solaris
451cdf0e10cSrcweir                         *  case if your platform has SystemV printing per default.
452cdf0e10cSrcweir                         */
453cdf0e10cSrcweir                         #if defined SOLARIS
454cdf0e10cSrcweir                         aValue = "lp";
455cdf0e10cSrcweir                         #else
456cdf0e10cSrcweir                         aValue = "lpr";
457cdf0e10cSrcweir                         #endif
458cdf0e10cSrcweir                     }
459cdf0e10cSrcweir                     aPrinter.m_aInfo.m_aCommand = String( aValue, RTL_TEXTENCODING_UTF8 );
460cdf0e10cSrcweir                 }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "QuickCommand" );
463cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aQuickCommand = String( aValue, RTL_TEXTENCODING_UTF8 );
464cdf0e10cSrcweir 
465cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "Features" );
466cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aFeatures = String( aValue, RTL_TEXTENCODING_UTF8 );
467cdf0e10cSrcweir 
468cdf0e10cSrcweir                 // override the settings in m_aGlobalDefaults if keys exist
469cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "DefaultPrinter" );
470cdf0e10cSrcweir                 if( ! aValue.Equals( "0" ) && ! aValue.EqualsIgnoreCaseAscii( "false" ) )
471cdf0e10cSrcweir                     aDefaultPrinter = aPrinterName;
472cdf0e10cSrcweir 
473cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "Location" );
474cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aLocation = String( aValue, RTL_TEXTENCODING_UTF8 );
475cdf0e10cSrcweir 
476cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "Comment" );
477cdf0e10cSrcweir                 aPrinter.m_aInfo.m_aComment = String( aValue, RTL_TEXTENCODING_UTF8 );
478cdf0e10cSrcweir 
479cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "Copies" );
480cdf0e10cSrcweir                 if( aValue.Len() )
481cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nCopies = aValue.ToInt32();
482cdf0e10cSrcweir 
483cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "Orientation" );
484cdf0e10cSrcweir                 if( aValue.Len() )
485cdf0e10cSrcweir                     aPrinter.m_aInfo.m_eOrientation = aValue.EqualsIgnoreCaseAscii( "Landscape" ) ? orientation::Landscape : orientation::Portrait;
486cdf0e10cSrcweir 
487cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "MarginAdjust" );
488cdf0e10cSrcweir                 if( aValue.Len() )
489cdf0e10cSrcweir                 {
490cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nLeftMarginAdjust    = aValue.GetToken( 0, ',' ).ToInt32();
491cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nRightMarginAdjust   = aValue.GetToken( 1, ',' ).ToInt32();
492cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nTopMarginAdjust     = aValue.GetToken( 2, ',' ).ToInt32();
493cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nBottomMarginAdjust  = aValue.GetToken( 3, ',' ).ToInt32();
494cdf0e10cSrcweir                 }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "ColorDepth" );
497cdf0e10cSrcweir                 if( aValue.Len() )
498cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nColorDepth = aValue.ToInt32();
499cdf0e10cSrcweir 
500cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "ColorDevice" );
501cdf0e10cSrcweir                 if( aValue.Len() )
502cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nColorDevice = aValue.ToInt32();
503cdf0e10cSrcweir 
504cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "PSLevel" );
505cdf0e10cSrcweir                 if( aValue.Len() )
506cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nPSLevel = aValue.ToInt32();
507cdf0e10cSrcweir 
508cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "PDFDevice" );
509cdf0e10cSrcweir                 if( aValue.Len() )
510cdf0e10cSrcweir                     aPrinter.m_aInfo.m_nPDFDevice = aValue.ToInt32();
511cdf0e10cSrcweir 
512cdf0e10cSrcweir                 aValue = aConfig.ReadKey( "PerformFontSubstitution" );
513cdf0e10cSrcweir                 if( ! aValue.Equals( "0" ) && ! aValue.EqualsIgnoreCaseAscii( "false" ) )
514cdf0e10cSrcweir                     aPrinter.m_aInfo.m_bPerformFontSubstitution = true;
515cdf0e10cSrcweir                 else
516cdf0e10cSrcweir                     aPrinter.m_aInfo.m_bPerformFontSubstitution = false;
517cdf0e10cSrcweir 
518cdf0e10cSrcweir                 // now iterate over all keys to extract multi key information:
519cdf0e10cSrcweir                 // 1. PPDContext information
520cdf0e10cSrcweir                 // 2. Font substitution table
521cdf0e10cSrcweir                 for( int nKey = 0; nKey < aConfig.GetKeyCount(); nKey++ )
522cdf0e10cSrcweir                 {
523cdf0e10cSrcweir                     ByteString aKey( aConfig.GetKeyName( nKey ) );
524cdf0e10cSrcweir                     if( aKey.CompareTo( "PPD_", 4 ) == COMPARE_EQUAL && aPrinter.m_aInfo.m_pParser )
525cdf0e10cSrcweir                     {
526cdf0e10cSrcweir                         aValue = aConfig.ReadKey( aKey );
527cdf0e10cSrcweir                         const PPDKey* pKey = aPrinter.m_aInfo.m_pParser->getKey( String( aKey.Copy( 4 ), RTL_TEXTENCODING_ISO_8859_1 ) );
528cdf0e10cSrcweir                         if( pKey )
529cdf0e10cSrcweir                         {
530cdf0e10cSrcweir                             aPrinter.m_aInfo.m_aContext.
531cdf0e10cSrcweir                             setValue( pKey,
532cdf0e10cSrcweir                             aValue.Equals( "*nil" ) ? NULL : pKey->getValue( String( aValue, RTL_TEXTENCODING_ISO_8859_1 ) ),
533cdf0e10cSrcweir                             sal_True );
534cdf0e10cSrcweir                         }
535cdf0e10cSrcweir                     }
536cdf0e10cSrcweir                     else if( aKey.Len() > 10 && aKey.CompareTo("SubstFont_", 10 ) == COMPARE_EQUAL )
537cdf0e10cSrcweir                     {
538cdf0e10cSrcweir                         aValue = aConfig.ReadKey( aKey );
539cdf0e10cSrcweir                         aPrinter.m_aInfo.m_aFontSubstitutes[ OStringToOUString( aKey.Copy( 10 ), RTL_TEXTENCODING_ISO_8859_1 ) ] = OStringToOUString( aValue, RTL_TEXTENCODING_ISO_8859_1 );
540cdf0e10cSrcweir                     }
541cdf0e10cSrcweir                 }
542cdf0e10cSrcweir 
543cdf0e10cSrcweir                 setDefaultPaper( aPrinter.m_aInfo.m_aContext );
544cdf0e10cSrcweir                 fillFontSubstitutions( aPrinter.m_aInfo );
545cdf0e10cSrcweir 
546cdf0e10cSrcweir                 // finally insert printer
547cdf0e10cSrcweir                 FileBase::getFileURLFromSystemPath( aFile.PathToFileName(), aPrinter.m_aFile );
548cdf0e10cSrcweir                 aPrinter.m_bModified    = false;
549cdf0e10cSrcweir                 aPrinter.m_aGroup       = aConfig.GetGroupName( nGroup );
550cdf0e10cSrcweir                 std::hash_map< OUString, Printer, OUStringHash >::const_iterator find_it =
551cdf0e10cSrcweir                 m_aPrinters.find( aPrinterName );
552cdf0e10cSrcweir                 if( find_it != m_aPrinters.end() )
553cdf0e10cSrcweir                 {
554cdf0e10cSrcweir                     aPrinter.m_aAlternateFiles = find_it->second.m_aAlternateFiles;
555cdf0e10cSrcweir                     aPrinter.m_aAlternateFiles.push_front( find_it->second.m_aFile );
556cdf0e10cSrcweir                 }
557cdf0e10cSrcweir                 m_aPrinters[ aPrinterName ] = aPrinter;
558cdf0e10cSrcweir             }
559cdf0e10cSrcweir         }
560cdf0e10cSrcweir     }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir     // set default printer
563cdf0e10cSrcweir     if( m_aPrinters.size() )
564cdf0e10cSrcweir     {
565cdf0e10cSrcweir         if( m_aPrinters.find( aDefaultPrinter ) == m_aPrinters.end() )
566cdf0e10cSrcweir             aDefaultPrinter = m_aPrinters.begin()->first;
567cdf0e10cSrcweir     }
568cdf0e10cSrcweir     else
569cdf0e10cSrcweir         aDefaultPrinter = OUString();
570cdf0e10cSrcweir     m_aDefaultPrinter = aDefaultPrinter;
571cdf0e10cSrcweir 
572cdf0e10cSrcweir     if( m_eType != Default )
573cdf0e10cSrcweir         return;
574cdf0e10cSrcweir 
575cdf0e10cSrcweir     // add a default printer for every available print queue
576cdf0e10cSrcweir     // merge paper and font substitution from default printer,
577cdf0e10cSrcweir     // all else from global defaults
578cdf0e10cSrcweir     PrinterInfo aMergeInfo( m_aGlobalDefaults );
579cdf0e10cSrcweir     aMergeInfo.m_aDriverName    = String( RTL_CONSTASCII_USTRINGPARAM( "SGENPRT" ) );
580cdf0e10cSrcweir     aMergeInfo.m_aFeatures      = String( RTL_CONSTASCII_USTRINGPARAM( "autoqueue" ) );
581cdf0e10cSrcweir 
582cdf0e10cSrcweir     if( m_aDefaultPrinter.getLength() )
583cdf0e10cSrcweir     {
584cdf0e10cSrcweir         PrinterInfo aDefaultInfo( getPrinterInfo( m_aDefaultPrinter ) );
585cdf0e10cSrcweir         aMergeInfo.m_bPerformFontSubstitution = aDefaultInfo.m_bPerformFontSubstitution;
586cdf0e10cSrcweir         fillFontSubstitutions( aMergeInfo );
587cdf0e10cSrcweir 
588cdf0e10cSrcweir         const PPDKey* pDefKey           = aDefaultInfo.m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
589cdf0e10cSrcweir         const PPDKey* pMergeKey         = aMergeInfo.m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
590cdf0e10cSrcweir         const PPDValue* pDefValue       = aDefaultInfo.m_aContext.getValue( pDefKey );
591cdf0e10cSrcweir         const PPDValue* pMergeValue     = pMergeKey ? pMergeKey->getValue( pDefValue->m_aOption ) : NULL;
592cdf0e10cSrcweir         if( pMergeKey && pMergeValue )
593cdf0e10cSrcweir             aMergeInfo.m_aContext.setValue( pMergeKey, pMergeValue );
594cdf0e10cSrcweir     }
595cdf0e10cSrcweir 
596cdf0e10cSrcweir     getSystemPrintQueues();
597cdf0e10cSrcweir     for( ::std::list< SystemPrintQueue >::iterator it = m_aSystemPrintQueues.begin(); it != m_aSystemPrintQueues.end(); ++it )
598cdf0e10cSrcweir     {
599cdf0e10cSrcweir         String aPrinterName( RTL_CONSTASCII_USTRINGPARAM( "<" ) );
600cdf0e10cSrcweir         aPrinterName += String( it->m_aQueue );
601cdf0e10cSrcweir         aPrinterName.Append( '>' );
602cdf0e10cSrcweir 
603cdf0e10cSrcweir         if( m_aPrinters.find( aPrinterName ) != m_aPrinters.end() )
604cdf0e10cSrcweir             // probably user made this one permanent in padmin
605cdf0e10cSrcweir         continue;
606cdf0e10cSrcweir 
607cdf0e10cSrcweir         String aCmd( m_aSystemPrintCommand );
608cdf0e10cSrcweir         aCmd.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "(PRINTER)" ) ), it->m_aQueue );
609cdf0e10cSrcweir 
610cdf0e10cSrcweir         Printer aPrinter;
611cdf0e10cSrcweir 
612cdf0e10cSrcweir         // initialize to merged defaults
613cdf0e10cSrcweir         aPrinter.m_aInfo = aMergeInfo;
614cdf0e10cSrcweir         aPrinter.m_aInfo.m_aPrinterName     = aPrinterName;
615cdf0e10cSrcweir         aPrinter.m_aInfo.m_aCommand         = aCmd;
616cdf0e10cSrcweir         aPrinter.m_aInfo.m_aComment         = it->m_aComment;
617cdf0e10cSrcweir         aPrinter.m_aInfo.m_aLocation        = it->m_aLocation;
618cdf0e10cSrcweir         aPrinter.m_bModified                = false;
619cdf0e10cSrcweir         aPrinter.m_aGroup                   = ByteString( aPrinterName, aEncoding ); //provide group name in case user makes this one permanent in padmin
620cdf0e10cSrcweir 
621cdf0e10cSrcweir         m_aPrinters[ aPrinterName ] = aPrinter;
622cdf0e10cSrcweir     }
623cdf0e10cSrcweir }
624cdf0e10cSrcweir 
625cdf0e10cSrcweir // -----------------------------------------------------------------
626cdf0e10cSrcweir 
listPrinters(::std::list<OUString> & rList) const627cdf0e10cSrcweir void PrinterInfoManager::listPrinters( ::std::list< OUString >& rList ) const
628cdf0e10cSrcweir {
629cdf0e10cSrcweir     ::std::hash_map< OUString, Printer, OUStringHash >::const_iterator it;
630cdf0e10cSrcweir     rList.clear();
631cdf0e10cSrcweir     for( it = m_aPrinters.begin(); it != m_aPrinters.end(); ++it )
632cdf0e10cSrcweir         rList.push_back( it->first );
633cdf0e10cSrcweir }
634cdf0e10cSrcweir 
635cdf0e10cSrcweir // -----------------------------------------------------------------
636cdf0e10cSrcweir 
getPrinterInfo(const OUString & rPrinter) const637cdf0e10cSrcweir const PrinterInfo& PrinterInfoManager::getPrinterInfo( const OUString& rPrinter ) const
638cdf0e10cSrcweir {
639cdf0e10cSrcweir     static PrinterInfo aEmptyInfo;
640cdf0e10cSrcweir     ::std::hash_map< OUString, Printer, OUStringHash >::const_iterator it = m_aPrinters.find( rPrinter );
641cdf0e10cSrcweir 
642cdf0e10cSrcweir     DBG_ASSERT( it != m_aPrinters.end(), "Do not ask for info about nonexistent printers" );
643cdf0e10cSrcweir 
644cdf0e10cSrcweir     return it != m_aPrinters.end() ? it->second.m_aInfo : aEmptyInfo;
645cdf0e10cSrcweir }
646cdf0e10cSrcweir 
647cdf0e10cSrcweir // -----------------------------------------------------------------
648cdf0e10cSrcweir 
changePrinterInfo(const OUString & rPrinter,const PrinterInfo & rNewInfo)649cdf0e10cSrcweir void PrinterInfoManager::changePrinterInfo( const OUString& rPrinter, const PrinterInfo& rNewInfo )
650cdf0e10cSrcweir {
651cdf0e10cSrcweir     ::std::hash_map< OUString, Printer, OUStringHash >::iterator it = m_aPrinters.find( rPrinter );
652cdf0e10cSrcweir 
653cdf0e10cSrcweir     DBG_ASSERT( it != m_aPrinters.end(), "Do not change nonexistant printers" );
654cdf0e10cSrcweir 
655cdf0e10cSrcweir     if( it != m_aPrinters.end() )
656cdf0e10cSrcweir     {
657cdf0e10cSrcweir         it->second.m_aInfo      = rNewInfo;
658cdf0e10cSrcweir         // recalculate font substitutions
659cdf0e10cSrcweir         fillFontSubstitutions( it->second.m_aInfo );
660cdf0e10cSrcweir         it->second.m_bModified  = true;
661cdf0e10cSrcweir         writePrinterConfig();
662cdf0e10cSrcweir     }
663cdf0e10cSrcweir }
664cdf0e10cSrcweir 
665cdf0e10cSrcweir // -----------------------------------------------------------------
666cdf0e10cSrcweir 
667cdf0e10cSrcweir // need to check writeability / creatability of config files
checkWriteability(const OUString & rUniPath)668cdf0e10cSrcweir static bool checkWriteability( const OUString& rUniPath )
669cdf0e10cSrcweir {
670cdf0e10cSrcweir     bool bRet = false;
671cdf0e10cSrcweir     OUString aSysPath;
672cdf0e10cSrcweir     FileBase::getSystemPathFromFileURL( rUniPath, aSysPath );
673cdf0e10cSrcweir     SvFileStream aStream( aSysPath, STREAM_READ | STREAM_WRITE );
674cdf0e10cSrcweir     if( aStream.IsOpen() && aStream.IsWritable() )
675cdf0e10cSrcweir         bRet = true;
676cdf0e10cSrcweir     return bRet;
677cdf0e10cSrcweir }
678cdf0e10cSrcweir 
writePrinterConfig()679cdf0e10cSrcweir bool PrinterInfoManager::writePrinterConfig()
680cdf0e10cSrcweir {
681cdf0e10cSrcweir     // find at least one writeable config
682cdf0e10cSrcweir     ::std::hash_map< OUString, Config*, OUStringHash > files;
683cdf0e10cSrcweir     ::std::hash_map< OUString, int, OUStringHash > rofiles;
684cdf0e10cSrcweir     ::std::hash_map< OUString, Config*, OUStringHash >::iterator file_it;
685cdf0e10cSrcweir 
686cdf0e10cSrcweir     for( ::std::list< WatchFile >::const_iterator wit = m_aWatchFiles.begin(); wit != m_aWatchFiles.end(); ++wit )
687cdf0e10cSrcweir     {
688cdf0e10cSrcweir         if( checkWriteability( wit->m_aFilePath ) )
689cdf0e10cSrcweir         {
690cdf0e10cSrcweir             files[ wit->m_aFilePath ] = new Config( wit->m_aFilePath );
691cdf0e10cSrcweir             break;
692cdf0e10cSrcweir         }
693cdf0e10cSrcweir     }
694cdf0e10cSrcweir 
695cdf0e10cSrcweir     if( files.empty() )
696cdf0e10cSrcweir         return false;
697cdf0e10cSrcweir 
698cdf0e10cSrcweir     Config* pGlobal = files.begin()->second;
699cdf0e10cSrcweir     pGlobal->SetGroup( GLOBAL_DEFAULTS_GROUP );
700cdf0e10cSrcweir     pGlobal->WriteKey( "DisableCUPS", m_bDisableCUPS ? "true" : "false" );
701cdf0e10cSrcweir 
702cdf0e10cSrcweir     ::std::hash_map< OUString, Printer, OUStringHash >::iterator it;
703cdf0e10cSrcweir     for( it = m_aPrinters.begin(); it != m_aPrinters.end(); ++it )
704cdf0e10cSrcweir     {
705cdf0e10cSrcweir         if( ! it->second.m_bModified )
706cdf0e10cSrcweir             // printer was not changed, do nothing
707cdf0e10cSrcweir         continue;
708cdf0e10cSrcweir 
709cdf0e10cSrcweir         // don't save autoqueue printers
710cdf0e10cSrcweir         sal_Int32 nIndex = 0;
711cdf0e10cSrcweir         bool bAutoQueue = false;
712cdf0e10cSrcweir         while( nIndex != -1 && ! bAutoQueue )
713cdf0e10cSrcweir         {
714cdf0e10cSrcweir             OUString aToken( it->second.m_aInfo.m_aFeatures.getToken( 0, ',', nIndex ) );
715cdf0e10cSrcweir             if( aToken.getLength() && aToken.compareToAscii( "autoqueue" ) == 0 )
716cdf0e10cSrcweir                 bAutoQueue = true;
717cdf0e10cSrcweir         }
718cdf0e10cSrcweir         if( bAutoQueue )
719cdf0e10cSrcweir             continue;
720cdf0e10cSrcweir 
721cdf0e10cSrcweir         if( it->second.m_aFile.getLength() )
722cdf0e10cSrcweir         {
723cdf0e10cSrcweir             // check if file is writable
724cdf0e10cSrcweir             if( files.find( it->second.m_aFile ) == files.end() )
725cdf0e10cSrcweir             {
726cdf0e10cSrcweir                 bool bInsertToNewFile = false;
727cdf0e10cSrcweir                 // maybe it is simply not inserted yet
728cdf0e10cSrcweir                 if( rofiles.find( it->second.m_aFile ) == rofiles.end() )
729cdf0e10cSrcweir                 {
730cdf0e10cSrcweir                     if( checkWriteability( it->second.m_aFile ) )
731cdf0e10cSrcweir                         files[ it->second.m_aFile ] = new Config( it->second.m_aFile );
732cdf0e10cSrcweir                     else
733cdf0e10cSrcweir                         bInsertToNewFile = true;
734cdf0e10cSrcweir                 }
735cdf0e10cSrcweir                 else
736cdf0e10cSrcweir                     bInsertToNewFile = true;
737cdf0e10cSrcweir                 // original file is read only, insert printer in a new writeable file
738cdf0e10cSrcweir                 if( bInsertToNewFile )
739cdf0e10cSrcweir                 {
740cdf0e10cSrcweir                     rofiles[ it->second.m_aFile ] = 1;
741cdf0e10cSrcweir                     // update alternate file list
742cdf0e10cSrcweir                     // the remove operation ensures uniqueness of each alternate
743cdf0e10cSrcweir                     it->second.m_aAlternateFiles.remove( it->second.m_aFile );
744cdf0e10cSrcweir                     it->second.m_aAlternateFiles.remove( files.begin()->first );
745cdf0e10cSrcweir                     it->second.m_aAlternateFiles.push_front( it->second.m_aFile );
746cdf0e10cSrcweir                     // update file
747cdf0e10cSrcweir                     it->second.m_aFile = files.begin()->first;
748cdf0e10cSrcweir                 }
749cdf0e10cSrcweir             }
750cdf0e10cSrcweir         }
751cdf0e10cSrcweir         else // a new printer, write it to the first file available
752cdf0e10cSrcweir             it->second.m_aFile = files.begin()->first;
753cdf0e10cSrcweir 
754cdf0e10cSrcweir         if( ! it->second.m_aGroup.getLength() ) // probably a new printer
755cdf0e10cSrcweir             it->second.m_aGroup = OString( it->first.getStr(), it->first.getLength(), RTL_TEXTENCODING_UTF8 );
756cdf0e10cSrcweir 
757cdf0e10cSrcweir         if( files.find( it->second.m_aFile ) != files.end() )
758cdf0e10cSrcweir         {
759cdf0e10cSrcweir             Config* pConfig = files[ it->second.m_aFile ];
760cdf0e10cSrcweir             pConfig->DeleteGroup( it->second.m_aGroup ); // else some old keys may remain
761cdf0e10cSrcweir             pConfig->SetGroup( it->second.m_aGroup );
762cdf0e10cSrcweir 
763cdf0e10cSrcweir             ByteString aValue( String( it->second.m_aInfo.m_aDriverName ), RTL_TEXTENCODING_UTF8 );
764cdf0e10cSrcweir             aValue += '/';
765cdf0e10cSrcweir             aValue += ByteString( String( it->first ), RTL_TEXTENCODING_UTF8 );
766cdf0e10cSrcweir             pConfig->WriteKey( "Printer", aValue );
767cdf0e10cSrcweir             pConfig->WriteKey( "DefaultPrinter", it->first == m_aDefaultPrinter ? "1" : "0" );
768cdf0e10cSrcweir             pConfig->WriteKey( "Location", ByteString( String( it->second.m_aInfo.m_aLocation ), RTL_TEXTENCODING_UTF8 ) );
769cdf0e10cSrcweir             pConfig->WriteKey( "Comment", ByteString( String( it->second.m_aInfo.m_aComment ), RTL_TEXTENCODING_UTF8 ) );
770cdf0e10cSrcweir             pConfig->WriteKey( "Command", ByteString( String( it->second.m_aInfo.m_aCommand ), RTL_TEXTENCODING_UTF8 ) );
771cdf0e10cSrcweir             pConfig->WriteKey( "QuickCommand", ByteString( String( it->second.m_aInfo.m_aQuickCommand ), RTL_TEXTENCODING_UTF8 ) );
772cdf0e10cSrcweir             pConfig->WriteKey( "Features", ByteString( String( it->second.m_aInfo.m_aFeatures ), RTL_TEXTENCODING_UTF8 ) );
773cdf0e10cSrcweir             pConfig->WriteKey( "Copies", ByteString::CreateFromInt32( it->second.m_aInfo.m_nCopies ) );
774cdf0e10cSrcweir             pConfig->WriteKey( "Orientation", it->second.m_aInfo.m_eOrientation == orientation::Landscape ? "Landscape" : "Portrait" );
775cdf0e10cSrcweir             pConfig->WriteKey( "PSLevel", ByteString::CreateFromInt32( it->second.m_aInfo.m_nPSLevel ) );
776cdf0e10cSrcweir             pConfig->WriteKey( "PDFDevice", ByteString::CreateFromInt32( it->second.m_aInfo.m_nPDFDevice ) );
777cdf0e10cSrcweir             pConfig->WriteKey( "ColorDevice", ByteString::CreateFromInt32( it->second.m_aInfo.m_nColorDevice ) );
778cdf0e10cSrcweir             pConfig->WriteKey( "ColorDepth", ByteString::CreateFromInt32( it->second.m_aInfo.m_nColorDepth ) );
779cdf0e10cSrcweir             aValue = ByteString::CreateFromInt32( it->second.m_aInfo.m_nLeftMarginAdjust );
780cdf0e10cSrcweir             aValue += ',';
781cdf0e10cSrcweir             aValue += ByteString::CreateFromInt32( it->second.m_aInfo.m_nRightMarginAdjust );
782cdf0e10cSrcweir             aValue += ',';
783cdf0e10cSrcweir             aValue += ByteString::CreateFromInt32( it->second.m_aInfo.m_nTopMarginAdjust );
784cdf0e10cSrcweir             aValue += ',';
785cdf0e10cSrcweir             aValue += ByteString::CreateFromInt32( it->second.m_aInfo.m_nBottomMarginAdjust );
786cdf0e10cSrcweir             pConfig->WriteKey( "MarginAdjust", aValue );
787cdf0e10cSrcweir 
788cdf0e10cSrcweir             if( it->second.m_aInfo.m_aDriverName.compareToAscii( "CUPS:", 5 ) != 0 )
789cdf0e10cSrcweir             {
790cdf0e10cSrcweir                 // write PPDContext (not for CUPS)
791cdf0e10cSrcweir                 for( int i = 0; i < it->second.m_aInfo.m_aContext.countValuesModified(); i++ )
792cdf0e10cSrcweir                 {
793cdf0e10cSrcweir                     const PPDKey* pKey = it->second.m_aInfo.m_aContext.getModifiedKey( i );
794cdf0e10cSrcweir                     ByteString aKey( "PPD_" );
795cdf0e10cSrcweir                     aKey += ByteString( pKey->getKey(), RTL_TEXTENCODING_ISO_8859_1 );
796cdf0e10cSrcweir 
797cdf0e10cSrcweir                     const PPDValue* pValue = it->second.m_aInfo.m_aContext.getValue( pKey );
798cdf0e10cSrcweir                     aValue = pValue ? ByteString( pValue->m_aOption, RTL_TEXTENCODING_ISO_8859_1 ) : ByteString( "*nil" );
799cdf0e10cSrcweir                     pConfig->WriteKey( aKey, aValue );
800cdf0e10cSrcweir                 }
801cdf0e10cSrcweir             }
802cdf0e10cSrcweir 
803cdf0e10cSrcweir             // write font substitution table
804cdf0e10cSrcweir             pConfig->WriteKey( "PerformFontSubstitution", it->second.m_aInfo.m_bPerformFontSubstitution ? "true" : "false" );
805cdf0e10cSrcweir             for( ::std::hash_map< OUString, OUString, OUStringHash >::const_iterator subst = it->second.m_aInfo.m_aFontSubstitutes.begin();
806cdf0e10cSrcweir             subst != it->second.m_aInfo.m_aFontSubstitutes.end(); ++subst )
807cdf0e10cSrcweir             {
808cdf0e10cSrcweir                 ByteString aKey( "SubstFont_" );
809cdf0e10cSrcweir                 aKey.Append( OUStringToOString( subst->first, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
810cdf0e10cSrcweir                 pConfig->WriteKey( aKey, OUStringToOString( subst->second, RTL_TEXTENCODING_ISO_8859_1 ) );
811cdf0e10cSrcweir             }
812cdf0e10cSrcweir         }
813cdf0e10cSrcweir     }
814cdf0e10cSrcweir 
815cdf0e10cSrcweir     // get rid of Config objects. this also writes any changes
816cdf0e10cSrcweir     for( file_it = files.begin(); file_it != files.end(); ++file_it )
817cdf0e10cSrcweir         delete file_it->second;
818cdf0e10cSrcweir 
819cdf0e10cSrcweir     return true;
820cdf0e10cSrcweir }
821cdf0e10cSrcweir 
822cdf0e10cSrcweir // -----------------------------------------------------------------
823cdf0e10cSrcweir 
addPrinter(const OUString & rPrinterName,const OUString & rDriverName)824cdf0e10cSrcweir bool PrinterInfoManager::addPrinter( const OUString& rPrinterName, const OUString& rDriverName )
825cdf0e10cSrcweir {
826cdf0e10cSrcweir     bool bSuccess = false;
827cdf0e10cSrcweir 
828cdf0e10cSrcweir     const PPDParser* pParser = NULL;
829cdf0e10cSrcweir     if( m_aPrinters.find( rPrinterName ) == m_aPrinters.end() && ( pParser = PPDParser::getParser( rDriverName ) ) )
830cdf0e10cSrcweir     {
831cdf0e10cSrcweir         Printer aPrinter;
832cdf0e10cSrcweir         aPrinter.m_bModified                        = true;
833cdf0e10cSrcweir         aPrinter.m_aInfo                            = m_aGlobalDefaults;
834cdf0e10cSrcweir         aPrinter.m_aInfo.m_aDriverName              = rDriverName;
835cdf0e10cSrcweir         aPrinter.m_aInfo.m_pParser                  = pParser;
836cdf0e10cSrcweir         aPrinter.m_aInfo.m_aContext.setParser( pParser );
837cdf0e10cSrcweir         aPrinter.m_aInfo.m_aPrinterName             = rPrinterName;
838cdf0e10cSrcweir 
839cdf0e10cSrcweir         fillFontSubstitutions( aPrinter.m_aInfo );
840cdf0e10cSrcweir         // merge PPD values with global defaults
841cdf0e10cSrcweir         for( int nPPDValueModified = 0; nPPDValueModified < m_aGlobalDefaults.m_aContext.countValuesModified(); nPPDValueModified++ )
842cdf0e10cSrcweir         {
843cdf0e10cSrcweir             const PPDKey* pDefKey = m_aGlobalDefaults.m_aContext.getModifiedKey( nPPDValueModified );
844cdf0e10cSrcweir             const PPDValue* pDefValue = m_aGlobalDefaults.m_aContext.getValue( pDefKey );
845cdf0e10cSrcweir             const PPDKey* pPrinterKey = pDefKey ? aPrinter.m_aInfo.m_pParser->getKey( pDefKey->getKey() ) : NULL;
846cdf0e10cSrcweir             if( pDefKey && pPrinterKey )
847cdf0e10cSrcweir                 // at least the options exist in both PPDs
848cdf0e10cSrcweir             {
849cdf0e10cSrcweir                 if( pDefValue )
850cdf0e10cSrcweir                 {
851cdf0e10cSrcweir                     const PPDValue* pPrinterValue = pPrinterKey->getValue( pDefValue->m_aOption );
852cdf0e10cSrcweir                     if( pPrinterValue )
853cdf0e10cSrcweir                         // the printer has a corresponding option for the key
854cdf0e10cSrcweir                     aPrinter.m_aInfo.m_aContext.setValue( pPrinterKey, pPrinterValue );
855cdf0e10cSrcweir                 }
856cdf0e10cSrcweir                 else
857cdf0e10cSrcweir                     aPrinter.m_aInfo.m_aContext.setValue( pPrinterKey, NULL );
858cdf0e10cSrcweir             }
859cdf0e10cSrcweir         }
860cdf0e10cSrcweir 
861cdf0e10cSrcweir         m_aPrinters[ rPrinterName ] = aPrinter;
862cdf0e10cSrcweir         bSuccess = true;
863cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
864cdf0e10cSrcweir         fprintf( stderr, "new printer %s, level = %d, pdfdevice = %d, colordevice = %d, depth = %d\n",
865cdf0e10cSrcweir         OUStringToOString( rPrinterName, osl_getThreadTextEncoding() ).getStr(),
866cdf0e10cSrcweir         m_aPrinters[rPrinterName].m_aInfo.m_nPSLevel,
867cdf0e10cSrcweir         m_aPrinters[rPrinterName].m_aInfo.m_nPDFDevice,
868cdf0e10cSrcweir         m_aPrinters[rPrinterName].m_aInfo.m_nColorDevice,
869cdf0e10cSrcweir         m_aPrinters[rPrinterName].m_aInfo.m_nColorDepth );
870cdf0e10cSrcweir         #endif
871cdf0e10cSrcweir         // comment: logically one should writePrinterConfig() here
872cdf0e10cSrcweir         // but immediately after addPrinter() a changePrinterInfo()
873cdf0e10cSrcweir         // will follow (see padmin code), which writes it again,
874cdf0e10cSrcweir         // so we can currently save some performance here
875cdf0e10cSrcweir     }
876cdf0e10cSrcweir     return bSuccess;
877cdf0e10cSrcweir }
878cdf0e10cSrcweir 
879cdf0e10cSrcweir // -----------------------------------------------------------------
880cdf0e10cSrcweir 
removePrinter(const OUString & rPrinterName,bool bCheckOnly)881cdf0e10cSrcweir bool PrinterInfoManager::removePrinter( const OUString& rPrinterName, bool bCheckOnly )
882cdf0e10cSrcweir {
883cdf0e10cSrcweir     bool bSuccess = true;
884cdf0e10cSrcweir 
885cdf0e10cSrcweir     ::std::hash_map< OUString, Printer, OUStringHash >::iterator it = m_aPrinters.find( rPrinterName );
886cdf0e10cSrcweir     if( it != m_aPrinters.end() )
887cdf0e10cSrcweir     {
888cdf0e10cSrcweir         if( it->second.m_aFile.getLength() )
889cdf0e10cSrcweir         {
890cdf0e10cSrcweir             // this printer already exists in a config file
891cdf0e10cSrcweir 
892cdf0e10cSrcweir 
893cdf0e10cSrcweir             // check writeability of config file(s)
894cdf0e10cSrcweir             if( ! checkWriteability( it->second.m_aFile ) )
895cdf0e10cSrcweir                 bSuccess = false;
896cdf0e10cSrcweir             else
897cdf0e10cSrcweir             {
898cdf0e10cSrcweir                 for( std::list< OUString >::const_iterator file_it = it->second.m_aAlternateFiles.begin();
899cdf0e10cSrcweir                 file_it != it->second.m_aAlternateFiles.end() && bSuccess; ++file_it )
900cdf0e10cSrcweir                 {
901cdf0e10cSrcweir                     if( ! checkWriteability( *file_it ) )
902cdf0e10cSrcweir                         bSuccess = false;
903cdf0e10cSrcweir                 }
904cdf0e10cSrcweir             }
905cdf0e10cSrcweir             if( bSuccess && ! bCheckOnly )
906cdf0e10cSrcweir             {
907cdf0e10cSrcweir 
908cdf0e10cSrcweir                 Config aConfig( it->second.m_aFile );
909cdf0e10cSrcweir                 aConfig.DeleteGroup( it->second.m_aGroup );
910cdf0e10cSrcweir                 aConfig.Flush();
911cdf0e10cSrcweir                 for( std::list< OUString >::const_iterator file_it = it->second.m_aAlternateFiles.begin();
912cdf0e10cSrcweir                 file_it != it->second.m_aAlternateFiles.end() && bSuccess; ++file_it )
913cdf0e10cSrcweir                 {
914cdf0e10cSrcweir                     Config aAltConfig( *file_it );
915cdf0e10cSrcweir                     aAltConfig.DeleteGroup( it->second.m_aGroup );
916cdf0e10cSrcweir                     aAltConfig.Flush();
917cdf0e10cSrcweir                 }
918cdf0e10cSrcweir             }
919cdf0e10cSrcweir         }
920cdf0e10cSrcweir         if( bSuccess && ! bCheckOnly )
921cdf0e10cSrcweir         {
922cdf0e10cSrcweir             m_aPrinters.erase( it );
923cdf0e10cSrcweir             // need this here because someone may call
924cdf0e10cSrcweir             // checkPrintersChanged after the removal
925cdf0e10cSrcweir             // but then other added printers were not flushed
926cdf0e10cSrcweir             // to disk, so they are discarded
927cdf0e10cSrcweir             writePrinterConfig();
928cdf0e10cSrcweir         }
929cdf0e10cSrcweir     }
930cdf0e10cSrcweir     return bSuccess;
931cdf0e10cSrcweir }
932cdf0e10cSrcweir 
933cdf0e10cSrcweir // -----------------------------------------------------------------
934cdf0e10cSrcweir 
setDefaultPrinter(const OUString & rPrinterName)935cdf0e10cSrcweir bool PrinterInfoManager::setDefaultPrinter( const OUString& rPrinterName )
936cdf0e10cSrcweir {
937cdf0e10cSrcweir     bool bSuccess = false;
938cdf0e10cSrcweir 
939cdf0e10cSrcweir     ::std::hash_map< OUString, Printer, OUStringHash >::iterator it = m_aPrinters.find( rPrinterName );
940cdf0e10cSrcweir     if( it != m_aPrinters.end() )
941cdf0e10cSrcweir     {
942cdf0e10cSrcweir         bSuccess = true;
943cdf0e10cSrcweir         it->second.m_bModified = true;
944cdf0e10cSrcweir         if( ( it = m_aPrinters.find( m_aDefaultPrinter ) ) != m_aPrinters.end() )
945cdf0e10cSrcweir             it->second.m_bModified = true;
946cdf0e10cSrcweir         m_aDefaultPrinter = rPrinterName;
947cdf0e10cSrcweir         writePrinterConfig();
948cdf0e10cSrcweir     }
949cdf0e10cSrcweir     return bSuccess;
950cdf0e10cSrcweir }
951cdf0e10cSrcweir 
952cdf0e10cSrcweir // -----------------------------------------------------------------
addOrRemovePossible() const953cdf0e10cSrcweir bool PrinterInfoManager::addOrRemovePossible() const
954cdf0e10cSrcweir {
955cdf0e10cSrcweir     return true;
956cdf0e10cSrcweir }
957cdf0e10cSrcweir 
958cdf0e10cSrcweir // -----------------------------------------------------------------
959cdf0e10cSrcweir 
fillFontSubstitutions(PrinterInfo & rInfo) const960cdf0e10cSrcweir void PrinterInfoManager::fillFontSubstitutions( PrinterInfo& rInfo ) const
961cdf0e10cSrcweir {
962cdf0e10cSrcweir     PrintFontManager& rFontManager( PrintFontManager::get() );
963cdf0e10cSrcweir     rInfo.m_aFontSubstitutions.clear();
964cdf0e10cSrcweir 
965cdf0e10cSrcweir     if( ! rInfo.m_bPerformFontSubstitution ||
966cdf0e10cSrcweir         ! rInfo.m_aFontSubstitutes.size() )
967cdf0e10cSrcweir     return;
968cdf0e10cSrcweir 
969cdf0e10cSrcweir     ::std::list< FastPrintFontInfo > aFonts;
970cdf0e10cSrcweir     ::std::hash_map< OUString, ::std::list< FastPrintFontInfo >, OUStringHash > aPrinterFonts;
971cdf0e10cSrcweir     rFontManager.getFontListWithFastInfo( aFonts, rInfo.m_pParser );
972cdf0e10cSrcweir 
973cdf0e10cSrcweir     // get builtin fonts
974cdf0e10cSrcweir     ::std::list< FastPrintFontInfo >::const_iterator it;
975cdf0e10cSrcweir     for( it = aFonts.begin(); it != aFonts.end(); ++it )
976cdf0e10cSrcweir         if( it->m_eType == fonttype::Builtin )
977cdf0e10cSrcweir             aPrinterFonts[ it->m_aFamilyName.toAsciiLowerCase() ].push_back( *it );
978cdf0e10cSrcweir 
979cdf0e10cSrcweir     // map lower case, so build a local copy of the font substitutions
980cdf0e10cSrcweir     ::std::hash_map< OUString, OUString, OUStringHash > aSubstitutions;
981cdf0e10cSrcweir     ::std::hash_map< OUString, OUString, OUStringHash >::const_iterator subst;
982cdf0e10cSrcweir     for( subst = rInfo.m_aFontSubstitutes.begin(); subst != rInfo.m_aFontSubstitutes.end(); ++subst )
983cdf0e10cSrcweir     {
984cdf0e10cSrcweir         OUString aFamily( subst->first.toAsciiLowerCase() );
985cdf0e10cSrcweir         // first look if there is a builtin of this family
986cdf0e10cSrcweir         // in this case override the substitution table
987cdf0e10cSrcweir         if( aPrinterFonts.find( aFamily ) != aPrinterFonts.end() )
988cdf0e10cSrcweir             aSubstitutions[ aFamily ] = aFamily;
989cdf0e10cSrcweir         else
990cdf0e10cSrcweir             aSubstitutions[ aFamily ] = subst->second.toAsciiLowerCase();
991cdf0e10cSrcweir     }
992cdf0e10cSrcweir 
993cdf0e10cSrcweir 
994cdf0e10cSrcweir     // now find substitutions
995cdf0e10cSrcweir     for( it = aFonts.begin(); it != aFonts.end(); ++it )
996cdf0e10cSrcweir     {
997cdf0e10cSrcweir         if( it->m_eType != fonttype::Builtin )
998cdf0e10cSrcweir         {
999cdf0e10cSrcweir             OUString aFamily( it->m_aFamilyName.toAsciiLowerCase() );
1000cdf0e10cSrcweir             subst = aSubstitutions.find( aFamily );
1001cdf0e10cSrcweir             if( subst != aSubstitutions.end() )
1002cdf0e10cSrcweir             {
1003cdf0e10cSrcweir                 // search a substitution
1004cdf0e10cSrcweir                 const ::std::list< FastPrintFontInfo >& rBuiltins( aPrinterFonts[ aSubstitutions[ aFamily ] ] );
1005cdf0e10cSrcweir                 ::std::list< FastPrintFontInfo >::const_iterator builtin;
1006cdf0e10cSrcweir                 int nLastMatch = -10000;
1007cdf0e10cSrcweir                 fontID nSubstitute = -1;
1008cdf0e10cSrcweir                 for( builtin = rBuiltins.begin(); builtin != rBuiltins.end(); ++builtin )
1009cdf0e10cSrcweir                 {
1010cdf0e10cSrcweir                     int nMatch = 0;
1011cdf0e10cSrcweir                     int nDiff;
1012cdf0e10cSrcweir                     if( builtin->m_eItalic == it->m_eItalic )
1013cdf0e10cSrcweir                         nMatch += 8000;
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir                     nDiff = builtin->m_eWeight - it->m_eWeight;
1016cdf0e10cSrcweir                     nDiff = nDiff < 0 ? -nDiff : nDiff;
1017cdf0e10cSrcweir                     nMatch += 4000 - 1000*nDiff;
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir                     nDiff = builtin->m_eWidth - it->m_eWidth;
1020cdf0e10cSrcweir                     nDiff = nDiff < 0 ? -nDiff : nDiff;
1021cdf0e10cSrcweir                     nMatch += 2000 - 500*nDiff;
1022cdf0e10cSrcweir 
1023cdf0e10cSrcweir                     if( nMatch > nLastMatch )
1024cdf0e10cSrcweir                     {
1025cdf0e10cSrcweir                         nLastMatch = nMatch;
1026cdf0e10cSrcweir                         nSubstitute = builtin->m_nID;
1027cdf0e10cSrcweir                     }
1028cdf0e10cSrcweir                 }
1029cdf0e10cSrcweir                 if( nSubstitute != -1 )
1030cdf0e10cSrcweir                 {
1031cdf0e10cSrcweir                     rInfo.m_aFontSubstitutions[ it->m_nID ] = nSubstitute;
1032cdf0e10cSrcweir                     #if OSL_DEBUG_LEVEL > 2
1033cdf0e10cSrcweir                     FastPrintFontInfo aInfo;
1034cdf0e10cSrcweir                     rFontManager.getFontFastInfo( nSubstitute, aInfo );
1035cdf0e10cSrcweir                     fprintf( stderr,
1036cdf0e10cSrcweir                     "substitute %s %s %d %d\n"
1037cdf0e10cSrcweir                     " ->        %s %s %d %d\n",
1038cdf0e10cSrcweir                     OUStringToOString( it->m_aFamilyName, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1039cdf0e10cSrcweir                     it->m_eItalic == italic::Upright ? "r" : it->m_eItalic == italic::Oblique ? "o" : it->m_eItalic == italic::Italic ? "i" : "u",
1040cdf0e10cSrcweir                     it->m_eWeight,
1041cdf0e10cSrcweir                     it->m_eWidth,
1042cdf0e10cSrcweir 
1043cdf0e10cSrcweir                     OUStringToOString( aInfo.m_aFamilyName, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
1044cdf0e10cSrcweir                     aInfo.m_eItalic == italic::Upright ? "r" : aInfo.m_eItalic == italic::Oblique ? "o" : aInfo.m_eItalic == italic::Italic ? "i" : "u",
1045cdf0e10cSrcweir                     aInfo.m_eWeight,
1046cdf0e10cSrcweir                     aInfo.m_eWidth
1047cdf0e10cSrcweir                     );
1048cdf0e10cSrcweir                     #endif
1049cdf0e10cSrcweir                 }
1050cdf0e10cSrcweir             }
1051cdf0e10cSrcweir         }
1052cdf0e10cSrcweir     }
1053cdf0e10cSrcweir }
1054cdf0e10cSrcweir 
1055cdf0e10cSrcweir // -----------------------------------------------------------------
1056cdf0e10cSrcweir 
getSystemPrintCommands(std::list<OUString> & rCommands)1057cdf0e10cSrcweir void PrinterInfoManager::getSystemPrintCommands( std::list< OUString >& rCommands )
1058cdf0e10cSrcweir {
1059cdf0e10cSrcweir     if( m_pQueueInfo && m_pQueueInfo->hasChanged() )
1060cdf0e10cSrcweir     {
1061cdf0e10cSrcweir         m_aSystemPrintCommand = m_pQueueInfo->getCommand();
1062cdf0e10cSrcweir         m_pQueueInfo->getSystemQueues( m_aSystemPrintQueues );
1063cdf0e10cSrcweir         delete m_pQueueInfo, m_pQueueInfo = NULL;
1064cdf0e10cSrcweir     }
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir     std::list< SystemPrintQueue >::const_iterator it;
1067cdf0e10cSrcweir     rCommands.clear();
1068cdf0e10cSrcweir     String aPrinterConst( RTL_CONSTASCII_USTRINGPARAM( "(PRINTER)" ) );
1069cdf0e10cSrcweir     for( it = m_aSystemPrintQueues.begin(); it != m_aSystemPrintQueues.end(); ++it )
1070cdf0e10cSrcweir     {
1071cdf0e10cSrcweir         String aCmd( m_aSystemPrintCommand );
1072cdf0e10cSrcweir         aCmd.SearchAndReplace( aPrinterConst, it->m_aQueue );
1073cdf0e10cSrcweir         rCommands.push_back( aCmd );
1074cdf0e10cSrcweir     }
1075cdf0e10cSrcweir }
1076cdf0e10cSrcweir 
getSystemPrintQueues()1077cdf0e10cSrcweir const std::list< PrinterInfoManager::SystemPrintQueue >& PrinterInfoManager::getSystemPrintQueues()
1078cdf0e10cSrcweir {
1079cdf0e10cSrcweir     if( m_pQueueInfo && m_pQueueInfo->hasChanged() )
1080cdf0e10cSrcweir     {
1081cdf0e10cSrcweir         m_aSystemPrintCommand = m_pQueueInfo->getCommand();
1082cdf0e10cSrcweir         m_pQueueInfo->getSystemQueues( m_aSystemPrintQueues );
1083cdf0e10cSrcweir         delete m_pQueueInfo, m_pQueueInfo = NULL;
1084cdf0e10cSrcweir     }
1085cdf0e10cSrcweir 
1086cdf0e10cSrcweir     return m_aSystemPrintQueues;
1087cdf0e10cSrcweir }
1088cdf0e10cSrcweir 
checkFeatureToken(const rtl::OUString & rPrinterName,const char * pToken) const1089cdf0e10cSrcweir bool PrinterInfoManager::checkFeatureToken( const rtl::OUString& rPrinterName, const char* pToken ) const
1090cdf0e10cSrcweir {
1091cdf0e10cSrcweir     const PrinterInfo& rPrinterInfo( getPrinterInfo( rPrinterName ) );
1092cdf0e10cSrcweir     sal_Int32 nIndex = 0;
1093cdf0e10cSrcweir     while( nIndex != -1 )
1094cdf0e10cSrcweir     {
1095cdf0e10cSrcweir         OUString aOuterToken = rPrinterInfo.m_aFeatures.getToken( 0, ',', nIndex );
1096cdf0e10cSrcweir         sal_Int32 nInnerIndex = 0;
1097cdf0e10cSrcweir         OUString aInnerToken = aOuterToken.getToken( 0, '=', nInnerIndex );
1098cdf0e10cSrcweir         if( aInnerToken.equalsIgnoreAsciiCaseAscii( pToken ) )
1099cdf0e10cSrcweir             return true;
1100cdf0e10cSrcweir     }
1101cdf0e10cSrcweir     return false;
1102cdf0e10cSrcweir }
1103cdf0e10cSrcweir 
startSpool(const OUString & rPrintername,bool bQuickCommand)1104cdf0e10cSrcweir FILE* PrinterInfoManager::startSpool( const OUString& rPrintername, bool bQuickCommand )
1105cdf0e10cSrcweir {
1106cdf0e10cSrcweir     const PrinterInfo&   rPrinterInfo   = getPrinterInfo (rPrintername);
1107cdf0e10cSrcweir     const rtl::OUString& rCommand       = (bQuickCommand && rPrinterInfo.m_aQuickCommand.getLength() ) ?
1108cdf0e10cSrcweir                                           rPrinterInfo.m_aQuickCommand : rPrinterInfo.m_aCommand;
1109cdf0e10cSrcweir     rtl::OString aShellCommand  = OUStringToOString (rCommand, RTL_TEXTENCODING_ISO_8859_1);
1110cdf0e10cSrcweir     aShellCommand += rtl::OString( " 2>/dev/null" );
1111cdf0e10cSrcweir 
1112cdf0e10cSrcweir     return popen (aShellCommand.getStr(), "w");
1113cdf0e10cSrcweir }
1114cdf0e10cSrcweir 
endSpool(const OUString &,const OUString &,FILE * pFile,const JobData &,bool)1115cdf0e10cSrcweir int PrinterInfoManager::endSpool( const OUString& /*rPrintername*/, const OUString& /*rJobTitle*/, FILE* pFile, const JobData& /*rDocumentJobData*/, bool /*bBanner*/ )
1116cdf0e10cSrcweir {
1117cdf0e10cSrcweir     return (0 == pclose( pFile ));
1118cdf0e10cSrcweir }
1119cdf0e10cSrcweir 
setupJobContextData(JobData & rData)1120cdf0e10cSrcweir void PrinterInfoManager::setupJobContextData( JobData& rData )
1121cdf0e10cSrcweir {
1122cdf0e10cSrcweir     std::hash_map< OUString, Printer, OUStringHash >::iterator it =
1123cdf0e10cSrcweir     m_aPrinters.find( rData.m_aPrinterName );
1124cdf0e10cSrcweir     if( it != m_aPrinters.end() )
1125cdf0e10cSrcweir     {
1126cdf0e10cSrcweir         rData.m_pParser     = it->second.m_aInfo.m_pParser;
1127cdf0e10cSrcweir         rData.m_aContext    = it->second.m_aInfo.m_aContext;
1128cdf0e10cSrcweir     }
1129cdf0e10cSrcweir }
1130cdf0e10cSrcweir 
setDefaultPaper(PPDContext & rContext) const1131cdf0e10cSrcweir void PrinterInfoManager::setDefaultPaper( PPDContext& rContext ) const
1132cdf0e10cSrcweir {
1133cdf0e10cSrcweir     if(  ! rContext.getParser() )
1134cdf0e10cSrcweir         return;
1135cdf0e10cSrcweir 
1136cdf0e10cSrcweir     const PPDKey* pPageSizeKey = rContext.getParser()->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
1137cdf0e10cSrcweir     if( ! pPageSizeKey )
1138cdf0e10cSrcweir         return;
1139cdf0e10cSrcweir 
1140cdf0e10cSrcweir     int nModified = rContext.countValuesModified();
1141cdf0e10cSrcweir     while( nModified-- &&
1142cdf0e10cSrcweir         rContext.getModifiedKey( nModified ) != pPageSizeKey )
1143cdf0e10cSrcweir     ;
1144cdf0e10cSrcweir 
1145cdf0e10cSrcweir     if( nModified >= 0 ) // paper was set already, do not modify
1146cdf0e10cSrcweir     {
1147cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
1148cdf0e10cSrcweir         fprintf( stderr, "not setting default paper, already set %s\n",
1149cdf0e10cSrcweir         OUStringToOString( rContext.getValue( pPageSizeKey )->m_aOption, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1150cdf0e10cSrcweir         #endif
1151cdf0e10cSrcweir         return;
1152cdf0e10cSrcweir     }
1153cdf0e10cSrcweir 
1154cdf0e10cSrcweir     // paper not set, fill in default value
1155cdf0e10cSrcweir     const PPDValue* pPaperVal = NULL;
1156cdf0e10cSrcweir     int nValues = pPageSizeKey->countValues();
1157cdf0e10cSrcweir     for( int i = 0; i < nValues && ! pPaperVal; i++ )
1158cdf0e10cSrcweir     {
1159cdf0e10cSrcweir         const PPDValue* pVal = pPageSizeKey->getValue( i );
1160cdf0e10cSrcweir         if( pVal->m_aOption.EqualsIgnoreCaseAscii( m_aSystemDefaultPaper.getStr() ) )
1161cdf0e10cSrcweir             pPaperVal = pVal;
1162cdf0e10cSrcweir     }
1163cdf0e10cSrcweir     if( pPaperVal )
1164cdf0e10cSrcweir     {
1165cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
1166cdf0e10cSrcweir         fprintf( stderr, "setting default paper %s\n", OUStringToOString( pPaperVal->m_aOption, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1167cdf0e10cSrcweir         #endif
1168cdf0e10cSrcweir         rContext.setValue( pPageSizeKey, pPaperVal );
1169cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
1170cdf0e10cSrcweir         pPaperVal = rContext.getValue( pPageSizeKey );
1171cdf0e10cSrcweir         fprintf( stderr, "-> got paper %s\n", OUStringToOString( pPaperVal->m_aOption, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
1172cdf0e10cSrcweir         #endif
1173cdf0e10cSrcweir     }
1174cdf0e10cSrcweir }
1175cdf0e10cSrcweir 
1176cdf0e10cSrcweir // -----------------------------------------------------------------
1177cdf0e10cSrcweir 
SystemQueueInfo()1178cdf0e10cSrcweir SystemQueueInfo::SystemQueueInfo() :
1179cdf0e10cSrcweir     m_bChanged( false )
1180cdf0e10cSrcweir {
1181cdf0e10cSrcweir     create();
1182cdf0e10cSrcweir }
1183cdf0e10cSrcweir 
~SystemQueueInfo()1184cdf0e10cSrcweir SystemQueueInfo::~SystemQueueInfo()
1185cdf0e10cSrcweir {
1186cdf0e10cSrcweir     static const char* pNoSyncDetection = getenv( "SAL_DISABLE_SYNCHRONOUS_PRINTER_DETECTION" );
1187cdf0e10cSrcweir     if( ! pNoSyncDetection || !*pNoSyncDetection )
1188cdf0e10cSrcweir         join();
1189cdf0e10cSrcweir     else
1190cdf0e10cSrcweir         terminate();
1191cdf0e10cSrcweir }
1192cdf0e10cSrcweir 
hasChanged() const1193cdf0e10cSrcweir bool SystemQueueInfo::hasChanged() const
1194cdf0e10cSrcweir {
1195cdf0e10cSrcweir     MutexGuard aGuard( m_aMutex );
1196cdf0e10cSrcweir     bool bChanged = m_bChanged;
1197cdf0e10cSrcweir     return bChanged;
1198cdf0e10cSrcweir }
1199cdf0e10cSrcweir 
getSystemQueues(std::list<PrinterInfoManager::SystemPrintQueue> & rQueues)1200cdf0e10cSrcweir void SystemQueueInfo::getSystemQueues( std::list< PrinterInfoManager::SystemPrintQueue >& rQueues )
1201cdf0e10cSrcweir {
1202cdf0e10cSrcweir     MutexGuard aGuard( m_aMutex );
1203cdf0e10cSrcweir     rQueues = m_aQueues;
1204cdf0e10cSrcweir     m_bChanged = false;
1205cdf0e10cSrcweir }
1206cdf0e10cSrcweir 
getCommand() const1207cdf0e10cSrcweir OUString SystemQueueInfo::getCommand() const
1208cdf0e10cSrcweir {
1209cdf0e10cSrcweir     MutexGuard aGuard( m_aMutex );
1210cdf0e10cSrcweir     OUString aRet = m_aCommand;
1211cdf0e10cSrcweir     return aRet;
1212cdf0e10cSrcweir }
1213cdf0e10cSrcweir 
1214cdf0e10cSrcweir struct SystemCommandParameters;
1215cdf0e10cSrcweir typedef void(* tokenHandler)(const std::list< rtl::OString >&,
1216cdf0e10cSrcweir                 std::list< PrinterInfoManager::SystemPrintQueue >&,
1217cdf0e10cSrcweir                 const SystemCommandParameters*);
1218cdf0e10cSrcweir 
1219cdf0e10cSrcweir struct SystemCommandParameters
1220cdf0e10cSrcweir {
1221cdf0e10cSrcweir     const char*     pQueueCommand;
1222cdf0e10cSrcweir     const char*     pPrintCommand;
1223cdf0e10cSrcweir     const char*     pForeToken;
1224cdf0e10cSrcweir     const char*     pAftToken;
1225cdf0e10cSrcweir     unsigned int    nForeTokenCount;
1226cdf0e10cSrcweir     tokenHandler    pHandler;
1227cdf0e10cSrcweir };
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir #if ! (defined(LINUX) || defined(NETBSD) || defined(FREEBSD))
lpgetSysQueueTokenHandler(const std::list<rtl::OString> & i_rLines,std::list<PrinterInfoManager::SystemPrintQueue> & o_rQueues,const SystemCommandParameters *)1230cdf0e10cSrcweir static void lpgetSysQueueTokenHandler(
1231cdf0e10cSrcweir     const std::list< rtl::OString >& i_rLines,
1232cdf0e10cSrcweir     std::list< PrinterInfoManager::SystemPrintQueue >& o_rQueues,
1233cdf0e10cSrcweir     const SystemCommandParameters* )
1234cdf0e10cSrcweir {
1235cdf0e10cSrcweir     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
1236cdf0e10cSrcweir     std::hash_set< OUString, OUStringHash > aUniqueSet;
1237cdf0e10cSrcweir     std::hash_set< OUString, OUStringHash > aOnlySet;
1238cdf0e10cSrcweir     aUniqueSet.insert( OUString( RTL_CONSTASCII_USTRINGPARAM( "_all" ) ) );
1239cdf0e10cSrcweir     aUniqueSet.insert( OUString( RTL_CONSTASCII_USTRINGPARAM( "_default" ) ) );
1240cdf0e10cSrcweir 
1241cdf0e10cSrcweir     // the eventual "all" attribute of the "_all" queue tells us, which
1242cdf0e10cSrcweir     // printers are to be used for this user at all
1243cdf0e10cSrcweir 
1244cdf0e10cSrcweir     // find _all: line
1245cdf0e10cSrcweir     rtl::OString aAllLine( "_all:" );
1246cdf0e10cSrcweir     rtl::OString aAllAttr( "all=" );
1247cdf0e10cSrcweir     for( std::list< rtl::OString >::const_iterator it = i_rLines.begin();
1248cdf0e10cSrcweir          it != i_rLines.end(); ++it )
1249cdf0e10cSrcweir     {
1250cdf0e10cSrcweir         if( it->indexOf( aAllLine, 0 ) == 0 )
1251cdf0e10cSrcweir         {
1252cdf0e10cSrcweir             // now find the "all" attribute
1253cdf0e10cSrcweir             ++it;
1254cdf0e10cSrcweir             while( it != i_rLines.end() )
1255cdf0e10cSrcweir             {
1256cdf0e10cSrcweir                 rtl::OString aClean( WhitespaceToSpace( *it ) );
1257cdf0e10cSrcweir                 if( aClean.indexOf( aAllAttr, 0 ) == 0 )
1258cdf0e10cSrcweir                 {
1259cdf0e10cSrcweir                     // insert the comma separated entries into the set of printers to use
1260cdf0e10cSrcweir                     sal_Int32 nPos = aAllAttr.getLength();
1261cdf0e10cSrcweir                     while( nPos != -1 )
1262cdf0e10cSrcweir                     {
1263cdf0e10cSrcweir                         OString aTok( aClean.getToken( 0, ',', nPos ) );
1264cdf0e10cSrcweir                         if( aTok.getLength() > 0 )
1265cdf0e10cSrcweir                             aOnlySet.insert( rtl::OStringToOUString( aTok, aEncoding ) );
1266cdf0e10cSrcweir                     }
1267cdf0e10cSrcweir                     break;
1268cdf0e10cSrcweir                 }
1269cdf0e10cSrcweir             }
1270cdf0e10cSrcweir             break;
1271cdf0e10cSrcweir         }
1272cdf0e10cSrcweir     }
1273cdf0e10cSrcweir 
1274cdf0e10cSrcweir     bool bInsertAttribute = false;
1275cdf0e10cSrcweir     rtl::OString aDescrStr( "description=" );
1276cdf0e10cSrcweir     rtl::OString aLocStr( "location=" );
1277cdf0e10cSrcweir     for( std::list< rtl::OString >::const_iterator it = i_rLines.begin();
1278cdf0e10cSrcweir          it != i_rLines.end(); ++it )
1279cdf0e10cSrcweir     {
1280cdf0e10cSrcweir         sal_Int32 nPos = 0;
1281cdf0e10cSrcweir         // find the begin of a new printer section
1282cdf0e10cSrcweir         nPos = it->indexOf( ':', 0 );
1283cdf0e10cSrcweir         if( nPos != -1 )
1284cdf0e10cSrcweir         {
1285cdf0e10cSrcweir             OUString aSysQueue( rtl::OStringToOUString( it->copy( 0, nPos ), aEncoding ) );
1286cdf0e10cSrcweir             // do not insert duplicates (e.g. lpstat tends to produce such lines)
1287cdf0e10cSrcweir             // in case there was a "_all" section, insert only those printer explicitly
1288cdf0e10cSrcweir             // set in the "all" attribute
1289cdf0e10cSrcweir             if( aUniqueSet.find( aSysQueue ) == aUniqueSet.end() &&
1290cdf0e10cSrcweir                 ( aOnlySet.empty() || aOnlySet.find( aSysQueue ) != aOnlySet.end() )
1291cdf0e10cSrcweir                 )
1292cdf0e10cSrcweir             {
1293cdf0e10cSrcweir                 o_rQueues.push_back( PrinterInfoManager::SystemPrintQueue() );
1294cdf0e10cSrcweir                 o_rQueues.back().m_aQueue = aSysQueue;
1295cdf0e10cSrcweir                 o_rQueues.back().m_aLocation = aSysQueue;
1296cdf0e10cSrcweir                 aUniqueSet.insert( aSysQueue );
1297cdf0e10cSrcweir                 bInsertAttribute = true;
1298cdf0e10cSrcweir             }
1299cdf0e10cSrcweir             else
1300cdf0e10cSrcweir                 bInsertAttribute = false;
1301cdf0e10cSrcweir             continue;
1302cdf0e10cSrcweir         }
1303cdf0e10cSrcweir         if( bInsertAttribute && ! o_rQueues.empty() )
1304cdf0e10cSrcweir         {
1305cdf0e10cSrcweir             // look for "description" attribute, insert as comment
1306cdf0e10cSrcweir             nPos = it->indexOf( aDescrStr, 0 );
1307cdf0e10cSrcweir             if( nPos != -1 )
1308cdf0e10cSrcweir             {
1309cdf0e10cSrcweir                 ByteString aComment( WhitespaceToSpace( it->copy(nPos+12) ) );
1310cdf0e10cSrcweir                 if( aComment.Len() > 0 )
1311cdf0e10cSrcweir                     o_rQueues.back().m_aComment = String( aComment, aEncoding );
1312cdf0e10cSrcweir                 continue;
1313cdf0e10cSrcweir             }
1314cdf0e10cSrcweir             // look for "location" attribute, inser as location
1315cdf0e10cSrcweir             nPos = it->indexOf( aLocStr, 0 );
1316cdf0e10cSrcweir             if( nPos != -1 )
1317cdf0e10cSrcweir             {
1318cdf0e10cSrcweir                 ByteString aLoc( WhitespaceToSpace( it->copy(nPos+9) ) );
1319cdf0e10cSrcweir                 if( aLoc.Len() > 0 )
1320cdf0e10cSrcweir                     o_rQueues.back().m_aLocation = String( aLoc, aEncoding );
1321cdf0e10cSrcweir                 continue;
1322cdf0e10cSrcweir             }
1323cdf0e10cSrcweir         }
1324cdf0e10cSrcweir     }
1325cdf0e10cSrcweir }
1326cdf0e10cSrcweir #endif
standardSysQueueTokenHandler(const std::list<rtl::OString> & i_rLines,std::list<PrinterInfoManager::SystemPrintQueue> & o_rQueues,const SystemCommandParameters * i_pParms)1327cdf0e10cSrcweir static void standardSysQueueTokenHandler(
1328cdf0e10cSrcweir     const std::list< rtl::OString >& i_rLines,
1329cdf0e10cSrcweir     std::list< PrinterInfoManager::SystemPrintQueue >& o_rQueues,
1330cdf0e10cSrcweir     const SystemCommandParameters* i_pParms)
1331cdf0e10cSrcweir {
1332cdf0e10cSrcweir     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
1333cdf0e10cSrcweir     std::hash_set< OUString, OUStringHash > aUniqueSet;
1334cdf0e10cSrcweir     rtl::OString aForeToken( i_pParms->pForeToken );
1335cdf0e10cSrcweir     rtl::OString aAftToken( i_pParms->pAftToken );
1336cdf0e10cSrcweir     /* Normal Unix print queue discovery, also used for Darwin 5 LPR printing
1337cdf0e10cSrcweir     */
1338cdf0e10cSrcweir     for( std::list< rtl::OString >::const_iterator it = i_rLines.begin();
1339cdf0e10cSrcweir          it != i_rLines.end(); ++it )
1340cdf0e10cSrcweir     {
1341cdf0e10cSrcweir         sal_Int32 nPos = 0;
1342cdf0e10cSrcweir 
1343cdf0e10cSrcweir         // search for a line describing a printer:
1344cdf0e10cSrcweir         // find if there are enough tokens before the name
1345cdf0e10cSrcweir         for( unsigned int i = 0; i < i_pParms->nForeTokenCount && nPos != -1; i++ )
1346cdf0e10cSrcweir         {
1347cdf0e10cSrcweir             nPos = it->indexOf( aForeToken, nPos );
1348cdf0e10cSrcweir             if( nPos != -1 && it->getLength() >= nPos+aForeToken.getLength() )
1349cdf0e10cSrcweir                 nPos += aForeToken.getLength();
1350cdf0e10cSrcweir         }
1351cdf0e10cSrcweir         if( nPos != -1 )
1352cdf0e10cSrcweir         {
1353cdf0e10cSrcweir             // find if there is the token after the queue
1354cdf0e10cSrcweir             sal_Int32 nAftPos = it->indexOf( aAftToken, nPos );
1355cdf0e10cSrcweir             if( nAftPos != -1 )
1356cdf0e10cSrcweir             {
1357cdf0e10cSrcweir                 // get the queue name between fore and aft tokens
1358cdf0e10cSrcweir                 OUString aSysQueue( rtl::OStringToOUString( it->copy( nPos, nAftPos - nPos ), aEncoding ) );
1359cdf0e10cSrcweir                 // do not insert duplicates (e.g. lpstat tends to produce such lines)
1360cdf0e10cSrcweir                 if( aUniqueSet.find( aSysQueue ) == aUniqueSet.end() )
1361cdf0e10cSrcweir                 {
1362cdf0e10cSrcweir                     o_rQueues.push_back( PrinterInfoManager::SystemPrintQueue() );
1363cdf0e10cSrcweir                     o_rQueues.back().m_aQueue = aSysQueue;
1364cdf0e10cSrcweir                     o_rQueues.back().m_aLocation = aSysQueue;
1365cdf0e10cSrcweir                     aUniqueSet.insert( aSysQueue );
1366cdf0e10cSrcweir                 }
1367cdf0e10cSrcweir             }
1368cdf0e10cSrcweir         }
1369cdf0e10cSrcweir     }
1370cdf0e10cSrcweir }
1371cdf0e10cSrcweir 
1372cdf0e10cSrcweir static const struct SystemCommandParameters aParms[] =
1373cdf0e10cSrcweir {
1374cdf0e10cSrcweir     #if defined(LINUX) || defined(NETBSD) || defined(FREEBSD)
1375cdf0e10cSrcweir     { "/usr/sbin/lpc status", "lpr -P \"(PRINTER)\"", "", ":", 0, standardSysQueueTokenHandler },
1376cdf0e10cSrcweir     { "lpc status", "lpr -P \"(PRINTER)\"", "", ":", 0, standardSysQueueTokenHandler },
1377cdf0e10cSrcweir     { "LANG=C;LC_ALL=C;export LANG LC_ALL;lpstat -s", "lp -d \"(PRINTER)\"", "system for ", ": ", 1, standardSysQueueTokenHandler }
1378cdf0e10cSrcweir     #else
1379cdf0e10cSrcweir     { "LANG=C;LC_ALL=C;export LANG LC_ALL;lpget list", "lp -d \"(PRINTER)\"", "", ":", 0, lpgetSysQueueTokenHandler },
1380cdf0e10cSrcweir     { "LANG=C;LC_ALL=C;export LANG LC_ALL;lpstat -s", "lp -d \"(PRINTER)\"", "system for ", ": ", 1, standardSysQueueTokenHandler },
1381cdf0e10cSrcweir     { "/usr/sbin/lpc status", "lpr -P \"(PRINTER)\"", "", ":", 0, standardSysQueueTokenHandler },
1382cdf0e10cSrcweir     { "lpc status", "lpr -P \"(PRINTER)\"", "", ":", 0, standardSysQueueTokenHandler }
1383cdf0e10cSrcweir     #endif
1384cdf0e10cSrcweir };
1385cdf0e10cSrcweir 
run()1386cdf0e10cSrcweir void SystemQueueInfo::run()
1387cdf0e10cSrcweir {
1388cdf0e10cSrcweir     char pBuffer[1024];
1389cdf0e10cSrcweir     FILE *pPipe;
1390cdf0e10cSrcweir     std::list< rtl::OString > aLines;
1391cdf0e10cSrcweir 
1392cdf0e10cSrcweir     /* Discover which command we can use to get a list of all printer queues */
1393cdf0e10cSrcweir     for( unsigned int i = 0; i < sizeof(aParms)/sizeof(aParms[0]); i++ )
1394cdf0e10cSrcweir     {
1395cdf0e10cSrcweir         aLines.clear();
1396cdf0e10cSrcweir         rtl::OStringBuffer aCmdLine( 128 );
1397cdf0e10cSrcweir         aCmdLine.append( aParms[i].pQueueCommand );
1398cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
1399cdf0e10cSrcweir         fprintf( stderr, "trying print queue command \"%s\" ... ", aParms[i].pQueueCommand );
1400cdf0e10cSrcweir         #endif
1401cdf0e10cSrcweir         aCmdLine.append( " 2>/dev/null" );
1402cdf0e10cSrcweir         if( (pPipe = popen( aCmdLine.getStr(), "r" )) )
1403cdf0e10cSrcweir         {
1404cdf0e10cSrcweir             while( fgets( pBuffer, 1024, pPipe ) )
1405cdf0e10cSrcweir                 aLines.push_back( rtl::OString( pBuffer ) );
1406cdf0e10cSrcweir             if( ! pclose( pPipe ) )
1407cdf0e10cSrcweir             {
1408cdf0e10cSrcweir                 std::list< PrinterInfoManager::SystemPrintQueue > aSysPrintQueues;
1409cdf0e10cSrcweir                 aParms[i].pHandler( aLines, aSysPrintQueues, &(aParms[i]) );
1410cdf0e10cSrcweir                 MutexGuard aGuard( m_aMutex );
1411cdf0e10cSrcweir                 m_bChanged  = true;
1412cdf0e10cSrcweir                 m_aQueues   = aSysPrintQueues;
1413cdf0e10cSrcweir                 m_aCommand  = rtl::OUString::createFromAscii( aParms[i].pPrintCommand );
1414cdf0e10cSrcweir                 #if OSL_DEBUG_LEVEL > 1
1415cdf0e10cSrcweir                 fprintf( stderr, "success\n" );
1416cdf0e10cSrcweir                 #endif
1417cdf0e10cSrcweir                 break;
1418cdf0e10cSrcweir             }
1419cdf0e10cSrcweir         }
1420cdf0e10cSrcweir         #if OSL_DEBUG_LEVEL > 1
1421cdf0e10cSrcweir         fprintf( stderr, "failed\n" );
1422cdf0e10cSrcweir         #endif
1423cdf0e10cSrcweir     }
1424cdf0e10cSrcweir }
1425cdf0e10cSrcweir 
1426