Blame SOURCES/0001-rhbz-1691287-tdf-53029-ui-prompt-for-printer-authent.patch

64af5b
From e2a12e931018f04b8aba1644ac22b8f1e57a0d0d Mon Sep 17 00:00:00 2001
bf2fec
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
bf2fec
Date: Tue, 26 Mar 2019 12:09:03 +0000
bf2fec
Subject: [PATCH] rhbz#1691287 tdf#53029 ui prompt for printer authentication
bf2fec
bf2fec
refactor and reuse existing dialog to add potential domain entry
bf2fec
bf2fec
Change-Id: Ib884931f8ccc62aad9b3e92ecf93d1da7ffe607b
64af5b
64af5b
Related: rhbz#1691287 fill in default domain and username
64af5b
64af5b
and grab focus to first entry entry
64af5b
64af5b
Change-Id: Icb50766948c77072eaab9faf89436c6ecbb49ecc
64af5b
64af5b
Related: tdf#53029 escape backslash in username for 'domain\username'
64af5b
64af5b
Change-Id: I645623886396b55ccea273bfd697cf319b53f506
bf2fec
---
bf2fec
 vcl/inc/printerinfomanager.hxx        |   2 +
bf2fec
 vcl/uiconfig/ui/cupspassworddialog.ui |  34 +++-
64af5b
 vcl/unx/generic/printer/cupsmgr.cxx   | 259 +++++++++++++++++++-------
64af5b
 3 files changed, 221 insertions(+), 74 deletions(-)
bf2fec
bf2fec
diff --git a/vcl/inc/printerinfomanager.hxx b/vcl/inc/printerinfomanager.hxx
bf2fec
index 7691fbeebc6c..4c225a22f91e 100644
bf2fec
--- a/vcl/inc/printerinfomanager.hxx
bf2fec
+++ b/vcl/inc/printerinfomanager.hxx
bf2fec
@@ -52,6 +52,8 @@ struct PrinterInfo : JobData
bf2fec
     // a list of special features separated by ',' not used by psprint
bf2fec
     // but assigned from the outside (currently for "fax","pdf=","autoqueue","external_dialog")
bf2fec
     OUString             m_aFeatures;
bf2fec
+    // auth-info-required, potential [domain],[username],[password] to prompt for to authenticate printing
bf2fec
+    OUString             m_aAuthInfoRequired;
bf2fec
     bool                 m_bPapersizeFromSetup;
bf2fec
 
bf2fec
     PrinterInfo()
bf2fec
diff --git a/vcl/uiconfig/ui/cupspassworddialog.ui b/vcl/uiconfig/ui/cupspassworddialog.ui
bf2fec
index f4fb757209bb..2c17d1397c3e 100644
bf2fec
--- a/vcl/uiconfig/ui/cupspassworddialog.ui
bf2fec
+++ b/vcl/uiconfig/ui/cupspassworddialog.ui
bf2fec
@@ -73,7 +73,7 @@
bf2fec
               </object>
bf2fec
               <packing>
bf2fec
                 <property name="left_attach">0</property>
bf2fec
-                <property name="top_attach">1</property>
bf2fec
+                <property name="top_attach">2</property>
bf2fec
               </packing>
bf2fec
             </child>
bf2fec
             <child>
bf2fec
@@ -87,7 +87,7 @@
bf2fec
               </object>
bf2fec
               <packing>
bf2fec
                 <property name="left_attach">0</property>
bf2fec
-                <property name="top_attach">2</property>
bf2fec
+                <property name="top_attach">3</property>
bf2fec
               </packing>
bf2fec
             </child>
bf2fec
             <child>
bf2fec
@@ -114,7 +114,7 @@
bf2fec
               </object>
bf2fec
               <packing>
bf2fec
                 <property name="left_attach">1</property>
bf2fec
-                <property name="top_attach">1</property>
bf2fec
+                <property name="top_attach">2</property>
bf2fec
               </packing>
bf2fec
             </child>
bf2fec
             <child>
bf2fec
@@ -123,10 +123,36 @@
bf2fec
                 <property name="can_focus">True</property>
bf2fec
                 <property name="hexpand">True</property>
bf2fec
                 <property name="visibility">False</property>
bf2fec
+                <property name="input_purpose">password</property>
bf2fec
               </object>
bf2fec
               <packing>
bf2fec
                 <property name="left_attach">1</property>
bf2fec
-                <property name="top_attach">2</property>
bf2fec
+                <property name="top_attach">3</property>
bf2fec
+              </packing>
bf2fec
+            </child>
bf2fec
+            <child>
bf2fec
+              <object class="GtkLabel" id="label3">
bf2fec
+                <property name="can_focus">False</property>
bf2fec
+                <property name="no_show_all">True</property>
bf2fec
+                <property name="label" translatable="yes" context="cupspassworddialog|label1">_Domain:</property>
bf2fec
+                <property name="use_underline">True</property>
bf2fec
+                <property name="mnemonic_widget">domain</property>
bf2fec
+                <property name="xalign">1</property>
bf2fec
+              </object>
bf2fec
+              <packing>
bf2fec
+                <property name="left_attach">0</property>
bf2fec
+                <property name="top_attach">1</property>
bf2fec
+              </packing>
bf2fec
+            </child>
bf2fec
+            <child>
bf2fec
+              <object class="GtkEntry" id="domain">
bf2fec
+                <property name="can_focus">True</property>
bf2fec
+                <property name="no_show_all">True</property>
bf2fec
+                <property name="hexpand">True</property>
bf2fec
+              </object>
bf2fec
+              <packing>
bf2fec
+                <property name="left_attach">1</property>
bf2fec
+                <property name="top_attach">1</property>
bf2fec
               </packing>
bf2fec
             </child>
bf2fec
           </object>
bf2fec
diff --git a/vcl/unx/generic/printer/cupsmgr.cxx b/vcl/unx/generic/printer/cupsmgr.cxx
64af5b
index 328e9246e9f5..0f5a647f92a6 100644
bf2fec
--- a/vcl/unx/generic/printer/cupsmgr.cxx
bf2fec
+++ b/vcl/unx/generic/printer/cupsmgr.cxx
bf2fec
@@ -319,6 +319,8 @@ void CUPSManager::initialize()
bf2fec
                 aPrinter.m_aInfo.m_aComment=OStringToOUString(pDest->options[k].value, aEncoding);
bf2fec
             if(!strcmp(pDest->options[k].name, "printer-location"))
bf2fec
                 aPrinter.m_aInfo.m_aLocation=OStringToOUString(pDest->options[k].value, aEncoding);
bf2fec
+            if(!strcmp(pDest->options[k].name, "auth-info-required"))
bf2fec
+                aPrinter.m_aInfo.m_aAuthInfoRequired=OStringToOUString(pDest->options[k].value, aEncoding);
bf2fec
         }
bf2fec
 
bf2fec
         OUStringBuffer aBuf( 256 );
64af5b
@@ -617,6 +619,143 @@ void CUPSManager::getOptionsFromDocumentSetup( const JobData& rJob, bool bBanner
bf2fec
     }
bf2fec
 }
bf2fec
 
bf2fec
+namespace
bf2fec
+{
bf2fec
+    class RTSPWDialog : public ModalDialog
bf2fec
+    {
bf2fec
+        VclPtr<FixedText> m_xText;
bf2fec
+        VclPtr<FixedText> m_xDomainLabel;
bf2fec
+        VclPtr<Edit> m_xDomainEdit;
bf2fec
+        VclPtr<FixedText> m_xUserLabel;
bf2fec
+        VclPtr<Edit> m_xUserEdit;
bf2fec
+        VclPtr<FixedText> m_xPassLabel;
bf2fec
+        VclPtr<Edit> m_xPassEdit;
bf2fec
+
bf2fec
+    public:
bf2fec
+        RTSPWDialog(vcl::Window* pParent, const OString& rServer, const OString& rUserName);
bf2fec
+        virtual void dispose() override;
bf2fec
+        virtual ~RTSPWDialog() override;
bf2fec
+
bf2fec
+        OString getDomain() const
bf2fec
+        {
bf2fec
+            return OUStringToOString( m_xDomainEdit->GetText(), osl_getThreadTextEncoding() );
bf2fec
+        }
bf2fec
+
bf2fec
+        OString getUserName() const
bf2fec
+        {
bf2fec
+            return OUStringToOString( m_xUserEdit->GetText(), osl_getThreadTextEncoding() );
bf2fec
+        }
bf2fec
+
bf2fec
+        OString getPassword() const
bf2fec
+        {
bf2fec
+            return OUStringToOString( m_xPassEdit->GetText(), osl_getThreadTextEncoding() );
bf2fec
+        }
bf2fec
+
bf2fec
+        void SetDomainVisible(bool bShow)
bf2fec
+        {
bf2fec
+            m_xDomainLabel->Show(bShow);
bf2fec
+            m_xDomainEdit->Show(bShow);
bf2fec
+        }
bf2fec
+
bf2fec
+        void SetUserVisible(bool bShow)
bf2fec
+        {
bf2fec
+            m_xUserLabel->Show(bShow);
bf2fec
+            m_xUserEdit->Show(bShow);
bf2fec
+        }
bf2fec
+
bf2fec
+        void SetPassVisible(bool bShow)
bf2fec
+        {
bf2fec
+            m_xPassLabel->Show(bShow);
bf2fec
+            m_xPassEdit->Show(bShow);
bf2fec
+        }
bf2fec
+    };
bf2fec
+
bf2fec
+    RTSPWDialog::RTSPWDialog(vcl::Window* pParent, const OString& rServer, const OString& rUserName)
bf2fec
+        : ModalDialog(pParent, "CUPSPasswordDialog", "vcl/ui/cupspassworddialog.ui")
bf2fec
+    {
bf2fec
+        get(m_xText, "text");
bf2fec
+        get(m_xDomainLabel, "label3");
64af5b
+        get(m_xDomainEdit, "domain");
bf2fec
+        get(m_xUserLabel, "label1");
bf2fec
+        get(m_xUserEdit, "user");
bf2fec
+        get(m_xPassLabel, "label2");
bf2fec
+        get(m_xPassEdit, "pass");
bf2fec
+
bf2fec
+        OUString aText(m_xText->GetText());
bf2fec
+        aText = aText.replaceFirst("%s", OStringToOUString(rServer, osl_getThreadTextEncoding()));
bf2fec
+        m_xText->SetText(aText);
64af5b
+        m_xDomainEdit->SetText("WORKGROUP");
64af5b
+        if (rUserName.isEmpty())
64af5b
+            m_xUserEdit->GrabFocus();
64af5b
+        else
64af5b
+        {
64af5b
+            m_xUserEdit->SetText(OStringToOUString(rUserName, osl_getThreadTextEncoding()));
64af5b
+            m_xPassEdit->GrabFocus();
64af5b
+        }
bf2fec
+    }
bf2fec
+
bf2fec
+    RTSPWDialog::~RTSPWDialog()
bf2fec
+    {
bf2fec
+        disposeOnce();
bf2fec
+    }
bf2fec
+
bf2fec
+    void RTSPWDialog::dispose()
bf2fec
+    {
bf2fec
+        m_xText.clear();
bf2fec
+        m_xDomainLabel.clear();
bf2fec
+        m_xDomainEdit.clear();
bf2fec
+        m_xUserLabel.clear();
bf2fec
+        m_xUserEdit.clear();
bf2fec
+        m_xPassLabel.clear();
bf2fec
+        m_xPassEdit.clear();
bf2fec
+        ModalDialog::dispose();
bf2fec
+    }
bf2fec
+
bf2fec
+    bool AuthenticateQuery(const OString& rServer, OString& rUserName, OString& rPassword)
bf2fec
+    {
bf2fec
+        bool bRet = false;
bf2fec
+
bf2fec
+        ScopedVclPtrInstance<RTSPWDialog> aDialog(nullptr, rServer, rUserName);
bf2fec
+        if (aDialog->Execute())
bf2fec
+        {
bf2fec
+            rUserName = aDialog->getUserName();
bf2fec
+            rPassword = aDialog->getPassword();
bf2fec
+            bRet = true;
bf2fec
+        }
bf2fec
+
bf2fec
+        return bRet;
bf2fec
+    }
bf2fec
+}
bf2fec
+
64af5b
+namespace
64af5b
+{
64af5b
+    OString EscapeCupsOption(const OString& rIn)
64af5b
+    {
64af5b
+        OStringBuffer sRet;
64af5b
+        sal_Int32 nLen = rIn.getLength();
64af5b
+        for (sal_Int32 i = 0; i < nLen; ++i)
64af5b
+        {
64af5b
+            switch(rIn[i])
64af5b
+            {
64af5b
+                case '\\':
64af5b
+                case '\'':
64af5b
+                case '\"':
64af5b
+                case ',':
64af5b
+                case ' ':
64af5b
+                case '\f':
64af5b
+                case '\n':
64af5b
+                case '\r':
64af5b
+                case '\t':
64af5b
+                case '\v':
64af5b
+                    sRet.append('\\');
64af5b
+                    break;
64af5b
+            }
64af5b
+            sRet.append(rIn[i]);
64af5b
+        }
64af5b
+        return sRet.makeStringAndClear();
64af5b
+    }
64af5b
+}
64af5b
+
bf2fec
 bool CUPSManager::endSpool( const OUString& rPrintername, const OUString& rJobTitle, FILE* pFile, const JobData& rDocumentJobData, bool bBanner, const OUString& rFaxNumber )
bf2fec
 {
bf2fec
     SAL_INFO( "vcl.unx.print", "endSpool: " << rPrintername << "," << rJobTitle << " copy count = " << rDocumentJobData.m_nCopies );
64af5b
@@ -642,7 +781,56 @@ bool CUPSManager::endSpool( const OUString& rPrintername, const OUString& rJobTi
bf2fec
         // setup cups options
bf2fec
         int nNumOptions = 0;
bf2fec
         cups_option_t* pOptions = nullptr;
bf2fec
-        getOptionsFromDocumentSetup( rDocumentJobData, bBanner, nNumOptions, reinterpret_cast<void**>(&pOptions) );
bf2fec
+        auto ppOptions = reinterpret_cast<void**>(&pOptions);
bf2fec
+        getOptionsFromDocumentSetup( rDocumentJobData, bBanner, nNumOptions, ppOptions );
bf2fec
+
bf2fec
+        PrinterInfo aInfo(getPrinterInfo(rPrintername));
bf2fec
+        if (!aInfo.m_aAuthInfoRequired.isEmpty())
bf2fec
+        {
bf2fec
+            bool bDomain(false), bUser(false), bPass(false);
bf2fec
+            sal_Int32 nIndex = 0;
bf2fec
+            do
bf2fec
+            {
bf2fec
+                OUString aToken = aInfo.m_aAuthInfoRequired.getToken(0, ',', nIndex);
bf2fec
+                if (aToken == "domain")
bf2fec
+                    bDomain = true;
bf2fec
+                else if (aToken == "username")
bf2fec
+                    bUser = true;
bf2fec
+                else if (aToken == "password")
bf2fec
+                    bPass = true;
bf2fec
+            }
bf2fec
+            while (nIndex >= 0);
bf2fec
+
bf2fec
+            if (bDomain || bUser || bPass)
bf2fec
+            {
bf2fec
+                OString sPrinterName(OUStringToOString(rPrintername, RTL_TEXTENCODING_UTF8));
64af5b
+                OString sUser = cupsUser();
64af5b
+                ScopedVclPtrInstance<RTSPWDialog> aDialog(nullptr, sPrinterName, sUser);
64af5b
+                aDialog->SetDomainVisible(bDomain);
64af5b
+                aDialog->SetUserVisible(bUser);
64af5b
+                aDialog->SetPassVisible(bPass);
bf2fec
+
64af5b
+                if (aDialog->Execute() == RET_OK)
bf2fec
+                {
bf2fec
+                    OString sAuth;
bf2fec
+                    if (bDomain)
64af5b
+                        sAuth = EscapeCupsOption(aDialog->getDomain());
bf2fec
+                    if (bUser)
bf2fec
+                    {
64af5b
+                        if (bDomain)
bf2fec
+                            sAuth += ",";
64af5b
+                        sAuth += EscapeCupsOption(aDialog->getUserName());
bf2fec
+                    }
bf2fec
+                    if (bPass)
bf2fec
+                    {
64af5b
+                        if (bUser || bDomain)
bf2fec
+                            sAuth += ",";
64af5b
+                        sAuth += EscapeCupsOption(aDialog->getPassword());
bf2fec
+                    }
bf2fec
+                    nNumOptions = cupsAddOption("auth-info", sAuth.getStr(), nNumOptions, &pOptions);
bf2fec
+                }
bf2fec
+            }
bf2fec
+        }
bf2fec
 
bf2fec
         OString sJobName(OUStringToOString(rJobTitle, aEnc));
bf2fec
 
64af5b
@@ -825,75 +1013,6 @@ bool CUPSManager::writePrinterConfig()
bf2fec
     return PrinterInfoManager::writePrinterConfig();
bf2fec
 }
bf2fec
 
bf2fec
-namespace
bf2fec
-{
bf2fec
-    class RTSPWDialog : public ModalDialog
bf2fec
-    {
bf2fec
-        VclPtr<FixedText> m_pText;
bf2fec
-        VclPtr<Edit>      m_pUserEdit;
bf2fec
-        VclPtr<Edit>      m_pPassEdit;
bf2fec
-
bf2fec
-    public:
bf2fec
-        RTSPWDialog(const OString& rServer, const OString& rUserName, vcl::Window* pParent);
bf2fec
-        virtual ~RTSPWDialog() override;
bf2fec
-        virtual void dispose() override;
bf2fec
-        OString getUserName() const;
bf2fec
-        OString getPassword() const;
bf2fec
-    };
bf2fec
-
bf2fec
-    RTSPWDialog::RTSPWDialog( const OString& rServer, const OString& rUserName, vcl::Window* pParent )
bf2fec
-        : ModalDialog(pParent, "CUPSPasswordDialog",
bf2fec
-            "vcl/ui/cupspassworddialog.ui")
bf2fec
-    {
bf2fec
-        get(m_pText, "text");
bf2fec
-        get(m_pUserEdit, "user");
bf2fec
-        get(m_pPassEdit, "pass");
bf2fec
-
bf2fec
-        OUString aText(m_pText->GetText());
bf2fec
-        aText = aText.replaceFirst("%s", OStringToOUString(rServer, osl_getThreadTextEncoding()));
bf2fec
-        m_pText->SetText(aText);
bf2fec
-        m_pUserEdit->SetText( OStringToOUString(rUserName, osl_getThreadTextEncoding()));
bf2fec
-    }
bf2fec
-
bf2fec
-    RTSPWDialog::~RTSPWDialog()
bf2fec
-    {
bf2fec
-        disposeOnce();
bf2fec
-    }
bf2fec
-
bf2fec
-    void RTSPWDialog::dispose()
bf2fec
-    {
bf2fec
-        m_pText.clear();
bf2fec
-        m_pUserEdit.clear();
bf2fec
-        m_pPassEdit.clear();
bf2fec
-        ModalDialog::dispose();
bf2fec
-    }
bf2fec
-
bf2fec
-    OString RTSPWDialog::getUserName() const
bf2fec
-    {
bf2fec
-        return OUStringToOString( m_pUserEdit->GetText(), osl_getThreadTextEncoding() );
bf2fec
-    }
bf2fec
-
bf2fec
-    OString RTSPWDialog::getPassword() const
bf2fec
-    {
bf2fec
-        return OUStringToOString( m_pPassEdit->GetText(), osl_getThreadTextEncoding() );
bf2fec
-    }
bf2fec
-
bf2fec
-    bool AuthenticateQuery(const OString& rServer, OString& rUserName, OString& rPassword)
bf2fec
-    {
bf2fec
-        bool bRet = false;
bf2fec
-
bf2fec
-        ScopedVclPtrInstance<RTSPWDialog> aDialog(rServer, rUserName, nullptr);
bf2fec
-        if (aDialog->Execute())
bf2fec
-        {
bf2fec
-            rUserName = aDialog->getUserName();
bf2fec
-            rPassword = aDialog->getPassword();
bf2fec
-            bRet = true;
bf2fec
-        }
bf2fec
-
bf2fec
-        return bRet;
bf2fec
-    }
bf2fec
-}
bf2fec
-
bf2fec
 const char* CUPSManager::authenticateUser()
bf2fec
 {
bf2fec
     const char* pRet = nullptr;
bf2fec
-- 
bf2fec
2.21.0
bf2fec