Ensure that printer port is created before calling AddPrinter()
authorsherscher@apple.com <sherscher@apple.com@214c2c4a-bf3b-4dcf-9390-e4dd3010487d>
Wed, 30 Sep 2009 06:19:34 +0000 (06:19 +0000)
committersherscher@apple.com <sherscher@apple.com@214c2c4a-bf3b-4dcf-9390-e4dd3010487d>
Wed, 30 Sep 2009 06:19:34 +0000 (06:19 +0000)
git-svn-id: http://svn.macosforge.org/repository/mDNSResponder/trunk@6713 214c2c4a-bf3b-4dcf-9390-e4dd3010487d

Clients/PrinterSetupWizard/PrinterSetupWizardSheet.cpp
Clients/PrinterSetupWizard/PrinterSetupWizardSheet.h
Clients/PrinterSetupWizard/SecondPage.cpp
Clients/PrinterSetupWizard/ThirdPage.cpp

index 018d135..a27785d 100644 (file)
@@ -286,16 +286,22 @@ CPrinterSetupWizardSheet::InstallPrinter(Printer * printer)
 
                if ( service->type == kPDLServiceType )
                {
-                       err = InstallPrinterPDLAndLPR( printer, service, PROTOCOL_RAWTCP_TYPE, log );
+                       err = InstallPrinterPort( printer, service, PROTOCOL_RAWTCP_TYPE, log );
+                       require_noerr_with_log( log, "InstallPrinterPort()", err, exit );
+                       err = InstallPrinterPDLAndLPR( printer, service, log );
                        require_noerr_with_log( log, "InstallPrinterPDLAndLPR()", err, exit );
                }
                else if ( service->type == kLPRServiceType )
                {
-                       err = InstallPrinterPDLAndLPR( printer, service, PROTOCOL_LPR_TYPE, log );
+                       err = InstallPrinterPort( printer, service, PROTOCOL_LPR_TYPE, log );
+                       require_noerr_with_log( log, "InstallPrinterPort()", err, exit );
+                       err = InstallPrinterPDLAndLPR( printer, service, log );
                        require_noerr_with_log( log, "InstallPrinterPDLAndLPR()", err, exit );
                }
                else if ( service->type == kIPPServiceType )
                {
+                       err = InstallPrinterPort( printer, service, PROTOCOL_RAWTCP_TYPE, log );
+                       require_noerr_with_log( log, "InstallPrinterPort()", err, exit );
                        err = InstallPrinterIPP( printer, service, log );
                        require_noerr_with_log( log, "InstallPrinterIPP()", err, exit );
                }
@@ -324,23 +330,23 @@ exit:
 
 
 OSStatus
-CPrinterSetupWizardSheet::InstallPrinterPDLAndLPR(Printer * printer, Service * service, DWORD protocol, Logger & log )
+CPrinterSetupWizardSheet::InstallPrinterPort( Printer * printer, Service * service, DWORD protocol, Logger & log )
 {
        PRINTER_DEFAULTS        printerDefaults =       { NULL,  NULL, SERVER_ACCESS_ADMINISTER };
+       PORT_DATA_1                     portData;
        DWORD                           dwStatus;
        DWORD                           cbInputData             =       100;
        PBYTE                           pOutputData             =       NULL;
        DWORD                           cbOutputNeeded  =       0;
-       PORT_DATA_1                     portData;
-       PRINTER_INFO_2          pInfo;
        HANDLE                          hXcv                    =       NULL;
-       HANDLE                          hPrinter                =       NULL;
        Queue                   *       q;
        BOOL                            ok;
        OSStatus                        err;
 
-       check(printer != NULL);
-       check(printer->installed == false);
+       ZeroMemory(&portData, sizeof(PORT_DATA_1));
+
+       require_action_with_log( log, wcslen(printer->portName) < sizeof_array(portData.sztPortName), exit, err = kSizeErr );
+       wcscpy_s(portData.sztPortName, printer->portName);
 
        q = service->queues.front();
        check( q );
@@ -362,14 +368,6 @@ CPrinterSetupWizardSheet::InstallPrinterPDLAndLPR(Printer * printer, Service * s
        }
 
        require_action_with_log( log, pOutputData, exit, err = kNoMemoryErr );
-       
-       //
-       // setup the port
-       //
-       ZeroMemory(&portData, sizeof(PORT_DATA_1));
-
-       require_action_with_log( log, wcslen(printer->portName) < sizeof_array(portData.sztPortName), exit, err = kSizeErr );
-       wcscpy_s(portData.sztPortName, printer->portName);
        
        portData.dwPortNumber   =       service->portNumber;
        portData.dwVersion              =       1;
@@ -378,7 +376,7 @@ CPrinterSetupWizardSheet::InstallPrinterPDLAndLPR(Printer * printer, Service * s
        portData.dwProtocol     = protocol;
        portData.cbSize         = sizeof PORT_DATA_1;
        portData.dwReserved     = 0L;
-       
+
        require_action_with_log( log, wcslen(q->name) < sizeof_array(portData.sztQueue), exit, err = kSizeErr );
        wcscpy_s(portData.sztQueue, q->name);
 
@@ -389,6 +387,36 @@ CPrinterSetupWizardSheet::InstallPrinterPDLAndLPR(Printer * printer, Service * s
        err = translate_errno( ok, errno_compat(), kUnknownErr );
        require_noerr_with_log( log, "XcvData()", err, exit );
 
+exit:
+
+       if (hXcv != NULL)
+       {
+               ClosePrinter(hXcv);
+       }
+
+       if (pOutputData != NULL)
+       {
+               delete [] pOutputData;
+       }
+
+       return err;
+}
+
+
+OSStatus
+CPrinterSetupWizardSheet::InstallPrinterPDLAndLPR(Printer * printer, Service * service, Logger & log )
+{
+       PRINTER_INFO_2          pInfo;
+       HANDLE                          hPrinter = NULL;
+       Queue                   *       q;
+       OSStatus                        err;
+
+       check(printer != NULL);
+       check(printer->installed == false);
+
+       q = service->queues.front();
+       check( q );
+
        //
        // add the printer
        //
@@ -425,16 +453,6 @@ exit:
                ClosePrinter(hPrinter);
        }
 
-       if (hXcv != NULL)
-       {
-               ClosePrinter(hXcv);
-       }
-
-       if (pOutputData != NULL)
-       {
-               delete [] pOutputData;
-       }
-
        return err;
 }
 
@@ -725,7 +743,7 @@ BOOL CPrinterSetupWizardSheet::OnCommand(WPARAM wParam, LPARAM lParam)
 BOOL CPrinterSetupWizardSheet::OnInitDialog()
 {
        OSStatus err;
-       
+
        CPropertySheet::OnInitDialog();
 
        err = StartBrowse();
@@ -981,7 +999,7 @@ CPrinterSetupWizardSheet::OnBrowse(
        OSStatus                                                err = kNoErr;
 
        require_noerr( inErrorCode, exit );
-       
+
        self = reinterpret_cast <CPrinterSetupWizardSheet*>( inContext );
        require_quiet( self, exit );
 
@@ -1006,6 +1024,10 @@ CPrinterSetupWizardSheet::OnBrowse(
                        printer = self->OnAddPrinter( inInterfaceIndex, inName, inType, inDomain, moreComing );
                        require_action( printer, exit, err = kUnknownErr );
                }
+               else if ( self->GetActivePage() == &self->m_pgSecond )
+               {
+                       self->m_pgSecond.OnAddPrinter( NULL, moreComing );
+               }
 
                if ( !service )
                {
index e7823b8..1ffd9bb 100644 (file)
@@ -281,7 +281,10 @@ private:
        InstallPrinter(Printer * printer);
 
        OSStatus
-       InstallPrinterPDLAndLPR(Printer * printer, Service * service, DWORD protocol, Logger & log);
+       InstallPrinterPort( Printer * printer, Service * service, DWORD protocol, Logger & log );
+
+       OSStatus
+       InstallPrinterPDLAndLPR(Printer * printer, Service * service, Logger & log);
 
        OSStatus
        InstallPrinterIPP(Printer * printer, Service * service, Logger & log);
index 0499b5b..35bbefa 100644 (file)
@@ -234,36 +234,39 @@ CSecondPage::OnAddPrinter(
 
        psheet = reinterpret_cast<CPrinterSetupWizardSheet*>(GetParent());
        require_quiet( psheet, exit );
-       
-       selectedPrinter = psheet->GetSelectedPrinter();
 
-       printer->item = m_browseList.InsertItem(printer->displayName);
+       if ( printer )
+       {
+               selectedPrinter = psheet->GetSelectedPrinter();
 
-       m_browseList.SetItemData( printer->item, (DWORD_PTR) printer );
+               printer->item = m_browseList.InsertItem(printer->displayName);
 
-       m_browseList.SortChildren(TVI_ROOT);
+               m_browseList.SetItemData( printer->item, (DWORD_PTR) printer );
 
-       //
-       // if the searching item is still in the list
-       // get rid of it
-       //
-       // note that order is important here.  Insert the printer
-       // item before removing the placeholder so we always have
-       // an item in the list to avoid experiencing the bug
-       // in Microsoft's implementation of CTreeCtrl
-       //
-       if (m_emptyListItem != NULL)
-       {
-               m_browseList.DeleteItem(m_emptyListItem);
-               m_emptyListItem = NULL;
-               m_browseList.EnableWindow(TRUE);
-       }
+               m_browseList.SortChildren(TVI_ROOT);
 
-       if ( !selectedPrinter )
-       {
-               psheet->SetSelectedPrinter( printer );
-               m_browseList.SelectItem( printer->item );
-               ::SetFocus( m_browseList );
+               //
+               // if the searching item is still in the list
+               // get rid of it
+               //
+               // note that order is important here.  Insert the printer
+               // item before removing the placeholder so we always have
+               // an item in the list to avoid experiencing the bug
+               // in Microsoft's implementation of CTreeCtrl
+               //
+               if (m_emptyListItem != NULL)
+               {
+                       m_browseList.DeleteItem(m_emptyListItem);
+                       m_emptyListItem = NULL;
+                       m_browseList.EnableWindow(TRUE);
+               }
+
+               if ( !selectedPrinter )
+               {
+                       psheet->SetSelectedPrinter( printer );
+                       m_browseList.SelectItem( printer->item );
+                       ::SetFocus( m_browseList );
+               }
        }
 
 exit:
@@ -294,25 +297,28 @@ CSecondPage::OnRemovePrinter(
 
        m_browseList.SetRedraw(FALSE);
 
-       //
-       // check to make sure if we're the only item in the control...i.e.
-       // the list size is 1.
-       //
-       if (m_browseList.GetCount() > 1)
-       {
-               //
-               // if we're not the only thing in the list, then
-               // simply remove it from the list
-               //
-               m_browseList.DeleteItem( printer->item );
-       }
-       else
+       if ( printer )
        {
                //
-               // if we're the only thing in the list, then redisplay
-               // it with the no printers message
+               // check to make sure if we're the only item in the control...i.e.
+               // the list size is 1.
                //
-               InitBrowseList();
+               if (m_browseList.GetCount() > 1)
+               {
+                       //
+                       // if we're not the only thing in the list, then
+                       // simply remove it from the list
+                       //
+                       m_browseList.DeleteItem( printer->item );
+               }
+               else
+               {
+                       //
+                       // if we're the only thing in the list, then redisplay
+                       // it with the no printers message
+                       //
+                       InitBrowseList();
+               }
        }
 
 exit:
index 0261bf2..5a7acf8 100644 (file)
@@ -238,6 +238,8 @@ CThirdPage::SelectMatch(Manufacturers & manufacturers, Printer * printer, Servic
 void
 CThirdPage::CopyPrinterSettings( Printer * printer, Service * service, Manufacturer * manufacturer, Model * model )
 {
+       DWORD portNameLen;
+
        printer->manufacturer           =       manufacturer->name;
        printer->displayModelName       =       model->displayName;
        printer->modelName                      =       model->name;
@@ -281,6 +283,15 @@ CThirdPage::CopyPrinterSettings( Printer * printer, Service * service, Manufactu
 
                service->protocol = L"IPP";
        }
+
+       // Truncate the portName so that it's valid
+
+       portNameLen = printer->portName.GetLength() + 1;
+               
+       if ( portNameLen > MAX_PORTNAME_LEN )
+       {
+               printer->portName.Delete( MAX_PORTNAME_LEN - 1, ( portNameLen - MAX_PORTNAME_LEN ) );
+       }
 }
 
 // --------------------------------------------------------