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.41 2009/06/18 18:05:50 herscher
21 <rdar://problem/4694554> Eliminate the first screen of Printer Wizard and maybe combine others ("I'm Feeling Lucky")
23 Revision 1.40 2009/05/29 20:43:36 herscher
24 <rdar://problem/6928136> Printer Wizard doesn't work correctly in Windows 7 64 bit
26 Revision 1.39 2009/05/27 06:25:49 herscher
27 <rdar://problem/4176334> Need error dialog when selecting bad INF file
29 Revision 1.38 2009/05/27 04:59:57 herscher
30 <rdar://problem/4517393> COMPATIBILITY WITH HP CLJ4700
31 <rdar://problem/6142138> Compatibility with Samsung print driver files
33 Revision 1.37 2007/06/08 06:30:26 herscher
34 <rdar://problem/5257700> Fix uninitialized pointers when detecting generic PCL and PS drivers
36 Revision 1.36 2007/06/06 20:39:10 cheshire
37 <rdar://problem/5254377> Printer Setup Wizard started crashing in Bonjour104A8, after update to Visual Studio 2005
39 Revision 1.35 2007/06/06 20:08:01 cheshire
40 <rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
41 AutoScroll model list as well as manufacturer list
43 Revision 1.34 2007/06/06 19:53:48 cheshire
44 <rdar://problem/5187308> Move build train to Visual Studio 2005
46 Revision 1.33 2007/04/20 22:58:10 herscher
47 <rdar://problem/4826126> mDNS: Printer Wizard doesn't offer generic HP printers or generic PS support on Vista RC2
49 Revision 1.32 2007/04/13 23:42:20 herscher
50 <rdar://problem/4580061> mDNS: Printers added using Bonjour should be set as the default printer.
52 Revision 1.31 2007/04/13 21:38:46 herscher
53 <rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
55 Revision 1.30 2007/04/13 20:23:40 herscher
56 Fixed mistake in previous checkin that reverted license text for this file
58 Revision 1.29 2007/04/13 18:10:24 herscher
59 <rdar://problem/4496652> mDNS: Don't allow user to choose non-working driver
61 Revision 1.28 2006/08/14 23:24:09 cheshire
62 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
64 Revision 1.27 2005/10/05 21:41:45 herscher
65 <rdar://problem/4190104> Use "application/octet-stream" to determine if CUPS shared queue supports raw
67 Revision 1.26 2005/07/11 20:17:15 shersche
68 <rdar://problem/4124524> UI fixes associated with CUPS printer workaround fix.
70 Revision 1.25 2005/07/07 17:53:20 shersche
71 Fix problems associated with the CUPS printer workaround fix.
73 Revision 1.24 2005/06/30 18:02:54 shersche
74 <rdar://problem/4124524> Workaround for Mac OS X Printer Sharing bug
76 Revision 1.23 2005/04/18 02:33:47 shersche
77 <rdar://problem/4091216> Default printer option cannot be deselected
79 Revision 1.22 2005/04/13 17:46:22 shersche
80 <rdar://problem/4082122> Generic PCL not selected when printers advertise multiple text records
82 Revision 1.21 2005/03/30 02:09:55 shersche
83 Auto-resize the column width to account for differing fonts and font sizes
85 Revision 1.20 2005/03/05 02:27:45 shersche
86 <rdar://problem/4030388> Generic drivers don't do color
88 Revision 1.19 2005/02/23 02:08:51 shersche
89 <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".
91 Revision 1.18 2005/02/15 07:02:51 shersche
92 <rdar://problem/4003724> Display different UI text when generic printer drivers are selected
94 Revision 1.17 2005/02/08 21:45:06 shersche
95 <rdar://problem/3947490> Default to Generic PostScript or PCL if unable to match driver
97 Revision 1.16 2005/02/08 18:56:03 shersche
98 Fix generated IPP url so that it doesn't add "/printers" string
100 Revision 1.15 2005/02/01 01:44:07 shersche
101 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.
103 Revision 1.14 2005/01/25 08:55:54 shersche
104 <rdar://problem/3911084> Load icons at run-time from resource DLL
107 Revision 1.13 2005/01/06 08:15:45 shersche
108 Append queue name to end of LPR port name, correctly build port name when queue name is absent
110 Revision 1.12 2005/01/05 01:06:12 shersche
111 <rdar://problem/3841218> Strip the first substring off the product key if an initial match can't be found with the whole product key.
114 Revision 1.11 2004/12/29 18:53:38 shersche
115 <rdar://problem/3725106>
116 <rdar://problem/3737413> Added support for LPR and IPP protocols as well as support for obtaining multiple text records. Reorganized and simplified codebase.
117 Bug #: 3725106, 3737413
119 Revision 1.10 2004/10/11 22:55:34 shersche
120 <rdar://problem/3827624> Use the IP port number when deriving the printer port name.
123 Revision 1.9 2004/06/27 23:08:00 shersche
124 code cleanup, make sure EnumPrintDrivers returns non-zero value, ignore comments in inf files
126 Revision 1.8 2004/06/27 08:06:45 shersche
127 Parse [Strings] section of inf file
129 Revision 1.7 2004/06/26 04:00:05 shersche
130 fix warnings compiling in debug mode
131 Submitted by: herscher
133 Revision 1.6 2004/06/26 03:19:57 shersche
134 clean up warning messages
136 Submitted by: herscher
138 Revision 1.5 2004/06/25 05:06:02 shersche
139 Trim whitespace from key/value pairs when parsing inf files
140 Submitted by: herscher
142 Revision 1.4 2004/06/25 02:44:13 shersche
143 Tweaked code to handle Xerox Phaser printer identification
144 Submitted by: herscher
146 Revision 1.3 2004/06/25 02:27:58 shersche
147 Do a CListCtrl::FindItem() before calling CListCtrl::SetItemState().
148 Submitted by: herscher
150 Revision 1.2 2004/06/23 18:09:23 shersche
151 Normalize tag names when parsing inf files.
152 Submitted by: herscher
154 Revision 1.1 2004/06/18 04:36:58 rpantos
159 #include "PrinterSetupWizardApp.h"
160 #include "PrinterSetupWizardSheet.h"
161 #include "ThirdPage.h"
162 #include "StdioFileEx.h"
165 #include <winspool.h>
166 #include <setupapi.h>
168 // local variable is initialize but not referenced
169 #pragma warning(disable:4189)
172 // This is the printer description file that is shipped
173 // with Windows XP and below
175 #define kNTPrintFile L"inf\\ntprint.inf"
178 // Windows Vista ships with a set of prn*.inf files
180 #define kVistaPrintFiles L"inf\\prn*.inf"
183 // These are pre-defined names for Generic manufacturer and model
185 #define kGenericManufacturer L"Generic"
186 #define kGenericText L"Generic / Text Only"
187 #define kGenericPostscript L"Generic / Postscript"
188 #define kGenericPCL L"Generic / PCL"
189 #define kPDLPostscriptKey L"application/postscript"
190 #define kPDLPCLKey L"application/vnd.hp-pcl"
191 #define kGenericPSColorDriver L"HP Color LaserJet 4550 PS"
192 #define kGenericPSDriver L"HP LaserJet 4050 Series PS"
193 #define kGenericPCLColorDriver L"HP Color LaserJet 4550 PCL"
194 #define kGenericPCLDriver L"HP LaserJet 4050 Series PCL"
199 IMPLEMENT_DYNAMIC(CThirdPage
, CPropertyPage
)
200 CThirdPage::CThirdPage()
201 : CPropertyPage(CThirdPage::IDD
),
202 m_manufacturerSelected( NULL
),
203 m_modelSelected( NULL
),
204 m_genericPostscript( NULL
),
205 m_genericPCL( NULL
),
206 m_initialized(false),
207 m_printerImage( NULL
)
209 static const int bufferSize
= 32768;
210 TCHAR windowsDirectory
[bufferSize
];
212 WIN32_FIND_DATA findFileData
;
219 m_psp
.dwFlags
&= ~(PSP_HASHELP
);
220 m_psp
.dwFlags
|= PSP_DEFAULT
|PSP_USEHEADERTITLE
|PSP_USEHEADERSUBTITLE
;
222 m_psp
.pszHeaderTitle
= MAKEINTRESOURCE(IDS_INSTALL_TITLE
);
223 m_psp
.pszHeaderSubTitle
= MAKEINTRESOURCE(IDS_INSTALL_SUBTITLE
);
226 // load printers from ntprint.inf
228 ok
= GetWindowsDirectory( windowsDirectory
, bufferSize
);
229 err
= translate_errno( ok
, errno_compat(), kUnknownErr
);
230 require_noerr( err
, exit
);
233 // <rdar://problem/4826126>
235 // If there are no *prn.inf files, we'll assume that the information
238 prnFiles
.Format( L
"%s\\%s", windowsDirectory
, kVistaPrintFiles
);
239 findHandle
= FindFirstFile( prnFiles
, &findFileData
);
241 if ( findHandle
!= INVALID_HANDLE_VALUE
)
245 absolute
.Format( L
"%s\\inf\\%s", windowsDirectory
, findFileData
.cFileName
);
246 err
= LoadPrintDriverDefsFromFile( m_manufacturers
, absolute
, false );
247 require_noerr( err
, exit
);
249 while ( FindNextFile( findHandle
, &findFileData
) )
251 absolute
.Format( L
"%s\\inf\\%s", windowsDirectory
, findFileData
.cFileName
);
252 err
= LoadPrintDriverDefsFromFile( m_manufacturers
, absolute
, false );
253 require_noerr( err
, exit
);
256 FindClose( findHandle
);
260 ntPrint
.Format(L
"%s\\%s", windowsDirectory
, kNTPrintFile
);
261 err
= LoadPrintDriverDefsFromFile( m_manufacturers
, ntPrint
, false );
262 require_noerr(err
, exit
);
266 // load printer drivers that have been installed on this machine
268 err
= LoadPrintDriverDefs( m_manufacturers
);
269 require_noerr(err
, exit
);
272 // load our own special generic printer defs
274 err
= LoadGenericPrintDriverDefs( m_manufacturers
);
275 require_noerr( err
, exit
);
282 CThirdPage::~CThirdPage()
285 // clean up all the printer manufacturers
287 while (m_manufacturers
.size())
289 Manufacturers::iterator iter
= m_manufacturers
.begin();
291 while (iter
->second
->models
.size())
293 Models::iterator it
= iter
->second
->models
.begin();
299 iter
->second
->models
.erase(it
);
304 m_manufacturers
.erase(iter
);
308 // ----------------------------------------------------
311 // SelectMatch will do all the UI work associated with
312 // selected a manufacturer and model of printer. It also
313 // makes sure the printer object is update with the
316 // ----------------------------------------------------
318 CThirdPage::SelectMatch(Printer
* printer
, Service
* service
, Manufacturer
* manufacturer
, Model
* model
)
323 check( printer
!= NULL
);
324 check( manufacturer
!= NULL
);
325 check( model
!= NULL
);
328 // select the manufacturer
330 info
.flags
= LVFI_STRING
;
331 info
.psz
= manufacturer
->name
;
333 nIndex
= m_manufacturerListCtrl
.FindItem(&info
);
337 m_manufacturerListCtrl
.SetItemState(nIndex
, LVIS_SELECTED
, LVIS_SELECTED
);
339 //<rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
341 AutoScroll(m_manufacturerListCtrl
, nIndex
);
347 info
.flags
= LVFI_STRING
;
348 info
.psz
= model
->displayName
;
350 nIndex
= m_modelListCtrl
.FindItem(&info
);
354 m_modelListCtrl
.SetItemState(nIndex
, LVIS_SELECTED
, LVIS_SELECTED
);
355 AutoScroll( m_modelListCtrl
, nIndex
);
357 m_modelListCtrl
.SetFocus();
360 CopyPrinterSettings( printer
, service
, manufacturer
, model
);
364 CThirdPage::SelectMatch(Manufacturers
& manufacturers
, Printer
* printer
, Service
* service
, Manufacturer
* manufacturer
, Model
* model
)
366 PopulateUI( manufacturers
);
368 SelectMatch( printer
, service
, manufacturer
, model
);
371 // --------------------------------------------------------
372 // CopyPrinterSettings
374 // This function makes sure that the printer object has the
375 // latest settings from the manufacturer and model objects
376 // --------------------------------------------------------
379 CThirdPage::CopyPrinterSettings( Printer
* printer
, Service
* service
, Manufacturer
* manufacturer
, Model
* model
)
381 printer
->manufacturer
= manufacturer
->name
;
382 printer
->displayModelName
= model
->displayName
;
383 printer
->modelName
= model
->name
;
384 printer
->driverInstalled
= model
->driverInstalled
;
385 printer
->infFileName
= model
->infFileName
;
387 if ( service
->type
== kPDLServiceType
)
389 printer
->portName
.Format(L
"IP_%s.%d", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
);
390 service
->protocol
= L
"Raw";
392 else if ( service
->type
== kLPRServiceType
)
394 Queue
* q
= service
->queues
.front();
397 if ( q
->name
.GetLength() > 0 )
399 printer
->portName
.Format(L
"LPR_%s.%d.%s", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
, static_cast<LPCTSTR
>(q
->name
) );
403 printer
->portName
.Format(L
"LPR_%s.%d", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
);
406 service
->protocol
= L
"LPR";
408 else if ( service
->type
== kIPPServiceType
)
410 Queue
* q
= service
->queues
.front();
413 if ( q
->name
.GetLength() > 0 )
415 printer
->portName
.Format(L
"http://%s:%d/%s", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
, static_cast<LPCTSTR
>(q
->name
) );
419 printer
->portName
.Format(L
"http://%s:%d/", static_cast<LPCTSTR
>(service
->hostname
), service
->portNumber
);
422 service
->protocol
= L
"IPP";
426 // --------------------------------------------------------
427 // DefaultPrinterExists
429 // Checks to see if a default printer has been configured
431 // --------------------------------------------------------
433 CThirdPage::DefaultPrinterExists()
435 CPrintDialog
dlg(FALSE
);
437 dlg
.m_pd
.Flags
|= PD_RETURNDEFAULT
;
439 return dlg
.GetDefaults();
442 // --------------------------------------------------------
445 // Ensure selected item is in middle of list
446 // --------------------------------------------------------
448 CThirdPage::AutoScroll( CListCtrl
& list
, int nIndex
)
451 //<rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
457 list
.EnsureVisible( nIndex
, FALSE
);
459 top
= list
.GetTopIndex();
460 count
= list
.GetCountPerPage();
462 if ( ( nIndex
== top
) || ( ( nIndex
+ 1 ) == ( top
+ count
) ) )
467 rows
= ( count
/ 2 );
471 list
.GetItemRect(0, rect
, LVIR_BOUNDS
);
472 list
.Scroll( CPoint( 0, rows
* rect
.Height() * -1 ) );
476 list
.GetItemRect(0, rect
, LVIR_BOUNDS
);
477 list
.Scroll( CPoint( 0, rows
* rect
.Height() ) );
482 // ------------------------------------------------------
483 // LoadPrintDriverDefsFromFile
485 // The only potentially opaque thing about this function is the
486 // checkForDuplicateModels flag. The problem here is that ntprint.inf
487 // doesn't contain duplicate models, and it has hundreds of models
488 // listed. You wouldn't check for duplicates there. But oftentimes,
489 // loading different windows print driver files contain multiple
490 // entries for the same printer. You don't want the UI to display
491 // the same printer multiple times, so in that case, you would ask
492 // this function to check for multiple models.
495 CThirdPage::LoadPrintDriverDefsFromFile(Manufacturers
& manufacturers
, const CString
& filename
, bool checkForDuplicateModels
)
497 HINF handle
= INVALID_HANDLE_VALUE
;
498 const TCHAR
* section
= TEXT( "Manufacturer" );
502 INFCONTEXT manufacturerContext
;
506 // Make sure we can open the file
507 handle
= SetupOpenInfFile( filename
, NULL
, INF_STYLE_WIN4
, NULL
);
508 translate_errno( handle
!= INVALID_HANDLE_VALUE
, GetLastError(), kUnknownErr
);
509 require_noerr( err
, exit
);
511 // Make sure it's a printer file
512 ok
= SetupGetLineText( NULL
, handle
, TEXT( "Version" ), TEXT( "Class" ), line
, sizeof( line
), NULL
);
513 translate_errno( ok
, GetLastError(), kUnknownErr
);
514 require_noerr( err
, exit
);
516 require_action( klass
== TEXT( "Printer" ), exit
, err
= kUnknownErr
);
518 sectionCount
= SetupGetLineCount( handle
, section
);
519 translate_errno( sectionCount
!= -1, GetLastError(), kUnknownErr
);
520 require_noerr( err
, exit
);
522 memset( &manufacturerContext
, 0, sizeof( manufacturerContext
) );
524 for ( LONG i
= 0; i
< sectionCount
; i
++ )
526 Manufacturers::iterator iter
;
527 Manufacturer
* manufacturer
;
528 CString manufacturerName
;
530 CStringList modelSectionNameDecl
;
531 CString modelSectionName
;
532 CString baseModelName
;
534 INFCONTEXT modelContext
;
540 ok
= SetupFindFirstLine( handle
, section
, NULL
, &manufacturerContext
);
541 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
542 require_noerr( err
, exit
);
546 ok
= SetupFindNextLine( &manufacturerContext
, &manufacturerContext
);
547 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
548 require_noerr( err
, exit
);
551 ok
= SetupGetStringField( &manufacturerContext
, 0, line
, sizeof( line
), NULL
);
552 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
553 require_noerr( err
, exit
);
554 manufacturerName
= line
;
556 ok
= SetupGetLineText( &manufacturerContext
, handle
, NULL
, NULL
, line
, sizeof( line
), NULL
);
557 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
558 require_noerr( err
, exit
);
560 // Try to find some model section name that has entries. Explanation of int file structure
563 // <http://msdn.microsoft.com/en-us/library/ms794359.aspx>
564 Split( line
, ',', modelSectionNameDecl
);
566 p
= modelSectionNameDecl
.GetHeadPosition();
567 modelSectionName
= modelSectionNameDecl
.GetNext( p
);
568 modelCount
= SetupGetLineCount( handle
, modelSectionName
);
569 baseModelName
= modelSectionName
;
571 while ( modelCount
<= 0 && p
)
573 CString targetOSVersion
;
575 targetOSVersion
= modelSectionNameDecl
.GetNext( p
);
576 modelSectionName
= baseModelName
+ TEXT( "." ) + targetOSVersion
;
577 modelCount
= SetupGetLineCount( handle
, modelSectionName
);
580 if ( modelCount
> 0 )
582 manufacturerName
= NormalizeManufacturerName( manufacturerName
);
584 iter
= manufacturers
.find( manufacturerName
);
586 if ( iter
!= manufacturers
.end() )
588 manufacturer
= iter
->second
;
589 require_action( manufacturer
, exit
, err
= kUnknownErr
);
595 manufacturer
= new Manufacturer
;
602 require_action( manufacturer
, exit
, err
= kNoMemoryErr
);
604 manufacturer
->name
= manufacturerName
;
605 manufacturers
[ manufacturerName
] = manufacturer
;
608 memset( &modelContext
, 0, sizeof( modelContext
) );
610 for ( LONG j
= 0; j
< modelCount
; j
++ )
617 ok
= SetupFindFirstLine( handle
, modelSectionName
, NULL
, &modelContext
);
618 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
619 require_noerr( err
, exit
);
623 SetupFindNextLine( &modelContext
, &modelContext
);
624 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
625 require_noerr( err
, exit
);
628 ok
= SetupGetStringField( &modelContext
, 0, line
, sizeof( line
), NULL
);
629 err
= translate_errno( ok
, GetLastError(), kUnknownErr
);
630 require_noerr( err
, exit
);
634 if (checkForDuplicateModels
== true)
636 if ( MatchModel( manufacturer
, ConvertToModelName( modelName
) ) != NULL
)
643 // Stock Vista printer inf files embed guids in the model
644 // declarations for Epson printers. Let's ignore those.
646 if ( modelName
.Find( TEXT( "{" ), 0 ) != -1 )
660 require_action( model
, exit
, err
= kNoMemoryErr
);
662 model
->infFileName
= filename
;
663 model
->displayName
= modelName
;
664 model
->name
= modelName
;
665 model
->driverInstalled
= false;
667 manufacturer
->models
.push_back(model
);
674 if ( handle
!= INVALID_HANDLE_VALUE
)
676 SetupCloseInfFile( handle
);
684 // -------------------------------------------------------
685 // LoadPrintDriverDefs
687 // This function is responsible for loading the print driver
688 // definitions of all print drivers that have been installed
690 // -------------------------------------------------------
692 CThirdPage::LoadPrintDriverDefs( Manufacturers
& manufacturers
)
694 BYTE
* buffer
= NULL
;
695 DWORD bytesReceived
= 0;
696 DWORD numPrinters
= 0;
701 // like a lot of win32 calls, we call this first to get the
702 // size of the buffer we need.
704 EnumPrinterDrivers(NULL
, L
"all", 6, NULL
, 0, &bytesReceived
, &numPrinters
);
706 if (bytesReceived
> 0)
710 buffer
= new BYTE
[bytesReceived
];
717 require_action( buffer
, exit
, err
= kNoMemoryErr
);
720 // this call gets the real info
722 ok
= EnumPrinterDrivers(NULL
, L
"all", 6, buffer
, bytesReceived
, &bytesReceived
, &numPrinters
);
723 err
= translate_errno( ok
, errno_compat(), kUnknownErr
);
724 require_noerr( err
, exit
);
726 DRIVER_INFO_6
* info
= (DRIVER_INFO_6
*) buffer
;
728 for (DWORD i
= 0; i
< numPrinters
; i
++)
730 Manufacturer
* manufacturer
;
735 // skip over anything that doesn't have a manufacturer field. This
736 // fixes a bug that I noticed that occurred after I installed
737 // ProComm. This program add a print driver with no manufacturer
738 // that screwed up this wizard.
740 if (info
[i
].pszMfgName
== NULL
)
746 // look for manufacturer
748 Manufacturers::iterator iter
;
753 name
= NormalizeManufacturerName( info
[i
].pszMfgName
);
755 iter
= manufacturers
.find(name
);
757 if (iter
!= manufacturers
.end())
759 manufacturer
= iter
->second
;
765 manufacturer
= new Manufacturer
;
772 require_action( manufacturer
, exit
, err
= kNoMemoryErr
);
774 manufacturer
->name
= name
;
776 manufacturers
[name
] = manufacturer
;
780 // now look to see if we have already seen this guy. this could
781 // happen if we have already installed printers that are described
782 // in ntprint.inf. the extant drivers will show up in EnumPrinterDrivers
783 // but we have already loaded their info
786 if ( MatchModel( manufacturer
, ConvertToModelName( info
[i
].pName
) ) == NULL
)
797 require_action( model
, exit
, err
= kNoMemoryErr
);
799 model
->displayName
= info
[i
].pName
;
800 model
->name
= info
[i
].pName
;
801 model
->driverInstalled
= true;
803 manufacturer
->models
.push_back(model
);
818 // -------------------------------------------------------
819 // LoadGenericPrintDriverDefs
821 // This function is responsible for loading polymorphic
822 // generic print drivers defs. The UI will read
823 // something like "Generic / Postscript" and we can map
824 // that to any print driver we want.
825 // -------------------------------------------------------
827 CThirdPage::LoadGenericPrintDriverDefs( Manufacturers
& manufacturers
)
829 Manufacturer
* manufacturer
;
831 Manufacturers::iterator iter
;
832 CString psDriverName
;
833 CString pclDriverName
;
836 // <rdar://problem/4030388> Generic drivers don't do color
838 // First try and find our generic driver names
840 iter
= m_manufacturers
.find(L
"HP");
841 require_action( iter
!= m_manufacturers
.end(), exit
, err
= kUnknownErr
);
842 manufacturer
= iter
->second
;
844 // Look for Postscript
846 model
= manufacturer
->find( kGenericPSColorDriver
);
850 model
= manufacturer
->find( kGenericPSDriver
);
855 psDriverName
= model
->name
;
860 model
= manufacturer
->find( kGenericPCLColorDriver
);
864 model
= manufacturer
->find( kGenericPCLDriver
);
869 pclDriverName
= model
->name
;
872 // If we found either a generic PS driver, or a generic PCL driver,
873 // then add them to the list
875 if ( psDriverName
.GetLength() || pclDriverName
.GetLength() )
877 // Try and find generic manufacturer if there is one
879 iter
= manufacturers
.find(L
"Generic");
881 if (iter
!= manufacturers
.end())
883 manufacturer
= iter
->second
;
889 manufacturer
= new Manufacturer
;
896 require_action( manufacturer
, exit
, err
= kNoMemoryErr
);
898 manufacturer
->name
= "Generic";
899 manufacturers
[manufacturer
->name
] = manufacturer
;
902 if ( psDriverName
.GetLength() > 0 )
906 m_genericPostscript
= new Model
;
910 m_genericPostscript
= NULL
;
913 require_action( m_genericPostscript
, exit
, err
= kNoMemoryErr
);
915 m_genericPostscript
->displayName
= kGenericPostscript
;
916 m_genericPostscript
->name
= psDriverName
;
917 m_genericPostscript
->driverInstalled
= false;
919 manufacturer
->models
.push_back( m_genericPostscript
);
922 if ( pclDriverName
.GetLength() > 0 )
926 m_genericPCL
= new Model
;
933 require_action( m_genericPCL
, exit
, err
= kNoMemoryErr
);
935 m_genericPCL
->displayName
= kGenericPCL
;
936 m_genericPCL
->name
= pclDriverName
;
937 m_genericPCL
->driverInstalled
= false;
939 manufacturer
->models
.push_back( m_genericPCL
);
948 // ------------------------------------------------------
949 // ConvertToManufacturerName
951 // This function is responsible for tweaking the
952 // name so that subsequent string operations won't fail because
953 // of capitalizations/different names for the same manufacturer
954 // (i.e. Hewlett-Packard/HP/Hewlett Packard)
957 CThirdPage::ConvertToManufacturerName( const CString
& name
)
960 // first we're going to convert all the characters to lower
963 CString lower
= name
;
967 // now we're going to check to see if the string says "hewlett-packard",
968 // because sometimes they refer to themselves as "hewlett-packard", and
969 // sometimes they refer to themselves as "hp".
971 if ( lower
== L
"hewlett-packard")
977 // tweak for Xerox Phaser, which doesn't announce itself
980 else if ( lower
.Find( L
"phaser", 0 ) != -1 )
988 // ------------------------------------------------------
989 // ConvertToModelName
991 // This function is responsible for ensuring that subsequent
992 // string operations don't fail because of differing capitalization
993 // schemes and the like
994 // ------------------------------------------------------
997 CThirdPage::ConvertToModelName( const CString
& name
)
1000 // convert it to lowercase
1002 CString lower
= name
;
1008 // ------------------------------------------------------
1009 // NormalizeManufacturerName
1011 // This function is responsible for tweaking the manufacturer
1012 // name so that there are no aliases for vendors
1015 CThirdPage::NormalizeManufacturerName( const CString
& name
)
1017 CString normalized
= name
;
1020 // now we're going to check to see if the string says "hewlett-packard",
1021 // because sometimes they refer to themselves as "hewlett-packard", and
1022 // sometimes they refer to themselves as "hp".
1024 if ( normalized
== L
"Hewlett-Packard")
1032 // -------------------------------------------------------
1035 // This function is responsible for matching a printer
1036 // to a list of manufacturers and models. It calls
1037 // MatchManufacturer and MatchModel in turn.
1040 OSStatus
CThirdPage::MatchPrinter(Manufacturers
& manufacturers
, Printer
* printer
, Service
* service
, bool useCUPSWorkaround
)
1042 CString normalizedProductName
;
1043 Manufacturer
* manufacturer
= NULL
;
1044 Manufacturer
* genericManufacturer
= NULL
;
1045 Model
* model
= NULL
;
1046 Model
* genericModel
= NULL
;
1049 OSStatus err
= kNoErr
;
1054 Queue
* q
= service
->SelectedQueue();
1059 // first look to see if we have a usb_MFG descriptor
1061 if ( q
->usb_MFG
.GetLength() > 0)
1063 manufacturer
= MatchManufacturer( manufacturers
, ConvertToManufacturerName ( q
->usb_MFG
) );
1066 if ( manufacturer
== NULL
)
1068 q
->product
.Remove('(');
1069 q
->product
.Remove(')');
1071 manufacturer
= MatchManufacturer( manufacturers
, ConvertToManufacturerName ( q
->product
) );
1075 // if we found the manufacturer, then start looking for the model
1077 if ( manufacturer
!= NULL
)
1079 if ( q
->usb_MDL
.GetLength() > 0 )
1081 model
= MatchModel ( manufacturer
, ConvertToModelName ( q
->usb_MDL
) );
1084 if ( ( model
== NULL
) && ( q
->product
.GetLength() > 0 ) )
1086 q
->product
.Remove('(');
1087 q
->product
.Remove(')');
1089 model
= MatchModel ( manufacturer
, ConvertToModelName ( q
->product
) );
1092 if ( model
!= NULL
)
1094 // <rdar://problem/4124524> Offer Generic printers if printer advertises Postscript or PCL. Workaround
1095 // bug in OS X CUPS printer sharing by selecting Generic driver instead of matched printer.
1097 bool hasGenericDriver
= false;
1099 if ( MatchGeneric( manufacturers
, printer
, service
, &genericManufacturer
, &genericModel
) )
1101 hasGenericDriver
= true;
1104 // <rdar://problem/4190104> Use "application/octet-stream" to determine if CUPS
1105 // shared queue supports raw
1107 if ( q
->pdl
.Find( L
"application/octet-stream" ) != -1 )
1109 useCUPSWorkaround
= false;
1112 if ( useCUPSWorkaround
&& printer
->isSharedFromOSX
&& hasGenericDriver
)
1115 // <rdar://problem/4496652> mDNS: Don't allow user to choose non-working driver
1117 Manufacturers genericManufacturers
;
1119 LoadGenericPrintDriverDefs( genericManufacturers
);
1121 SelectMatch( genericManufacturers
, printer
, service
, genericManufacturer
, genericModel
);
1125 SelectMatch(manufacturers
, printer
, service
, manufacturer
, model
);
1133 // display a message to the user based on whether we could match
1138 text
.LoadString(IDS_PRINTER_MATCH_GOOD
);
1140 else if ( MatchGeneric( manufacturers
, printer
, service
, &genericManufacturer
, &genericModel
) )
1142 if ( printer
->isSharedFromOSX
)
1145 // <rdar://problem/4496652> mDNS: Don't allow user to choose non-working driver
1147 Manufacturers genericManufacturers
;
1149 LoadGenericPrintDriverDefs( genericManufacturers
);
1151 SelectMatch( genericManufacturers
, printer
, service
, genericManufacturer
, genericModel
);
1153 text
.LoadString(IDS_PRINTER_MATCH_GOOD
);
1157 SelectMatch( manufacturers
, printer
, service
, genericManufacturer
, genericModel
);
1158 text
.LoadString(IDS_PRINTER_MATCH_MAYBE
);
1163 text
.LoadString(IDS_PRINTER_MATCH_BAD
);
1166 // if there was any crud in this list from before, get rid of it now
1168 m_modelListCtrl
.DeleteAllItems();
1171 // select the manufacturer if we found one
1173 if (manufacturer
!= NULL
)
1179 // select the manufacturer
1181 info
.flags
= LVFI_STRING
;
1182 info
.psz
= manufacturer
->name
;
1184 nIndex
= m_manufacturerListCtrl
.FindItem(&info
);
1188 m_manufacturerListCtrl
.SetItemState(nIndex
, LVIS_SELECTED
, LVIS_SELECTED
);
1191 //<rdar://problem/4528853> mDNS: When auto-highlighting items in lists, scroll list so highlighted item is in the middle
1193 AutoScroll(m_manufacturerListCtrl
, nIndex
);
1198 m_printerSelectionText
.SetWindowText(text
);
1203 // ------------------------------------------------------
1204 // MatchManufacturer
1206 // This function is responsible for finding a manufacturer
1207 // object from a string name. It does a CString::Find, which
1208 // is like strstr, so it doesn't have to do an exact match
1210 // If it can't find a match, NULL is returned
1211 // ------------------------------------------------------
1214 CThirdPage::MatchManufacturer( Manufacturers
& manufacturers
, const CString
& name
)
1216 Manufacturers::iterator iter
;
1218 for (iter
= manufacturers
.begin(); iter
!= manufacturers
.end(); iter
++)
1221 // we're going to convert all the manufacturer names to lower case,
1222 // so we match the name passed in.
1224 CString lower
= iter
->second
->name
;
1228 // now try and find the lowered string in the name passed in.
1230 if (name
.Find(lower
) != -1)
1232 return iter
->second
;
1239 // -------------------------------------------------------
1242 // This function is responsible for matching a model from
1243 // a name. It does a CString::Find(), which works like strstr,
1244 // so it doesn't rely on doing an exact string match.
1248 CThirdPage::MatchModel(Manufacturer
* manufacturer
, const CString
& name
)
1250 Models::iterator iter
;
1252 iter
= manufacturer
->models
.begin();
1254 for (iter
= manufacturer
->models
.begin(); iter
!= manufacturer
->models
.end(); iter
++)
1256 Model
* model
= *iter
;
1259 // convert the model name to lower case
1261 CString lowered
= model
->name
;
1262 lowered
.MakeLower();
1264 if (lowered
.Find( name
) != -1)
1270 // <rdar://problem/3841218>
1271 // try removing the first substring and search again
1274 if ( name
.Find(' ') != -1 )
1276 CString altered
= name
;
1277 altered
.Delete( 0, altered
.Find(' ') + 1 );
1279 if ( lowered
.Find( altered
) != -1 )
1289 // -------------------------------------------------------
1292 // This function will attempt to find a generic printer
1293 // driver for a printer that we weren't able to match
1297 CThirdPage::MatchGeneric( Manufacturers
& manufacturers
, Printer
* printer
, Service
* service
, Manufacturer
** manufacturer
, Model
** model
)
1302 DEBUG_UNUSED( printer
);
1306 Queue
* q
= service
->SelectedQueue();
1310 Manufacturers::iterator iter
= manufacturers
.find( kGenericManufacturer
);
1311 require_action_quiet( iter
!= manufacturers
.end(), exit
, ok
= FALSE
);
1313 *manufacturer
= iter
->second
;
1318 if ( m_genericPCL
&& ( pdl
.Find( kPDLPCLKey
) != -1 ) )
1320 *model
= m_genericPCL
;
1323 else if ( m_genericPostscript
&& ( pdl
.Find( kPDLPostscriptKey
) != -1 ) )
1325 *model
= m_genericPostscript
;
1334 // -----------------------------------------------------------
1337 // This function is responsible for doing initialization that
1338 // only occurs once during a run of the wizard
1341 OSStatus
CThirdPage::OnInitPage()
1345 OSStatus err
= kNoErr
;
1347 // Load printer icon
1348 check( m_printerImage
== NULL
);
1350 m_printerImage
= (CStatic
*) GetDlgItem( 1 ); // 1 == IDR_MANIFEST
1351 check( m_printerImage
);
1353 if ( m_printerImage
!= NULL
)
1355 m_printerImage
->SetIcon( LoadIcon( GetNonLocalizedResources(), MAKEINTRESOURCE( IDI_PRINTER
) ) );
1359 // The CTreeCtrl widget automatically sends a selection changed
1360 // message which initially we want to ignore, because the user
1361 // hasn't selected anything
1363 // this flag gets reset in the message handler. Every subsequent
1364 // message gets handled.
1368 // we have to make sure that we only do this once. Typically,
1369 // we would do this in something like OnInitDialog, but we don't
1370 // have this in Wizards, because the window is a PropertySheet.
1371 // We're considered fully initialized when we receive the first
1374 header
.LoadString(IDS_MANUFACTURER_HEADING
);
1375 m_manufacturerListCtrl
.InsertColumn(0, header
, LVCFMT_LEFT
, -1 );
1376 m_manufacturerSelected
= NULL
;
1378 header
.LoadString(IDS_MODEL_HEADING
);
1379 m_modelListCtrl
.InsertColumn(0, header
, LVCFMT_LEFT
, -1 );
1380 m_modelSelected
= NULL
;
1385 void CThirdPage::DoDataExchange(CDataExchange
* pDX
)
1387 CPropertyPage::DoDataExchange(pDX
);
1388 DDX_Control(pDX
, IDC_PRINTER_MANUFACTURER
, m_manufacturerListCtrl
);
1389 DDX_Control(pDX
, IDC_PRINTER_MODEL
, m_modelListCtrl
);
1390 DDX_Control(pDX
, IDC_PRINTER_NAME
, m_printerName
);
1391 DDX_Control(pDX
, IDC_DEFAULT_PRINTER
, m_defaultPrinterCtrl
);
1392 DDX_Control(pDX
, IDC_PRINTER_SELECTION_TEXT
, m_printerSelectionText
);
1396 // ----------------------------------------------------------
1399 // This function is called by MFC after the window has been
1404 CThirdPage::OnSetActive()
1406 CPrinterSetupWizardSheet
* psheet
;
1410 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1411 require_quiet( psheet
, exit
);
1413 psheet
->SetWizardButtons( PSWIZB_BACK
);
1415 printer
= psheet
->GetSelectedPrinter();
1416 require_quiet( printer
, exit
);
1418 service
= printer
->services
.front();
1419 require_quiet( service
, exit
);
1422 // call OnInitPage once
1427 m_initialized
= true;
1431 // <rdar://problem/4580061> mDNS: Printers added using Bonjour should be set as the default printer.
1433 if ( DefaultPrinterExists() )
1435 m_defaultPrinterCtrl
.SetCheck( BST_UNCHECKED
);
1436 printer
->deflt
= false;
1440 m_defaultPrinterCtrl
.SetCheck( BST_CHECKED
);
1441 printer
->deflt
= true;
1445 // update the UI with the printer name
1447 m_printerName
.SetWindowText(printer
->displayName
);
1450 // populate the list controls with the manufacturers and models
1453 PopulateUI( m_manufacturers
);
1456 // and try and match the printer
1459 if ( psheet
->GetLastPage() == psheet
->GetPage(0) )
1461 MatchPrinter( m_manufacturers
, printer
, service
, true );
1463 if ( ( m_manufacturerSelected
!= NULL
) && ( m_modelSelected
!= NULL
) )
1465 GetParent()->PostMessage(PSM_SETCURSEL
, 2 );
1470 SelectMatch(printer
, service
, m_manufacturerSelected
, m_modelSelected
);
1475 return CPropertyPage::OnSetActive();
1479 CThirdPage::OnKillActive()
1481 CPrinterSetupWizardSheet
* psheet
;
1483 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1484 require_quiet( psheet
, exit
);
1486 psheet
->SetLastPage(this);
1490 return CPropertyPage::OnKillActive();
1493 // -------------------------------------------------------
1496 // This function is called to populate the list of manufacturers
1499 CThirdPage::PopulateUI(Manufacturers
& manufacturers
)
1501 Manufacturers::iterator iter
;
1503 m_manufacturerListCtrl
.DeleteAllItems();
1505 for (iter
= manufacturers
.begin(); iter
!= manufacturers
.end(); iter
++)
1509 Manufacturer
* manufacturer
= iter
->second
;
1511 nIndex
= m_manufacturerListCtrl
.InsertItem(0, manufacturer
->name
);
1513 m_manufacturerListCtrl
.SetItemData(nIndex
, (DWORD_PTR
) manufacturer
);
1515 m_manufacturerListCtrl
.SetColumnWidth( 0, LVSCW_AUTOSIZE_USEHEADER
);
1521 BEGIN_MESSAGE_MAP(CThirdPage
, CPropertyPage
)
1522 ON_NOTIFY(LVN_ITEMCHANGED
, IDC_PRINTER_MANUFACTURER
, OnLvnItemchangedManufacturer
)
1523 ON_NOTIFY(LVN_ITEMCHANGED
, IDC_PRINTER_MODEL
, OnLvnItemchangedPrinterModel
)
1524 ON_BN_CLICKED(IDC_DEFAULT_PRINTER
, OnBnClickedDefaultPrinter
)
1525 ON_BN_CLICKED(IDC_HAVE_DISK
, OnBnClickedHaveDisk
)
1528 // CThirdPage message handlers
1529 void CThirdPage::OnLvnItemchangedManufacturer(NMHDR
*pNMHDR
, LRESULT
*pResult
)
1531 LPNMLISTVIEW pNMLV
= reinterpret_cast<LPNMLISTVIEW
>(pNMHDR
);
1533 POSITION p
= m_manufacturerListCtrl
.GetFirstSelectedItemPosition();
1534 int nSelected
= m_manufacturerListCtrl
.GetNextSelectedItem(p
);
1536 if (nSelected
!= -1)
1538 m_manufacturerSelected
= (Manufacturer
*) m_manufacturerListCtrl
.GetItemData(nSelected
);
1540 m_modelListCtrl
.SetRedraw(FALSE
);
1542 m_modelListCtrl
.DeleteAllItems();
1543 m_modelSelected
= NULL
;
1545 Models::iterator iter
;
1547 for (iter
= m_manufacturerSelected
->models
.begin(); iter
!= m_manufacturerSelected
->models
.end(); iter
++)
1549 Model
* model
= *iter
;
1551 int nItem
= m_modelListCtrl
.InsertItem( 0, model
->displayName
);
1553 m_modelListCtrl
.SetItemData(nItem
, (DWORD_PTR
) model
);
1555 m_modelListCtrl
.SetColumnWidth( 0, LVSCW_AUTOSIZE_USEHEADER
);
1558 m_modelListCtrl
.SetRedraw(TRUE
);
1564 void CThirdPage::OnLvnItemchangedPrinterModel(NMHDR
*pNMHDR
, LRESULT
*pResult
)
1566 LPNMLISTVIEW pNMLV
= reinterpret_cast<LPNMLISTVIEW
>(pNMHDR
);
1568 CPrinterSetupWizardSheet
* psheet
;
1572 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1573 require_quiet( psheet
, exit
);
1575 printer
= psheet
->GetSelectedPrinter();
1576 require_quiet( printer
, exit
);
1578 service
= printer
->services
.front();
1579 require_quiet( service
, exit
);
1581 check ( m_manufacturerSelected
);
1583 POSITION p
= m_modelListCtrl
.GetFirstSelectedItemPosition();
1584 int nSelected
= m_modelListCtrl
.GetNextSelectedItem(p
);
1586 if (nSelected
!= -1)
1588 m_modelSelected
= (Model
*) m_modelListCtrl
.GetItemData(nSelected
);
1590 CopyPrinterSettings( printer
, service
, m_manufacturerSelected
, m_modelSelected
);
1592 psheet
->SetWizardButtons(PSWIZB_BACK
|PSWIZB_NEXT
);
1596 psheet
->SetWizardButtons(PSWIZB_BACK
);
1604 void CThirdPage::OnBnClickedDefaultPrinter()
1606 CPrinterSetupWizardSheet
* psheet
;
1609 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1610 require_quiet( psheet
, exit
);
1612 printer
= psheet
->GetSelectedPrinter();
1613 require_quiet( printer
, exit
);
1615 printer
->deflt
= ( m_defaultPrinterCtrl
.GetCheck() == BST_CHECKED
) ? true : false;
1622 void CThirdPage::OnBnClickedHaveDisk()
1624 CPrinterSetupWizardSheet
* psheet
;
1627 Manufacturers manufacturers
;
1629 CFileDialog
dlg(TRUE
, NULL
, NULL
, OFN_HIDEREADONLY
|OFN_FILEMUSTEXIST
, L
"Setup Information (*.inf)|*.inf||", this);
1631 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
1632 require_quiet( psheet
, exit
);
1634 printer
= psheet
->GetSelectedPrinter();
1635 require_quiet( printer
, exit
);
1637 service
= printer
->services
.front();
1638 require_quiet( service
, exit
);
1642 if ( dlg
.DoModal() == IDOK
)
1644 CString filename
= dlg
.GetPathName();
1646 LoadPrintDriverDefsFromFile( manufacturers
, filename
, true );
1650 if ( manufacturers
.size() > 0 )
1652 PopulateUI( manufacturers
);
1654 MatchPrinter( manufacturers
, printer
, service
, false );
1660 CString errorMessage
;
1661 CString errorCaption
;
1663 errorMessage
.LoadString( IDS_BAD_INF_FILE
);
1664 errorCaption
.LoadString( IDS_BAD_INF_FILE_CAPTION
);
1666 MessageBox( errorMessage
, errorCaption
, MB_OK
);
1682 CThirdPage::Split( const CString
& string
, TCHAR ch
, CStringList
& components
)
1689 while ( ( n
= temp
.Find( ch
) ) != -1 )
1691 components
.AddTail( temp
.Left( n
) );
1692 temp
= temp
.Right( temp
.GetLength() - ( n
+ 1 ) );
1695 components
.AddTail( temp
);