1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
19 $Log: ThirdPage.cpp,v $
20 Revision 1.42 2009/07/07 22:04:55 herscher
21 <rdar://problem/4176343> LOC Impact: Need custom text when selecting the wrong printer driver
23 Revision 1.41 2009/06/18 18:05:50 herscher
24 <rdar://problem/4694554> Eliminate the first screen of Printer Wizard and maybe combine others ("I'm Feeling Lucky")
26 Revision 1.40 2009/05/29 20:43:36 herscher
27 <rdar://problem/6928136> Printer Wizard doesn't work correctly in Windows 7 64 bit
29 Revision 1.39 2009/05/27 06:25:49 herscher
30 <rdar://problem/4176334> Need error dialog when selecting bad INF file
32 Revision 1.38 2009/05/27 04:59:57 herscher
33 <rdar://problem/4517393> COMPATIBILITY WITH HP CLJ4700
34 <rdar://problem/6142138> Compatibility with Samsung print driver files
36 Revision 1.37 2007/06/08 06:30:26 herscher
37 <rdar://problem/5257700> Fix uninitialized pointers when detecting generic PCL and PS drivers
39 Revision 1.36 2007/06/06 20:39:10 cheshire
40 <rdar://problem/5254377> Printer Setup Wizard started crashing in Bonjour104A8, after update to Visual Studio 2005
42 Revision 1.35 2007/06/06 20:08:01 cheshire
43 <rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
44 AutoScroll model list as well as manufacturer list
46 Revision 1.34 2007/06/06 19:53:48 cheshire
47 <rdar://problem/5187308> Move build train to Visual Studio 2005
49 Revision 1.33 2007/04/20 22:58:10 herscher
50 <rdar://problem/4826126> mDNS: Printer Wizard doesn't offer generic HP printers or generic PS support on Vista RC2
52 Revision 1.32 2007/04/13 23:42:20 herscher
53 <rdar://problem/4580061> mDNS: Printers added using Bonjour should be set as the default printer.
55 Revision 1.31 2007/04/13 21:38:46 herscher
56 <rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
58 Revision 1.30 2007/04/13 20:23:40 herscher
59 Fixed mistake in previous checkin that reverted license text for this file
61 Revision 1.29 2007/04/13 18:10:24 herscher
62 <rdar://problem/4496652> mDNS: Don't allow user to choose non-working driver
64 Revision 1.28 2006/08/14 23:24:09 cheshire
65 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
67 Revision 1.27 2005/10/05 21:41:45 herscher
68 <rdar://problem/4190104> Use "application/octet-stream" to determine if CUPS shared queue supports raw
70 Revision 1.26 2005/07/11 20:17:15 shersche
71 <rdar://problem/4124524> UI fixes associated with CUPS printer workaround fix.
73 Revision 1.25 2005/07/07 17:53:20 shersche
74 Fix problems associated with the CUPS printer workaround fix.
76 Revision 1.24 2005/06/30 18:02:54 shersche
77 <rdar://problem/4124524> Workaround for Mac OS X Printer Sharing bug
79 Revision 1.23 2005/04/18 02:33:47 shersche
80 <rdar://problem/4091216> Default printer option cannot be deselected
82 Revision 1.22 2005/04/13 17:46:22 shersche
83 <rdar://problem/4082122> Generic PCL not selected when printers advertise multiple text records
85 Revision 1.21 2005/03/30 02:09:55 shersche
86 Auto-resize the column width to account for differing fonts and font sizes
88 Revision 1.20 2005/03/05 02:27:45 shersche
89 <rdar://problem/4030388> Generic drivers don't do color
91 Revision 1.19 2005/02/23 02:08:51 shersche
92 <rdar://problem/4012275> If we can't match the manufacturer, and select a generic printer, then show all the manufacturers in the manufacturer pane, not just "Generic".
94 Revision 1.18 2005/02/15 07:02:51 shersche
95 <rdar://problem/4003724> Display different UI text when generic printer drivers are selected
97 Revision 1.17 2005/02/08 21:45:06 shersche
98 <rdar://problem/3947490> Default to Generic PostScript or PCL if unable to match driver
100 Revision 1.16 2005/02/08 18:56:03 shersche
101 Fix generated IPP url so that it doesn't add "/printers" string
103 Revision 1.15 2005/02/01 01:44:07 shersche
104 Load ntprint.inf at startup. This will cause the wizard to take a second or two longer to come up, but will eliminate the pause when auto-selecting the print drivers.
106 Revision 1.14 2005/01/25 08:55:54 shersche
107 <rdar://problem/3911084> Load icons at run-time from resource DLL
110 Revision 1.13 2005/01/06 08:15:45 shersche
111 Append queue name to end of LPR port name, correctly build port name when queue name is absent
113 Revision 1.12 2005/01/05 01:06:12 shersche
114 <rdar://problem/3841218> Strip the first substring off the product key if an initial match can't be found with the whole product key.
117 Revision 1.11 2004/12/29 18:53:38 shersche
118 <rdar://problem/3725106>
119 <rdar://problem/3737413> Added support for LPR and IPP protocols as well as support for obtaining multiple text records. Reorganized and simplified codebase.
120 Bug #: 3725106, 3737413
122 Revision 1.10 2004/10/11 22:55:34 shersche
123 <rdar://problem/3827624> Use the IP port number when deriving the printer port name.
126 Revision 1.9 2004/06/27 23:08:00 shersche
127 code cleanup, make sure EnumPrintDrivers returns non-zero value, ignore comments in inf files
129 Revision 1.8 2004/06/27 08:06:45 shersche
130 Parse [Strings] section of inf file
132 Revision 1.7 2004/06/26 04:00:05 shersche
133 fix warnings compiling in debug mode
134 Submitted by: herscher
136 Revision 1.6 2004/06/26 03:19:57 shersche
137 clean up warning messages
139 Submitted by: herscher
141 Revision 1.5 2004/06/25 05:06:02 shersche
142 Trim whitespace from key/value pairs when parsing inf files
143 Submitted by: herscher
145 Revision 1.4 2004/06/25 02:44:13 shersche
146 Tweaked code to handle Xerox Phaser printer identification
147 Submitted by: herscher
149 Revision 1.3 2004/06/25 02:27:58 shersche
150 Do a CListCtrl::FindItem() before calling CListCtrl::SetItemState().
151 Submitted by: herscher
153 Revision 1.2 2004/06/23 18:09:23 shersche
154 Normalize tag names when parsing inf files.
155 Submitted by: herscher
157 Revision 1.1 2004/06/18 04:36:58 rpantos
162 #include "PrinterSetupWizardApp.h"
163 #include "PrinterSetupWizardSheet.h"
164 #include "ThirdPage.h"
167 #include <winspool.h>
168 #include <setupapi.h>
170 // local variable is initialize but not referenced
171 #pragma warning(disable:4189)
174 // This is the printer description file that is shipped
175 // with Windows XP and below
177 #define kNTPrintFile L"inf\\ntprint.inf"
180 // Windows Vista ships with a set of prn*.inf files
182 #define kVistaPrintFiles L"inf\\prn*.inf"
185 // These are pre-defined names for Generic manufacturer and model
187 #define kGenericManufacturer L"Generic"
188 #define kGenericText L"Generic / Text Only"
189 #define kGenericPostscript L"Generic / Postscript"
190 #define kGenericPCL L"Generic / PCL"
191 #define kPDLPostscriptKey L"application/postscript"
192 #define kPDLPCLKey L"application/vnd.hp-pcl"
193 #define kGenericPSColorDriver L"HP Color LaserJet 4550 PS"
194 #define kGenericPSDriver L"HP LaserJet 4050 Series PS"
195 #define kGenericPCLColorDriver L"HP Color LaserJet 4550 PCL"
196 #define kGenericPCLDriver L"HP LaserJet 4050 Series PCL"
201 IMPLEMENT_DYNAMIC(CThirdPage
, CPropertyPage
)
202 CThirdPage::CThirdPage()
203 : CPropertyPage(CThirdPage::IDD
),
204 m_manufacturerSelected( NULL
),
205 m_modelSelected( NULL
),
206 m_genericPostscript( NULL
),
207 m_genericPCL( NULL
),
208 m_initialized(false),
209 m_printerImage( NULL
)
211 static const int bufferSize
= 32768;
212 TCHAR windowsDirectory
[bufferSize
];
214 WIN32_FIND_DATA findFileData
;
221 m_psp
.dwFlags
&= ~(PSP_HASHELP
);
222 m_psp
.dwFlags
|= PSP_DEFAULT
|PSP_USEHEADERTITLE
|PSP_USEHEADERSUBTITLE
;
224 m_psp
.pszHeaderTitle
= MAKEINTRESOURCE(IDS_INSTALL_TITLE
);
225 m_psp
.pszHeaderSubTitle
= MAKEINTRESOURCE(IDS_INSTALL_SUBTITLE
);
228 // load printers from ntprint.inf
230 ok
= GetWindowsDirectory( windowsDirectory
, bufferSize
);
231 err
= translate_errno( ok
, errno_compat(), kUnknownErr
);
232 require_noerr( err
, exit
);
235 // <rdar://problem/4826126>
237 // If there are no *prn.inf files, we'll assume that the information
240 prnFiles
.Format( L
"%s\\%s", windowsDirectory
, kVistaPrintFiles
);
241 findHandle
= FindFirstFile( prnFiles
, &findFileData
);
243 if ( findHandle
!= INVALID_HANDLE_VALUE
)
247 absolute
.Format( L
"%s\\inf\\%s", windowsDirectory
, findFileData
.cFileName
);
248 err
= LoadPrintDriverDefsFromFile( m_manufacturers
, absolute
, false );
249 require_noerr( err
, exit
);
251 while ( FindNextFile( findHandle
, &findFileData
) )
253 absolute
.Format( L
"%s\\inf\\%s", windowsDirectory
, findFileData
.cFileName
);
254 err
= LoadPrintDriverDefsFromFile( m_manufacturers
, absolute
, false );
255 require_noerr( err
, exit
);
258 FindClose( findHandle
);
262 ntPrint
.Format(L
"%s\\%s", windowsDirectory
, kNTPrintFile
);
263 err
= LoadPrintDriverDefsFromFile( m_manufacturers
, ntPrint
, false );
264 require_noerr(err
, exit
);
268 // load printer drivers that have been installed on this machine
270 err
= LoadPrintDriverDefs( m_manufacturers
);
271 require_noerr(err
, exit
);
274 // load our own special generic printer defs
276 err
= LoadGenericPrintDriverDefs( m_manufacturers
);
277 require_noerr( err
, exit
);
284 CThirdPage::~CThirdPage()
287 // clean up all the printer manufacturers
289 while (m_manufacturers
.size())
291 Manufacturers::iterator iter
= m_manufacturers
.begin();
293 while (iter
->second
->models
.size())
295 Models::iterator it
= iter
->second
->models
.begin();
301 iter
->second
->models
.erase(it
);
306 m_manufacturers
.erase(iter
);
310 // ----------------------------------------------------
313 // SelectMatch will do all the UI work associated with
314 // selected a manufacturer and model of printer. It also
315 // makes sure the printer object is update with the
318 // ----------------------------------------------------
320 CThirdPage::SelectMatch(Printer
* printer
, Service
* service
, Manufacturer
* manufacturer
, Model
* model
)
325 check( printer
!= NULL
);
326 check( manufacturer
!= NULL
);
327 check( model
!= NULL
);
330 // select the manufacturer
332 info
.flags
= LVFI_STRING
;
333 info
.psz
= manufacturer
->name
;
335 nIndex
= m_manufacturerListCtrl
.FindItem(&info
);
339 m_manufacturerListCtrl
.SetItemState(nIndex
, LVIS_SELECTED
, LVIS_SELECTED
);
341 //<rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
343 AutoScroll(m_manufacturerListCtrl
, nIndex
);
349 info
.flags
= LVFI_STRING
;
350 info
.psz
= model
->displayName
;
352 nIndex
= m_modelListCtrl
.FindItem(&info
);
356 m_modelListCtrl
.SetItemState(nIndex
, LVIS_SELECTED
, LVIS_SELECTED
);
357 AutoScroll( m_modelListCtrl
, nIndex
);
359 m_modelListCtrl
.SetFocus();
362 CopyPrinterSettings( printer
, service
, manufacturer
, model
);
366 CThirdPage::SelectMatch(Manufacturers
& manufacturers
, Printer
* printer
, Service
* service
, Manufacturer
* manufacturer
, Model
* model
)
368 PopulateUI( manufacturers
);
370 SelectMatch( printer
, service
, manufacturer
, model
);
373 // --------------------------------------------------------
374 // CopyPrinterSettings
376 // This function makes sure that the printer object has the
377 // latest settings from the manufacturer and model objects
378 // --------------------------------------------------------
381 CThirdPage::CopyPrinterSettings( Printer
* printer
, Service
* service
, Manufacturer
* manufacturer
, Model
* model
)
383 printer
->manufacturer
= manufacturer
->name
;
384 printer
->displayModelName
= model
->displayName
;
385 printer
->modelName
= model
->name
;
386 printer
->driverInstalled
= model
->driverInstalled
;
387 printer
->infFileName
= model
->infFileName
;
389 if ( service
->type
== kPDLServiceType
)
391 printer
->portName
.Format(L
"IP_%s.%d", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
);
392 service
->protocol
= L
"Raw";
394 else if ( service
->type
== kLPRServiceType
)
396 Queue
* q
= service
->queues
.front();
399 if ( q
->name
.GetLength() > 0 )
401 printer
->portName
.Format(L
"LPR_%s.%d.%s", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
, static_cast<LPCTSTR
>(q
->name
) );
405 printer
->portName
.Format(L
"LPR_%s.%d", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
);
408 service
->protocol
= L
"LPR";
410 else if ( service
->type
== kIPPServiceType
)
412 Queue
* q
= service
->queues
.front();
415 if ( q
->name
.GetLength() > 0 )
417 printer
->portName
.Format(L
"http://%s:%d/%s", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
, static_cast<LPCTSTR
>(q
->name
) );
421 printer
->portName
.Format(L
"http://%s:%d/", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
);
424 service
->protocol
= L
"IPP";
428 // --------------------------------------------------------
429 // DefaultPrinterExists
431 // Checks to see if a default printer has been configured
433 // --------------------------------------------------------
435 CThirdPage::DefaultPrinterExists()
437 CPrintDialog
dlg(FALSE
);
439 dlg
.m_pd
.Flags
|= PD_RETURNDEFAULT
;
441 return dlg
.GetDefaults();
444 // --------------------------------------------------------
447 // Ensure selected item is in middle of list
448 // --------------------------------------------------------
450 CThirdPage::AutoScroll( CListCtrl
& list
, int nIndex
)
453 //<rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
459 list
.EnsureVisible( nIndex
, FALSE
);
461 top
= list
.GetTopIndex();
462 count
= list
.GetCountPerPage();
464 if ( ( nIndex
== top
) || ( ( nIndex
+ 1 ) == ( top
+ count
) ) )
469 rows
= ( count
/ 2 );
473 list
.GetItemRect(0, rect
, LVIR_BOUNDS
);
474 list
.Scroll( CPoint( 0, rows
* rect
.Height() * -1 ) );
478 list
.GetItemRect(0, rect
, LVIR_BOUNDS
);
479 list
.Scroll( CPoint( 0, rows
* rect
.Height() ) );
484 // ------------------------------------------------------
485 // LoadPrintDriverDefsFromFile
487 // The only potentially opaque thing about this function is the
488 // checkForDuplicateModels flag. The problem here is that ntprint.inf
489 // doesn't contain duplicate models, and it has hundreds of models
490 // listed. You wouldn't check for duplicates there. But oftentimes,
491 // loading different windows print driver files contain multiple
492 // entries for the same printer. You don't want the UI to display
493 // the same printer multiple times, so in that case, you would ask
494 // this function to check for multiple models.
497 CThirdPage::LoadPrintDriverDefsFromFile(Manufacturers
& manufacturers
, const CString
& filename
, bool checkForDuplicateModels
)
499 HINF handle
= INVALID_HANDLE_VALUE
;
500 const TCHAR
* section
= TEXT( "Manufacturer" );
504 INFCONTEXT manufacturerContext
;
508 // Make sure we can open the file
509 handle
= SetupOpenInfFile( filename
, NULL
, INF_STYLE_WIN4
, NULL
);
510 translate_errno( handle
!= INVALID_HANDLE_VALUE
, GetLastError(), kUnknownErr
);
511 require_noerr( err
, exit
);
513 // Make sure it's a printer file
514 ok
= SetupGetLineText( NULL
, handle
, TEXT( "Version" ), TEXT( "Class" ), line
, sizeof( line
), NULL
);
515 translate_errno( ok
, GetLastError(), kUnknownErr
);
516 require_noerr( err
, exit
);
518 require_action( klass
== TEXT( "Printer" ), exit
, err
= kUnknownErr
);
520 sectionCount
= SetupGetLineCount( handle
, section
);
521 translate_errno( sectionCount
!= -1, GetLastError(), kUnknownErr
);
522 require_noerr( err
, exit
);
524 memset( &manufacturerContext
, 0, sizeof( manufacturerContext
) );
526 for ( LONG i
= 0; i
< sectionCount
; i
++ )
528 Manufacturers::iterator iter
;
529 Manufacturer
* manufacturer
;
530 CString manufacturerName
;
532 CStringList modelSectionNameDecl
;
533 CString modelSectionName
;
534 CString baseModelName
;
536 INFCONTEXT modelContext
;
542 ok
= SetupFindFirstLine( handle
, section
, NULL
, &manufacturerContext
);
543 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
544 require_noerr( err
, exit
);
548 ok
= SetupFindNextLine( &manufacturerContext
, &manufacturerContext
);
549 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
550 require_noerr( err
, exit
);
553 ok
= SetupGetStringField( &manufacturerContext
, 0, line
, sizeof( line
), NULL
);
554 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
555 require_noerr( err
, exit
);
556 manufacturerName
= line
;
558 ok
= SetupGetLineText( &manufacturerContext
, handle
, NULL
, NULL
, line
, sizeof( line
), NULL
);
559 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
560 require_noerr( err
, exit
);
562 // Try to find some model section name that has entries. Explanation of int file structure
565 // <http://msdn.microsoft.com/en-us/library/ms794359.aspx>
566 Split( line
, ',', modelSectionNameDecl
);
568 p
= modelSectionNameDecl
.GetHeadPosition();
569 modelSectionName
= modelSectionNameDecl
.GetNext( p
);
570 modelCount
= SetupGetLineCount( handle
, modelSectionName
);
571 baseModelName
= modelSectionName
;
573 while ( modelCount
<= 0 && p
)
575 CString targetOSVersion
;
577 targetOSVersion
= modelSectionNameDecl
.GetNext( p
);
578 modelSectionName
= baseModelName
+ TEXT( "." ) + targetOSVersion
;
579 modelCount
= SetupGetLineCount( handle
, modelSectionName
);
582 if ( modelCount
> 0 )
584 manufacturerName
= NormalizeManufacturerName( manufacturerName
);
586 iter
= manufacturers
.find( manufacturerName
);
588 if ( iter
!= manufacturers
.end() )
590 manufacturer
= iter
->second
;
591 require_action( manufacturer
, exit
, err
= kUnknownErr
);
597 manufacturer
= new Manufacturer
;
604 require_action( manufacturer
, exit
, err
= kNoMemoryErr
);
606 manufacturer
->name
= manufacturerName
;
607 manufacturers
[ manufacturerName
] = manufacturer
;
610 memset( &modelContext
, 0, sizeof( modelContext
) );
612 for ( LONG j
= 0; j
< modelCount
; j
++ )
619 ok
= SetupFindFirstLine( handle
, modelSectionName
, NULL
, &modelContext
);
620 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
621 require_noerr( err
, exit
);
625 SetupFindNextLine( &modelContext
, &modelContext
);
626 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
627 require_noerr( err
, exit
);
630 ok
= SetupGetStringField( &modelContext
, 0, line
, sizeof( line
), NULL
);
631 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
632 require_noerr( err
, exit
);
636 if (checkForDuplicateModels
== true)
638 if ( MatchModel( manufacturer
, ConvertToModelName( modelName
) ) != NULL
)
645 // Stock Vista printer inf files embed guids in the model
646 // declarations for Epson printers. Let's ignore those.
648 if ( modelName
.Find( TEXT( "{" ), 0 ) != -1 )
662 require_action( model
, exit
, err
= kNoMemoryErr
);
664 model
->infFileName
= filename
;
665 model
->displayName
= modelName
;
666 model
->name
= modelName
;
667 model
->driverInstalled
= false;
669 manufacturer
->models
.push_back(model
);
676 if ( handle
!= INVALID_HANDLE_VALUE
)
678 SetupCloseInfFile( handle
);
686 // -------------------------------------------------------
687 // LoadPrintDriverDefs
689 // This function is responsible for loading the print driver
690 // definitions of all print drivers that have been installed
692 // -------------------------------------------------------
694 CThirdPage::LoadPrintDriverDefs( Manufacturers
& manufacturers
)
696 BYTE
* buffer
= NULL
;
697 DWORD bytesReceived
= 0;
698 DWORD numPrinters
= 0;
703 // like a lot of win32 calls, we call this first to get the
704 // size of the buffer we need.
706 EnumPrinterDrivers(NULL
, L
"all", 6, NULL
, 0, &bytesReceived
, &numPrinters
);
708 if (bytesReceived
> 0)
712 buffer
= new BYTE
[bytesReceived
];
719 require_action( buffer
, exit
, err
= kNoMemoryErr
);
722 // this call gets the real info
724 ok
= EnumPrinterDrivers(NULL
, L
"all", 6, buffer
, bytesReceived
, &bytesReceived
, &numPrinters
);
725 err
= translate_errno( ok
, errno_compat(), kUnknownErr
);
726 require_noerr( err
, exit
);
728 DRIVER_INFO_6
* info
= (DRIVER_INFO_6
*) buffer
;
730 for (DWORD i
= 0; i
< numPrinters
; i
++)
732 Manufacturer
* manufacturer
;
737 // skip over anything that doesn't have a manufacturer field. This
738 // fixes a bug that I noticed that occurred after I installed
739 // ProComm. This program add a print driver with no manufacturer
740 // that screwed up this wizard.
742 if (info
[i
].pszMfgName
== NULL
)
748 // look for manufacturer
750 Manufacturers::iterator iter
;
755 name
= NormalizeManufacturerName( info
[i
].pszMfgName
);
757 iter
= manufacturers
.find(name
);
759 if (iter
!= manufacturers
.end())
761 manufacturer
= iter
->second
;
767 manufacturer
= new Manufacturer
;
774 require_action( manufacturer
, exit
, err
= kNoMemoryErr
);
776 manufacturer
->name
= name
;
778 manufacturers
[name
] = manufacturer
;
782 // now look to see if we have already seen this guy. this could
783 // happen if we have already installed printers that are described
784 // in ntprint.inf. the extant drivers will show up in EnumPrinterDrivers
785 // but we have already loaded their info
788 if ( MatchModel( manufacturer
, ConvertToModelName( info
[i
].pName
) ) == NULL
)
799 require_action( model
, exit
, err
= kNoMemoryErr
);
801 model
->displayName
= info
[i
].pName
;
802 model
->name
= info
[i
].pName
;
803 model
->driverInstalled
= true;
805 manufacturer
->models
.push_back(model
);
820 // -------------------------------------------------------
821 // LoadGenericPrintDriverDefs
823 // This function is responsible for loading polymorphic
824 // generic print drivers defs. The UI will read
825 // something like "Generic / Postscript" and we can map
826 // that to any print driver we want.
827 // -------------------------------------------------------
829 CThirdPage::LoadGenericPrintDriverDefs( Manufacturers
& manufacturers
)
831 Manufacturer
* manufacturer
;
833 Manufacturers::iterator iter
;
834 CString psDriverName
;
835 CString pclDriverName
;
838 // <rdar://problem/4030388> Generic drivers don't do color
840 // First try and find our generic driver names
842 iter
= m_manufacturers
.find(L
"HP");
843 require_action( iter
!= m_manufacturers
.end(), exit
, err
= kUnknownErr
);
844 manufacturer
= iter
->second
;
846 // Look for Postscript
848 model
= manufacturer
->find( kGenericPSColorDriver
);
852 model
= manufacturer
->find( kGenericPSDriver
);
857 psDriverName
= model
->name
;
862 model
= manufacturer
->find( kGenericPCLColorDriver
);
866 model
= manufacturer
->find( kGenericPCLDriver
);
871 pclDriverName
= model
->name
;
874 // If we found either a generic PS driver, or a generic PCL driver,
875 // then add them to the list
877 if ( psDriverName
.GetLength() || pclDriverName
.GetLength() )
879 // Try and find generic manufacturer if there is one
881 iter
= manufacturers
.find(L
"Generic");
883 if (iter
!= manufacturers
.end())
885 manufacturer
= iter
->second
;
891 manufacturer
= new Manufacturer
;
898 require_action( manufacturer
, exit
, err
= kNoMemoryErr
);
900 manufacturer
->name
= "Generic";
901 manufacturers
[manufacturer
->name
] = manufacturer
;
904 if ( psDriverName
.GetLength() > 0 )
908 m_genericPostscript
= new Model
;
912 m_genericPostscript
= NULL
;
915 require_action( m_genericPostscript
, exit
, err
= kNoMemoryErr
);
917 m_genericPostscript
->displayName
= kGenericPostscript
;
918 m_genericPostscript
->name
= psDriverName
;
919 m_genericPostscript
->driverInstalled
= false;
921 manufacturer
->models
.push_back( m_genericPostscript
);
924 if ( pclDriverName
.GetLength() > 0 )
928 m_genericPCL
= new Model
;
935 require_action( m_genericPCL
, exit
, err
= kNoMemoryErr
);
937 m_genericPCL
->displayName
= kGenericPCL
;
938 m_genericPCL
->name
= pclDriverName
;
939 m_genericPCL
->driverInstalled
= false;
941 manufacturer
->models
.push_back( m_genericPCL
);
950 // ------------------------------------------------------
951 // ConvertToManufacturerName
953 // This function is responsible for tweaking the
954 // name so that subsequent string operations won't fail because
955 // of capitalizations/different names for the same manufacturer
956 // (i.e. Hewlett-Packard/HP/Hewlett Packard)
959 CThirdPage::ConvertToManufacturerName( const CString
& name
)
962 // first we're going to convert all the characters to lower
965 CString lower
= name
;
969 // now we're going to check to see if the string says "hewlett-packard",
970 // because sometimes they refer to themselves as "hewlett-packard", and
971 // sometimes they refer to themselves as "hp".
973 if ( lower
== L
"hewlett-packard")
979 // tweak for Xerox Phaser, which doesn't announce itself
982 else if ( lower
.Find( L
"phaser", 0 ) != -1 )
990 // ------------------------------------------------------
991 // ConvertToModelName
993 // This function is responsible for ensuring that subsequent
994 // string operations don't fail because of differing capitalization
995 // schemes and the like
996 // ------------------------------------------------------
999 CThirdPage::ConvertToModelName( const CString
& name
)
1002 // convert it to lowercase
1004 CString lower
= name
;
1010 // ------------------------------------------------------
1011 // NormalizeManufacturerName
1013 // This function is responsible for tweaking the manufacturer
1014 // name so that there are no aliases for vendors
1017 CThirdPage::NormalizeManufacturerName( const CString
& name
)
1019 CString normalized
= name
;
1022 // now we're going to check to see if the string says "hewlett-packard",
1023 // because sometimes they refer to themselves as "hewlett-packard", and
1024 // sometimes they refer to themselves as "hp".
1026 if ( normalized
== L
"Hewlett-Packard")
1034 // -------------------------------------------------------
1037 // This function is responsible for matching a printer
1038 // to a list of manufacturers and models. It calls
1039 // MatchManufacturer and MatchModel in turn.
1042 OSStatus
CThirdPage::MatchPrinter(Manufacturers
& manufacturers
, Printer
* printer
, Service
* service
, bool useCUPSWorkaround
)
1044 CString normalizedProductName
;
1045 Manufacturer
* manufacturer
= NULL
;
1046 Manufacturer
* genericManufacturer
= NULL
;
1047 Model
* model
= NULL
;
1048 Model
* genericModel
= NULL
;
1051 OSStatus err
= kNoErr
;
1056 Queue
* q
= service
->SelectedQueue();
1061 // first look to see if we have a usb_MFG descriptor
1063 if ( q
->usb_MFG
.GetLength() > 0)
1065 manufacturer
= MatchManufacturer( manufacturers
, ConvertToManufacturerName ( q
->usb_MFG
) );
1068 if ( manufacturer
== NULL
)
1070 q
->product
.Remove('(');
1071 q
->product
.Remove(')');
1073 manufacturer
= MatchManufacturer( manufacturers
, ConvertToManufacturerName ( q
->product
) );
1077 // if we found the manufacturer, then start looking for the model
1079 if ( manufacturer
!= NULL
)
1081 if ( q
->usb_MDL
.GetLength() > 0 )
1083 model
= MatchModel ( manufacturer
, ConvertToModelName ( q
->usb_MDL
) );
1086 if ( ( model
== NULL
) && ( q
->product
.GetLength() > 0 ) )
1088 q
->product
.Remove('(');
1089 q
->product
.Remove(')');
1091 model
= MatchModel ( manufacturer
, ConvertToModelName ( q
->product
) );
1094 if ( model
!= NULL
)
1096 // <rdar://problem/4124524> Offer Generic printers if printer advertises Postscript or PCL. Workaround
1097 // bug in OS X CUPS printer sharing by selecting Generic driver instead of matched printer.
1099 bool hasGenericDriver
= false;
1101 if ( MatchGeneric( manufacturers
, printer
, service
, &genericManufacturer
, &genericModel
) )
1103 hasGenericDriver
= true;
1106 // <rdar://problem/4190104> Use "application/octet-stream" to determine if CUPS
1107 // shared queue supports raw
1109 if ( q
->pdl
.Find( L
"application/octet-stream" ) != -1 )
1111 useCUPSWorkaround
= false;
1114 if ( useCUPSWorkaround
&& printer
->isSharedFromOSX
&& hasGenericDriver
)
1117 // <rdar://problem/4496652> mDNS: Don't allow user to choose non-working driver
1119 Manufacturers genericManufacturers
;
1121 LoadGenericPrintDriverDefs( genericManufacturers
);
1123 SelectMatch( genericManufacturers
, printer
, service
, genericManufacturer
, genericModel
);
1127 SelectMatch(manufacturers
, printer
, service
, manufacturer
, model
);
1135 // display a message to the user based on whether we could match
1140 text
.LoadString(IDS_PRINTER_MATCH_GOOD
);
1143 else if ( MatchGeneric( manufacturers
, printer
, service
, &genericManufacturer
, &genericModel
) )
1145 if ( printer
->isSharedFromOSX
)
1148 // <rdar://problem/4496652> mDNS: Don't allow user to choose non-working driver
1150 Manufacturers genericManufacturers
;
1152 LoadGenericPrintDriverDefs( genericManufacturers
);
1154 SelectMatch( genericManufacturers
, printer
, service
, genericManufacturer
, genericModel
);
1156 text
.LoadString(IDS_PRINTER_MATCH_GOOD
);
1160 SelectMatch( manufacturers
, printer
, service
, genericManufacturer
, genericModel
);
1161 text
.LoadString(IDS_PRINTER_MATCH_MAYBE
);
1168 text
.LoadString(IDS_PRINTER_MATCH_BAD
);
1171 // if there was any crud in this list from before, get rid of it now
1173 m_modelListCtrl
.DeleteAllItems();
1176 // select the manufacturer if we found one
1178 if (manufacturer
!= NULL
)
1184 // select the manufacturer
1186 info
.flags
= LVFI_STRING
;
1187 info
.psz
= manufacturer
->name
;
1189 nIndex
= m_manufacturerListCtrl
.FindItem(&info
);
1193 m_manufacturerListCtrl
.SetItemState(nIndex
, LVIS_SELECTED
, LVIS_SELECTED
);
1196 //<rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
1198 AutoScroll(m_manufacturerListCtrl
, nIndex
);
1205 m_printerSelectionText
.SetWindowText(text
);
1210 // ------------------------------------------------------
1211 // MatchManufacturer
1213 // This function is responsible for finding a manufacturer
1214 // object from a string name. It does a CString::Find, which
1215 // is like strstr, so it doesn't have to do an exact match
1217 // If it can't find a match, NULL is returned
1218 // ------------------------------------------------------
1221 CThirdPage::MatchManufacturer( Manufacturers
& manufacturers
, const CString
& name
)
1223 Manufacturers::iterator iter
;
1225 for (iter
= manufacturers
.begin(); iter
!= manufacturers
.end(); iter
++)
1228 // we're going to convert all the manufacturer names to lower case,
1229 // so we match the name passed in.
1231 CString lower
= iter
->second
->name
;
1235 // now try and find the lowered string in the name passed in.
1237 if (name
.Find(lower
) != -1)
1239 return iter
->second
;
1246 // -------------------------------------------------------
1249 // This function is responsible for matching a model from
1250 // a name. It does a CString::Find(), which works like strstr,
1251 // so it doesn't rely on doing an exact string match.
1255 CThirdPage::MatchModel(Manufacturer
* manufacturer
, const CString
& name
)
1257 Models::iterator iter
;
1259 iter
= manufacturer
->models
.begin();
1261 for (iter
= manufacturer
->models
.begin(); iter
!= manufacturer
->models
.end(); iter
++)
1263 Model
* model
= *iter
;
1266 // convert the model name to lower case
1268 CString lowered
= model
->name
;
1269 lowered
.MakeLower();
1271 if (lowered
.Find( name
) != -1)
1277 // <rdar://problem/3841218>
1278 // try removing the first substring and search again
1281 if ( name
.Find(' ') != -1 )
1283 CString altered
= name
;
1284 altered
.Delete( 0, altered
.Find(' ') + 1 );
1286 if ( lowered
.Find( altered
) != -1 )
1296 // -------------------------------------------------------
1299 // This function will attempt to find a generic printer
1300 // driver for a printer that we weren't able to match
1304 CThirdPage::MatchGeneric( Manufacturers
& manufacturers
, Printer
* printer
, Service
* service
, Manufacturer
** manufacturer
, Model
** model
)
1309 DEBUG_UNUSED( printer
);
1313 Queue
* q
= service
->SelectedQueue();
1317 Manufacturers::iterator iter
= manufacturers
.find( kGenericManufacturer
);
1318 require_action_quiet( iter
!= manufacturers
.end(), exit
, ok
= FALSE
);
1320 *manufacturer
= iter
->second
;
1325 if ( m_genericPCL
&& ( pdl
.Find( kPDLPCLKey
) != -1 ) )
1327 *model
= m_genericPCL
;
1330 else if ( m_genericPostscript
&& ( pdl
.Find( kPDLPostscriptKey
) != -1 ) )
1332 *model
= m_genericPostscript
;
1341 // -----------------------------------------------------------
1344 // This function is responsible for doing initialization that
1345 // only occurs once during a run of the wizard
1348 OSStatus
CThirdPage::OnInitPage()
1352 OSStatus err
= kNoErr
;
1354 // Load printer icon
1355 check( m_printerImage
== NULL
);
1357 m_printerImage
= (CStatic
*) GetDlgItem( 1 ); // 1 == IDR_MANIFEST
1358 check( m_printerImage
);
1360 if ( m_printerImage
!= NULL
)
1362 m_printerImage
->SetIcon( LoadIcon( GetNonLocalizedResources(), MAKEINTRESOURCE( IDI_PRINTER
) ) );
1366 // The CTreeCtrl widget automatically sends a selection changed
1367 // message which initially we want to ignore, because the user
1368 // hasn't selected anything
1370 // this flag gets reset in the message handler. Every subsequent
1371 // message gets handled.
1375 // we have to make sure that we only do this once. Typically,
1376 // we would do this in something like OnInitDialog, but we don't
1377 // have this in Wizards, because the window is a PropertySheet.
1378 // We're considered fully initialized when we receive the first
1381 header
.LoadString(IDS_MANUFACTURER_HEADING
);
1382 m_manufacturerListCtrl
.InsertColumn(0, header
, LVCFMT_LEFT
, -1 );
1383 m_manufacturerSelected
= NULL
;
1385 header
.LoadString(IDS_MODEL_HEADING
);
1386 m_modelListCtrl
.InsertColumn(0, header
, LVCFMT_LEFT
, -1 );
1387 m_modelSelected
= NULL
;
1392 void CThirdPage::DoDataExchange(CDataExchange
* pDX
)
1394 CPropertyPage::DoDataExchange(pDX
);
1395 DDX_Control(pDX
, IDC_PRINTER_MANUFACTURER
, m_manufacturerListCtrl
);
1396 DDX_Control(pDX
, IDC_PRINTER_MODEL
, m_modelListCtrl
);
1397 DDX_Control(pDX
, IDC_PRINTER_NAME
, m_printerName
);
1398 DDX_Control(pDX
, IDC_DEFAULT_PRINTER
, m_defaultPrinterCtrl
);
1399 DDX_Control(pDX
, IDC_PRINTER_SELECTION_TEXT
, m_printerSelectionText
);
1403 // ----------------------------------------------------------
1406 // This function is called by MFC after the window has been
1411 CThirdPage::OnSetActive()
1413 CPrinterSetupWizardSheet
* psheet
;
1417 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1418 require_quiet( psheet
, exit
);
1420 psheet
->SetWizardButtons( PSWIZB_BACK
);
1422 printer
= psheet
->GetSelectedPrinter();
1423 require_quiet( printer
, exit
);
1425 service
= printer
->services
.front();
1426 require_quiet( service
, exit
);
1429 // call OnInitPage once
1434 m_initialized
= true;
1438 // <rdar://problem/4580061> mDNS: Printers added using Bonjour should be set as the default printer.
1440 if ( DefaultPrinterExists() )
1442 m_defaultPrinterCtrl
.SetCheck( BST_UNCHECKED
);
1443 printer
->deflt
= false;
1447 m_defaultPrinterCtrl
.SetCheck( BST_CHECKED
);
1448 printer
->deflt
= true;
1452 // update the UI with the printer name
1454 m_printerName
.SetWindowText(printer
->displayName
);
1457 // populate the list controls with the manufacturers and models
1460 PopulateUI( m_manufacturers
);
1463 // and try and match the printer
1466 if ( psheet
->GetLastPage() == psheet
->GetPage(0) )
1468 MatchPrinter( m_manufacturers
, printer
, service
, true );
1470 if ( ( m_manufacturerSelected
!= NULL
) && ( m_modelSelected
!= NULL
) )
1472 GetParent()->PostMessage(PSM_SETCURSEL
, 2 );
1477 SelectMatch(printer
, service
, m_manufacturerSelected
, m_modelSelected
);
1482 return CPropertyPage::OnSetActive();
1486 CThirdPage::OnKillActive()
1488 CPrinterSetupWizardSheet
* psheet
;
1490 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1491 require_quiet( psheet
, exit
);
1493 psheet
->SetLastPage(this);
1497 return CPropertyPage::OnKillActive();
1500 // -------------------------------------------------------
1503 // This function is called to populate the list of manufacturers
1506 CThirdPage::PopulateUI(Manufacturers
& manufacturers
)
1508 Manufacturers::iterator iter
;
1510 m_manufacturerListCtrl
.DeleteAllItems();
1512 for (iter
= manufacturers
.begin(); iter
!= manufacturers
.end(); iter
++)
1516 Manufacturer
* manufacturer
= iter
->second
;
1518 nIndex
= m_manufacturerListCtrl
.InsertItem(0, manufacturer
->name
);
1520 m_manufacturerListCtrl
.SetItemData(nIndex
, (DWORD_PTR
) manufacturer
);
1522 m_manufacturerListCtrl
.SetColumnWidth( 0, LVSCW_AUTOSIZE_USEHEADER
);
1528 BEGIN_MESSAGE_MAP(CThirdPage
, CPropertyPage
)
1529 ON_NOTIFY(LVN_ITEMCHANGED
, IDC_PRINTER_MANUFACTURER
, OnLvnItemchangedManufacturer
)
1530 ON_NOTIFY(LVN_ITEMCHANGED
, IDC_PRINTER_MODEL
, OnLvnItemchangedPrinterModel
)
1531 ON_BN_CLICKED(IDC_DEFAULT_PRINTER
, OnBnClickedDefaultPrinter
)
1532 ON_BN_CLICKED(IDC_HAVE_DISK
, OnBnClickedHaveDisk
)
1535 // CThirdPage message handlers
1536 void CThirdPage::OnLvnItemchangedManufacturer(NMHDR
*pNMHDR
, LRESULT
*pResult
)
1538 LPNMLISTVIEW pNMLV
= reinterpret_cast<LPNMLISTVIEW
>(pNMHDR
);
1540 POSITION p
= m_manufacturerListCtrl
.GetFirstSelectedItemPosition();
1541 int nSelected
= m_manufacturerListCtrl
.GetNextSelectedItem(p
);
1543 if (nSelected
!= -1)
1545 m_manufacturerSelected
= (Manufacturer
*) m_manufacturerListCtrl
.GetItemData(nSelected
);
1547 m_modelListCtrl
.SetRedraw(FALSE
);
1549 m_modelListCtrl
.DeleteAllItems();
1550 m_modelSelected
= NULL
;
1552 Models::iterator iter
;
1554 for (iter
= m_manufacturerSelected
->models
.begin(); iter
!= m_manufacturerSelected
->models
.end(); iter
++)
1556 Model
* model
= *iter
;
1558 int nItem
= m_modelListCtrl
.InsertItem( 0, model
->displayName
);
1560 m_modelListCtrl
.SetItemData(nItem
, (DWORD_PTR
) model
);
1562 m_modelListCtrl
.SetColumnWidth( 0, LVSCW_AUTOSIZE_USEHEADER
);
1565 m_modelListCtrl
.SetRedraw(TRUE
);
1571 void CThirdPage::OnLvnItemchangedPrinterModel(NMHDR
*pNMHDR
, LRESULT
*pResult
)
1573 LPNMLISTVIEW pNMLV
= reinterpret_cast<LPNMLISTVIEW
>(pNMHDR
);
1575 CPrinterSetupWizardSheet
* psheet
;
1579 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1580 require_quiet( psheet
, exit
);
1582 printer
= psheet
->GetSelectedPrinter();
1583 require_quiet( printer
, exit
);
1585 service
= printer
->services
.front();
1586 require_quiet( service
, exit
);
1588 check ( m_manufacturerSelected
);
1590 POSITION p
= m_modelListCtrl
.GetFirstSelectedItemPosition();
1591 int nSelected
= m_modelListCtrl
.GetNextSelectedItem(p
);
1593 if (nSelected
!= -1)
1595 m_modelSelected
= (Model
*) m_modelListCtrl
.GetItemData(nSelected
);
1597 CopyPrinterSettings( printer
, service
, m_manufacturerSelected
, m_modelSelected
);
1599 psheet
->SetWizardButtons(PSWIZB_BACK
|PSWIZB_NEXT
);
1603 psheet
->SetWizardButtons(PSWIZB_BACK
);
1611 void CThirdPage::OnBnClickedDefaultPrinter()
1613 CPrinterSetupWizardSheet
* psheet
;
1616 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1617 require_quiet( psheet
, exit
);
1619 printer
= psheet
->GetSelectedPrinter();
1620 require_quiet( printer
, exit
);
1622 printer
->deflt
= ( m_defaultPrinterCtrl
.GetCheck() == BST_CHECKED
) ? true : false;
1629 void CThirdPage::OnBnClickedHaveDisk()
1631 CPrinterSetupWizardSheet
* psheet
;
1634 Manufacturers manufacturers
;
1636 CFileDialog
dlg(TRUE
, NULL
, NULL
, OFN_HIDEREADONLY
|OFN_FILEMUSTEXIST
, L
"Setup Information (*.inf)|*.inf||", this);
1638 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1639 require_quiet( psheet
, exit
);
1641 printer
= psheet
->GetSelectedPrinter();
1642 require_quiet( printer
, exit
);
1644 service
= printer
->services
.front();
1645 require_quiet( service
, exit
);
1649 if ( dlg
.DoModal() == IDOK
)
1651 CString filename
= dlg
.GetPathName();
1653 LoadPrintDriverDefsFromFile( manufacturers
, filename
, true );
1657 if ( manufacturers
.size() > 0 )
1659 PopulateUI( manufacturers
);
1661 if ( MatchPrinter( manufacturers
, printer
, service
, false ) != kNoErr
)
1663 CString errorMessage
;
1664 CString errorCaption
;
1666 errorMessage
.LoadString( IDS_NO_MATCH_INF_FILE
);
1667 errorCaption
.LoadString( IDS_NO_MATCH_INF_FILE_CAPTION
);
1669 MessageBox( errorMessage
, errorCaption
, MB_OK
);
1676 CString errorMessage
;
1677 CString errorCaption
;
1679 errorMessage
.LoadString( IDS_BAD_INF_FILE
);
1680 errorCaption
.LoadString( IDS_BAD_INF_FILE_CAPTION
);
1682 MessageBox( errorMessage
, errorCaption
, MB_OK
);
1698 CThirdPage::Split( const CString
& string
, TCHAR ch
, CStringList
& components
)
1705 while ( ( n
= temp
.Find( ch
) ) != -1 )
1707 components
.AddTail( temp
.Left( n
) );
1708 temp
= temp
.Right( temp
.GetLength() - ( n
+ 1 ) );
1711 components
.AddTail( temp
);