xref: /trunk/main/vcl/inc/vcl/printerinfomanager.hxx (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _PSPRINT_PRINTERINFOMANAGER_HXX_
25 #define _PSPRINT_PRINTERINFOMANAGER_HXX_
26 
27 #include <hash_map>
28 #include <list>
29 
30 #include "vcl/dllapi.h"
31 #include "vcl/helper.hxx"
32 #include "vcl/jobdata.hxx"
33 #include "vcl/helper.hxx"
34 #include "osl/file.hxx"
35 
36 #include <cstdio>
37 
38 namespace psp
39 {
40 
41 class SystemQueueInfo;
42 
43 struct PrinterInfo : JobData
44 {
45     // basename of PPD
46     rtl::OUString             m_aDriverName;
47     // can be the queue
48     rtl::OUString             m_aLocation;
49     // a user defined comment
50     rtl::OUString             m_aComment;
51     // a command line to pipe a PS-file to
52     rtl::OUString             m_aCommand;
53     // a command line to pipe a PS-file to in case of direct print
54     rtl::OUString             m_aQuickCommand;
55     // a list of special features separated by ',' not used by psprint
56     // but assigned from the outside (currently for "fax","pdf=","autoqueue","external_dialog")
57     rtl::OUString             m_aFeatures;
58     // a mapping of fonts to other fonts.
59     // this provides a method for the user
60     // to replace arbitrary fonts by printer builtin fonts
61     // currently this is only a mapping between font names
62     // assuming that only adbobe standard encoding fonts are
63     // built into the printer. in future it may be necessary
64     // to map to a font name and UCS2 vector which should be mapped
65     // this vector is currently implicitly given by the adobe
66     // standard encoding
67     bool                        m_bPerformFontSubstitution;
68     std::hash_map< rtl::OUString, rtl::OUString, rtl::OUStringHash >
69     m_aFontSubstitutes;
70     std::hash_map< fontID, fontID >
71     m_aFontSubstitutions;
72 
PrinterInfopsp::PrinterInfo73     PrinterInfo() :
74             JobData(),
75             m_bPerformFontSubstitution( false )
76     {}
77 };
78 
79 class VCL_DLLPUBLIC PrinterInfoManager
80 {
81 public:
82     enum Type { Default = 0, CUPS = 1 };
83 
84     struct SystemPrintQueue
85     {
86         rtl::OUString       m_aQueue;
87         rtl::OUString       m_aLocation;
88         rtl::OUString       m_aComment;
89     };
90 protected:
91     // needed for checkPrintersChanged: files (not necessarily existent)
92     // and their last known modification time
93     struct WatchFile
94     {
95         // the file in question
96         rtl::OUString         m_aFilePath;
97         // the last know modification time or 0, if file did not exist
98         TimeValue               m_aModified;
99     };
100 
101     // internal data to describe a printer
102     struct Printer
103     {
104         // configuration file containing this printer
105         // empty means a freshly added printer that has to be saved yet
106         rtl::OUString         m_aFile;
107         // details other config files that have this printer
108         // in case of removal all have to be removed
109         std::list< rtl::OUString > m_aAlternateFiles;
110         // group in m_aFile containing the printer
111         // this must be unique over all configuration files
112         // it usually should be the printer name
113         rtl::OString          m_aGroup;
114         // whether changes need to be saved
115         bool                    m_bModified;
116         // the corresponding info and job data
117         PrinterInfo             m_aInfo;
118     };
119 
120     std::hash_map< rtl::OUString, Printer, rtl::OUStringHash > m_aPrinters;
121     PrinterInfo                         m_aGlobalDefaults;
122     std::list< WatchFile >            m_aWatchFiles;
123     rtl::OUString                     m_aDefaultPrinter;
124     rtl::OUString                     m_aSystemPrintCommand;
125 
126     std::list< SystemPrintQueue >     m_aSystemPrintQueues;
127 
128     SystemQueueInfo*				  m_pQueueInfo;
129 
130     Type						      m_eType;
131     bool                              m_bUseIncludeFeature;
132     bool                              m_bUseJobPatch;
133     rtl::OUString                     m_aSystemDefaultPaper;
134 
135     bool                              m_bDisableCUPS;
136 
137     PrinterInfoManager( Type eType = Default );
138     virtual ~PrinterInfoManager();
139 
140     virtual void initialize();
141 
142     // fill in font substitutions
143     // the resulting hash_map maps from source to target font ids
144     void fillFontSubstitutions( PrinterInfo& rInfo ) const;
145 
146     // fill default paper if not configured in config file
147     // default paper is e.g. locale dependent
148     // if a paper is already set it will not be overwritten
149     void setDefaultPaper( PPDContext& rInfo ) const;
150 
151     void initSystemDefaultPaper();
152 public:
153 
154     // there can only be one
155     static PrinterInfoManager& get();
156     // only called by SalData destructor, frees the global instance
157     static void release();
158 
159     // get PrinterInfoManager type
getType() const160     Type getType() const { return m_eType; }
161 
162     // lists the names of all known printers
163     void listPrinters( std::list< rtl::OUString >& rList ) const;
164 
165     // gets the number of known printers
countPrinters() const166     int countPrinters() const { return m_aPrinters.size(); }
167 
168     // gets info about a named printer
169     const PrinterInfo& getPrinterInfo( const rtl::OUString& rPrinter ) const;
170 
171     // gets the name of the default printer
getDefaultPrinter() const172     const rtl::OUString& getDefaultPrinter() const { return m_aDefaultPrinter; }
173 
174     virtual void setupJobContextData( JobData& rData );
175 
176     // changes the info about a named printer
177     virtual void changePrinterInfo( const rtl::OUString& rPrinter, const PrinterInfo& rNewInfo );
178 
179     // check if the printer configuration has changed
180     // if bwait is true, then this method waits for eventual asynchronous
181     // printer discovery to finish
182     virtual bool checkPrintersChanged( bool bWait );
183 
184     // members for administration (->padmin)
185 
186     // add a named printer
187     // addPrinter fails if a printer with the same name already exists
188     // or the driver does not exist
189     virtual bool addPrinter( const rtl::OUString& rPrinterName, const rtl::OUString& rDriverName );
190 
191     // remove a named printer
192     // this fails if the config file belonging to this printer
193     // is not writeable
194     // if bCheckOnly is true, the printer is not really removed;
195     // this is for checking if the removal would fail
196     virtual bool removePrinter( const rtl::OUString& rPrinterName, bool bCheckOnly = false );
197 
198     // save the changes to all printers. this fails if there
199     // is no writable config file at all
200     virtual bool writePrinterConfig();
201 
202     // set a new default printer
203     // fails if the specified printer does not exist
204     virtual bool setDefaultPrinter( const rtl::OUString& rPrinterName );
205 
206     // primarily used internally but also by padmin
207     // returns the printer queue names
208     virtual const std::list< SystemPrintQueue >& getSystemPrintQueues();
209 
210     // similar but returnse whole commandlines
211     virtual void getSystemPrintCommands( std::list< rtl::OUString >& rCommands );
212 
213     // abstract print command
214     // returns a stdio FILE* that a postscript file may be written to
215     // this may either be a regular file or the result of popen()
216     virtual FILE* startSpool( const rtl::OUString& rPrinterName, bool bQuickCommand );
217     // close the FILE* returned by startSpool and does the actual spooling
218     // set bBanner to "false" will attempt to suppress banner printing
219     // set bBanner to "true" will rely on the system default
220     // returns a numerical job id
221     virtual int endSpool( const rtl::OUString& rPrinterName, const rtl::OUString& rJobTitle, FILE* pFile, const JobData& rDocumentJobData, bool bBanner );
222 
223     // for spadmin: whether adding or removing a printer is possible
224     virtual bool addOrRemovePossible() const;
225 
getUseIncludeFeature() const226     bool getUseIncludeFeature() const { return m_bUseIncludeFeature; }
getUseJobPatch() const227     bool getUseJobPatch() const { return m_bUseJobPatch; }
228 
229     // check whether a printer's feature string contains a subfeature
230     bool checkFeatureToken( const rtl::OUString& rPrinterName, const char* pToken ) const;
231 
232     // set m_bDisableCUPS and update printer config
233     void setCUPSDisabled( bool );
234 
235     // gets m_bDisableCUPS, initialized from printer config
236     bool isCUPSDisabled() const;
237 };
238 
239 } // namespace
240 
241 #endif // _PSPRINT_PRINTERINFOMANAGER_HXX_
242