desktop/source/lib/init.cxx      |   21 +++++++++++++++++++++
 sfx2/source/doc/sfxbasemodel.cxx |   18 +++++++++++++++++-
 2 files changed, 38 insertions(+), 1 deletion(-)

New commits:
commit a52ba26e6a3567a1f63aeb8d20c084eb286974b4
Author:     Jaume Pujantell <jaume.pujant...@collabora.com>
AuthorDate: Fri Jul 14 09:11:07 2023 +0200
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Fri Jul 28 10:04:50 2023 +0200

    Added possibility to set password on save through arguments
    
    Change-Id: I579ed7487f87515a21d83912d9c0d12e9edc5eea
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154415
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 8d830c0cbd00..6e8c0db99223 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -3270,6 +3270,23 @@ static int doc_saveAs(LibreOfficeKitDocument* pThis, 
const char* sUrl, const cha
 
         bool bFullSheetPreview = sFullSheetPreview == u"true";
 
+        OUString filePassword;
+        if ((aIndex = aFilterOptions.indexOf(",Password=")) >= 0)
+        {
+            int bIndex = aFilterOptions.indexOf("PASSWORDEND");
+            filePassword = aFilterOptions.subView(aIndex + 10, bIndex - 
(aIndex + 10));
+            aFilterOptions = OUString::Concat(aFilterOptions.subView(0, 
aIndex))
+                             + aFilterOptions.subView(bIndex + 11);
+        }
+        OUString filePasswordToModify;
+        if ((aIndex = aFilterOptions.indexOf(",PasswordToModify=")) >= 0)
+        {
+            int bIndex = aFilterOptions.indexOf("PASSWORDTOMODIFYEND");
+            filePassword = aFilterOptions.subView(aIndex + 18, bIndex - 
(aIndex + 18));
+            aFilterOptions = OUString::Concat(aFilterOptions.subView(0, 
aIndex))
+                             + aFilterOptions.subView(bIndex + 19);
+        }
+
         // Select a pdf version if specified a valid one. If not specified 
then ignore.
         // If invalid then fail.
         sal_Int32 pdfVer = 0;
@@ -3344,6 +3361,10 @@ static int doc_saveAs(LibreOfficeKitDocument* pThis, 
const char* sUrl, const cha
         {
             aSaveMediaDescriptor["FilterData"] <<= 
aFilterDataMap.getAsConstPropertyValueList();
         }
+        if (!filePassword.isEmpty())
+            aSaveMediaDescriptor["Password"] <<= filePassword;
+        if (!filePasswordToModify.isEmpty())
+            aSaveMediaDescriptor["PasswordToModify"] <<= filePasswordToModify;
 
         // add interaction handler too
         if (gImpl)
diff --git a/sfx2/source/doc/sfxbasemodel.cxx b/sfx2/source/doc/sfxbasemodel.cxx
index 2b34d5fd5b46..ac0de0cc89a5 100644
--- a/sfx2/source/doc/sfxbasemodel.cxx
+++ b/sfx2/source/doc/sfxbasemodel.cxx
@@ -131,6 +131,7 @@
 #include <sfx2/DocumentMetadataAccess.hxx>
 #include "printhelper.hxx"
 #include <sfx2/sfxresid.hxx>
+#include <sfx2/filedlghelper.hxx>
 #include <comphelper/profilezone.hxx>
 #include <vcl/threadex.hxx>
 #include <unotools/mediadescriptor.hxx>
@@ -3046,13 +3047,13 @@ void SfxBaseModel::impl_store(  const   OUString&       
            sURL
         throw frame::IllegalArgumentIOException();
 
     bool bSaved = false;
+    ::comphelper::SequenceAsHashMap aArgHash(seqArguments);
     if ( !bSaveTo && m_pData->m_pObjectShell.is() && !sURL.isEmpty()
       && !sURL.startsWith( "private:stream" )
       && ::utl::UCBContentHelper::EqualURLs( getLocation(), sURL ) )
     {
         // this is the same file URL as the current document location, try to 
use storeOwn if possible
 
-        ::comphelper::SequenceAsHashMap aArgHash( seqArguments );
         static constexpr OUStringLiteral aFilterString( u"FilterName"  );
         const OUString aFilterName( aArgHash.getUnpackedValueOrDefault( 
aFilterString, OUString() ) );
         if ( !aFilterName.isEmpty() )
@@ -3122,11 +3123,26 @@ void SfxBaseModel::impl_store(  const   OUString&       
            sURL
     SfxGetpApp()->NotifyEvent( SfxEventHint( bSaveTo ? 
SfxEventHintId::SaveToDoc : SfxEventHintId::SaveAsDoc, 
GlobalEventConfig::GetEventName( bSaveTo ? GlobalEventId::SAVETODOC : 
GlobalEventId::SAVEASDOC ),
                                             m_pData->m_pObjectShell.get() ) );
 
+    const OUString 
aFilterName(aArgHash.getUnpackedValueOrDefault("FilterName", OUString()));
+    OUString aPassword, aPasswordToModify;
+    if (!aArgHash.getUnpackedValueOrDefault("EncryptionData", 
Sequence<beans::NamedValue>())
+             .hasElements())
+        aPassword = aArgHash.getUnpackedValueOrDefault("Password", OUString());
+    if (!aArgHash.getUnpackedValueOrDefault("ModifyPasswordInfo", 
Sequence<beans::PropertyValue>())
+             .hasElements()
+        && aArgHash.getUnpackedValueOrDefault("ModifyPasswordInfo", 
static_cast<sal_Int32>(0)) == 0)
+        aPasswordToModify = 
aArgHash.getUnpackedValueOrDefault("PasswordToModify", OUString());
+    aArgHash.erase("PasswordToModify");
+
     std::optional<SfxAllItemSet> pItemSet(SfxGetpApp()->GetPool());
     pItemSet->Put(SfxStringItem(SID_FILE_NAME, sURL));
     if ( bSaveTo )
         pItemSet->Put(SfxBoolItem(SID_SAVETO, true));
 
+    if (!aFilterName.isEmpty() && (!aPassword.isEmpty() || 
!aPasswordToModify.isEmpty()))
+        
sfx2::SetPassword(SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName(aFilterName),
+                          &*pItemSet, aPassword, aPasswordToModify, false);
+
     TransformParameters(SID_SAVEASDOC, seqArguments, *pItemSet);
 
     const SfxBoolItem* pCopyStreamItem = 
pItemSet->GetItem<SfxBoolItem>(SID_COPY_STREAM_IF_POSSIBLE, false);

Reply via email to