1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 
21 #include "secondpage.h"
22 #include "thirdpage.h"
23 #include "fourthpage.h"
24 #include "UtilTypes.h"
25 #include "Logger.h"
26 #include "dns_sd.h"
27 #include <stdexcept>
28 #include <map>
29 
30 using namespace PrinterSetupWizard;
31 
32 // CPrinterSetupWizardSheet
33 
34 class CPrinterSetupWizardSheet : public CPropertySheet
35 {
36 DECLARE_DYNAMIC(CPrinterSetupWizardSheet)
37 
38 public:
39 
40 struct WizardException
41 {
42     CString text;
43     CString caption;
44 };
45 
46 public:
47 
48 CPrinterSetupWizardSheet(UINT nIDCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);
49 virtual ~CPrinterSetupWizardSheet();
50 
51 CPropertyPage*
52 GetLastPage();
53 
54 void
55 SetLastPage(CPropertyPage * page );
56 
57 void
58 SetSelectedPrinter(Printer * printer);
59 
60 Printer*
61 GetSelectedPrinter();
62 
63 OSStatus
64 LoadPrinterDriver(const CString & filename);
65 
66 HCURSOR
67 GetCursor();
68 
69 //
70 // handles end of process event
71 //
72 virtual LRESULT
73 OnProcessEvent(WPARAM inWParam, LPARAM inLParam);
74 
75 virtual LRESULT
76 OnSocketEvent(WPARAM inWParam, LPARAM inLParam);
77 
78 virtual BOOL
79 OnCommand(WPARAM wParam, LPARAM lParam);
80 
81 virtual BOOL
82 OnInitDialog();
83 
84 virtual BOOL
85 OnSetCursor(CWnd * pWnd, UINT nHitTest, UINT message);
86 
87 virtual void
88 OnContextMenu(CWnd * pWnd, CPoint pos);
89 
90 afx_msg void
91 OnOK();
92 
93 OSStatus
94 StartResolve( Printer * printer );
95 
96 OSStatus
97 StopResolve( Printer * printer );
98 
99 Printers m_printers;
100 
101 HCURSOR m_active;
102 HCURSOR m_arrow;
103 HCURSOR m_wait;
104 
105 protected:
106 DECLARE_MESSAGE_MAP()
107 CSecondPage m_pgSecond;
108 CThirdPage m_pgThird;
109 CFourthPage m_pgFourth;
110 
111 void
112 OnServiceResolved(
113     Service             *   service);
114 
115 void Init(void);
116 
117 private:
118 
119 // This is from <cups/http.h>
120 typedef enum http_encryption_e          /**** HTTP encryption values ****/
121 {
122     HTTP_ENCRYPT_IF_REQUESTED,          /* Encrypt if requested (TLS upgrade) */
123     HTTP_ENCRYPT_NEVER,             /* Never encrypt */
124     HTTP_ENCRYPT_REQUIRED,          /* Encryption is required (TLS upgrade) */
125     HTTP_ENCRYPT_ALWAYS             /* Always encrypt (SSL) */
126 } http_encryption_t;
127 
128 typedef void*               ( *httpConnectEncryptFunc )( const char* host, int port, http_encryption_t encryption );
129 typedef http_encryption_t ( *cupsEncryptionFunc )( void );
130 typedef void ( *cupsSetEncryptionFunc )( http_encryption_t e );
131 typedef char*               ( *cupsAdminCreateWindowsPPDFunc )( void * http, const char *dest, char *buffer, int bufsize );
132 
133 class CUPSLibrary
134 {
135 public:
136 
CUPSLibrary()137 CUPSLibrary()
138     :
139     httpConnectEncrypt( NULL ),
140     cupsEncryption( NULL ),
141     cupsSetEncryption( NULL ),
142     cupsAdminCreateWindowsPPD( NULL ),
143     library( NULL )
144 {
145 #if defined( LIBCUPS_ENABLED )
146     if ( ( library = LoadLibrary( TEXT( "libcups2.dll" ) ) ) != NULL )
147     {
148         httpConnectEncrypt = ( httpConnectEncryptFunc ) GetProcAddress( library, "httpConnectEncrypt" );
149         cupsEncryption = ( cupsEncryptionFunc ) GetProcAddress( library, "cupsEncryption" );
150         cupsSetEncryption = ( cupsSetEncryptionFunc ) GetProcAddress( library, "cupsSetEncryption" );
151         cupsAdminCreateWindowsPPD = ( cupsAdminCreateWindowsPPDFunc ) GetProcAddress( library, "cupsAdminCreateWindowsPPD" );
152     }
153 #endif
154 }
155 
~CUPSLibrary()156 ~CUPSLibrary()
157 {
158     if ( library )
159     {
160         FreeLibrary( library );
161         library = NULL;
162     }
163 }
164 
165 BOOL
IsInstalled()166 IsInstalled()
167 {
168     return ( ( httpConnectEncrypt != NULL ) && ( cupsEncryption != NULL ) && ( cupsSetEncryption != NULL ) && ( cupsAdminCreateWindowsPPD != NULL ) );
169 }
170 
171 httpConnectEncryptFunc httpConnectEncrypt;
172 cupsEncryptionFunc cupsEncryption;
173 cupsSetEncryptionFunc cupsSetEncryption;
174 cupsAdminCreateWindowsPPDFunc cupsAdminCreateWindowsPPD;
175 
176 private:
177 
178 HMODULE library;
179 };
180 
181 
182 static void DNSSD_API
183 OnBrowse(
184     DNSServiceRef inRef,
185     DNSServiceFlags inFlags,
186     uint32_t inInterfaceIndex,
187     DNSServiceErrorType inErrorCode,
188     const char *            inName,
189     const char *            inType,
190     const char *            inDomain,
191     void *                  inContext );
192 
193 static void DNSSD_API
194 OnResolve(
195     DNSServiceRef inRef,
196     DNSServiceFlags inFlags,
197     uint32_t inInterfaceIndex,
198     DNSServiceErrorType inErrorCode,
199     const char *            inFullName,
200     const char *            inHostName,
201     uint16_t inPort,
202     uint16_t inTXTSize,
203     const char *            inTXT,
204     void *                  inContext );
205 
206 static void DNSSD_API
207 OnQuery(
208     DNSServiceRef inRef,
209     DNSServiceFlags inFlags,
210     uint32_t inInterfaceIndex,
211     DNSServiceErrorType inErrorCode,
212     const char          *   inFullName,
213     uint16_t inRRType,
214     uint16_t inRRClass,
215     uint16_t inRDLen,
216     const void          *   inRData,
217     uint32_t inTTL,
218     void                *   inContext);
219 
220 Printer*
221 OnAddPrinter(
222     uint32_t inInterfaceIndex,
223     const char          *   inName,
224     const char          *   inType,
225     const char          *   inDomain,
226     bool moreComing);
227 
228 OSStatus
229 OnRemovePrinter(
230     Printer             *   printer,
231     bool moreComing);
232 
233 OSStatus
234 OnAddService(
235     Printer             *   printer,
236     uint32_t inInterfaceIndex,
237     const char          *   inName,
238     const char          *   inType,
239     const char          *   inDomain);
240 
241 OSStatus
242 OnRemoveService(
243     Service             *   service);
244 
245 void
246 OnResolveService(
247     Service             *   service );
248 
249 static bool
250 OrderServiceFunc( const Service * a, const Service * b );
251 
252 static bool
253 OrderQueueFunc( const Queue * q1, const Queue * q2 );
254 
255 OSStatus
256 StartOperation( DNSServiceRef ref );
257 
258 OSStatus
259 StopOperation( DNSServiceRef & ref );
260 
261 OSStatus
262 StartBrowse();
263 
264 OSStatus
265 StopBrowse();
266 
267 OSStatus
268 StartResolve( Service * service );
269 
270 OSStatus
271 StopResolve( Service * service );
272 
273 OSStatus
274 ParseTextRecord( Service * service, Queue * q, uint16_t inTXTSize, const char * inTXT );
275 
276 OSStatus
277 LoadPrinterNames();
278 
279 Printer*
280 Lookup( const char * name );
281 
282 OSStatus
283 InstallPrinter(Printer * printer);
284 
285 OSStatus
286 InstallPrinterPort( Printer * printer, Service * service, DWORD protocol, Logger & log );
287 
288 OSStatus
289 InstallPrinterPDLAndLPR(Printer * printer, Service * service, Logger & log);
290 
291 OSStatus
292 InstallPrinterIPP(Printer * printer, Service * service, Logger & log);
293 
294 OSStatus
295 InstallPrinterCUPS( Printer * printer, Service * service, CUPSLibrary & cupsLib );
296 
297 OSStatus
298 InstallPrinterCUPS(Printer * printer, Service * service, CUPSLibrary & cupsLib, TCHAR * env );
299 
300 static unsigned WINAPI
301 InstallDriverThread( LPVOID inParam );
302 
303 typedef std::list<CString>          PrinterNames;
304 typedef std::list<DNSServiceRef>    ServiceRefList;
305 static CPrinterSetupWizardSheet *   m_self;
306 PrinterNames m_printerNames;
307 Printer                         *   m_selectedPrinter;
308 bool m_driverThreadFinished;
309 DWORD m_driverThreadExitCode;
310 ServiceRefList m_serviceRefList;
311 DNSServiceRef m_pdlBrowser;
312 DNSServiceRef m_lprBrowser;
313 DNSServiceRef m_ippBrowser;
314 DNSServiceRef m_resolver;
315 
316 CPropertyPage                   *   m_lastPage;
317 };
318 
319 
320 inline Printer*
GetSelectedPrinter()321 CPrinterSetupWizardSheet::GetSelectedPrinter()
322 {
323     return m_selectedPrinter;
324 }
325 
326 
327 inline HCURSOR
GetCursor()328 CPrinterSetupWizardSheet::GetCursor()
329 {
330     return m_active;
331 }
332 
333 
334 inline CPropertyPage*
GetLastPage()335 CPrinterSetupWizardSheet::GetLastPage()
336 {
337     return m_lastPage;
338 }
339 
340 
341 inline void
SetLastPage(CPropertyPage * lastPage)342 CPrinterSetupWizardSheet::SetLastPage(CPropertyPage * lastPage)
343 {
344     m_lastPage = lastPage;
345 }
346 
347 
348 // Service Types
349 
350 #define kPDLServiceType     "_pdl-datastream._tcp."
351 #define kLPRServiceType     "_printer._tcp."
352 #define kIPPServiceType     "_ipp._tcp."
353