tdf#150044 sc import: fix editing password in shared mode

Set the readonly state back after we create the tempfile
for shared files. It is necessary for later, when we checking
the file is readonly or not and we need to give the editing password.

TODO: filesave IO error is another bug

Change-Id: Iafc4dc6e7ce825fc3b7fe18abaee65c014c49a0d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137281
Tested-by: Jenkins
Reviewed-by: Tünde Tóth <toth.tunde@nisz.hu>
Tested-by: Gabor Kelemen <kelemeng@ubuntu.com>
Tested-by: Balazs Varga <balazs.varga.extern@allotropia.de>
Reviewed-by: Balazs Varga <balazs.varga.extern@allotropia.de>
(cherry picked from commit c7b6c9407ce109ab27257f4c1ec66b86b48622df)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137319
Reviewed-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
diff --git a/sc/qa/uitest/calc_tests7/tdf150044.py b/sc/qa/uitest/calc_tests7/tdf150044.py
new file mode 100644
index 0000000..6e42358
--- /dev/null
+++ b/sc/qa/uitest/calc_tests7/tdf150044.py
@@ -0,0 +1,69 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from uitest.framework import UITestCase
from libreoffice.uno.propertyvalue import mkPropertyValues
from org.libreoffice.unotest import systemPathToFileUrl
from uitest.uihelper.common import select_by_text
from tempfile import TemporaryDirectory
import os.path

class save_shared_readonly_with_password(UITestCase):

   def test_save_to_shared_ods(self):

        with TemporaryDirectory() as tempdir:
            xFilePath = os.path.join(tempdir, "shared_readonly_with_password_tmp.ods")

            with self.ui_test.create_doc_in_start_center("calc"):
                with self.ui_test.execute_dialog_through_command(".uno:ShareDocument", close_button="") as xShareDocumentDialog:
                    xShareCheckButton = xShareDocumentDialog.getChild("share")
                    xShareCheckButton.executeAction("CLICK", tuple())
                    xOk = xShareDocumentDialog.getChild("ok")
                    # Save the document
                    with self.ui_test.execute_dialog_through_action(xOk, "CLICK", close_button="") as xSaveDialog:
                        xFileName = xSaveDialog.getChild("file_name")
                        xFileName.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
                        xFileName.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
                        xFileName.executeAction("TYPE", mkPropertyValues({"TEXT": xFilePath}))
                        xPasswordCheckButton = xSaveDialog.getChild("password")
                        xPasswordCheckButton.executeAction("CLICK", tuple())
                        xOpen = xSaveDialog.getChild("open")

                        with self.ui_test.execute_dialog_through_action(xOpen, "CLICK") as xPasswordDialog:
                            xReadonly = xPasswordDialog.getChild("readonly")
                            xReadonly.executeAction("CLICK", tuple())
                            xNewPassword = xPasswordDialog.getChild("newpassroEntry")
                            xNewPassword.executeAction("TYPE", mkPropertyValues({"TEXT": "password"}))
                            xConfirmPassword = xPasswordDialog.getChild("confirmropassEntry")
                            xConfirmPassword.executeAction("TYPE", mkPropertyValues({"TEXT": "password"}))

            self.ui_test.wait_until_file_is_available(xFilePath)

            with self.ui_test.execute_dialog_through_command(".uno:Open", close_button="") as xOpenDialog:
                # Open document
                xFileName = xOpenDialog.getChild("file_name")
                xFileName.executeAction("TYPE", mkPropertyValues({"TEXT": xFilePath}))
                xOpenBtn = xOpenDialog.getChild("open")
                xOpenBtn.executeAction("CLICK", tuple())

                xDialog = self.ui_test.wait_for_top_focus_window('SharedWarningDialog')
                xOk = xDialog.getChild("ok")
                xOk.executeAction("CLICK", tuple())

                document = self.ui_test.get_component()
                self.assertTrue(document.isReadonly())

                with self.ui_test.execute_dialog_through_command(".uno:EditDoc") as xDialog:
                    # check that we have a password dialog for editing the shared document
                    xPassword = xDialog.getChild("newpassEntry")
                    xPassword.executeAction("TYPE", mkPropertyValues({"TEXT": "password"}))

                self.assertFalse(document.isReadonly())

# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sfx2/source/doc/docfile.cxx b/sfx2/source/doc/docfile.cxx
index aae5a87..63966fb 100644
--- a/sfx2/source/doc/docfile.cxx
+++ b/sfx2/source/doc/docfile.cxx
@@ -4387,17 +4387,18 @@ OUString SfxMedium::SwitchDocumentToTempFile()
                    {}
                }

                if (bWasReadonly)
                {
                    // set the readonly state back
                    pImpl->m_nStorOpenMode = SFX_STREAM_READONLY;
                    GetItemSet()->Put(SfxBoolItem(SID_DOC_READONLY, true));
                }

                if ( aResult.isEmpty() )
                {
                    Close();
                    SetPhysicalName_Impl( OUString() );
                    SetName( aOrigURL );
                    if ( bWasReadonly )
                    {
                        // set the readonly state back
                        pImpl->m_nStorOpenMode = SFX_STREAM_READONLY;
                        GetItemSet()->Put( SfxBoolItem(SID_DOC_READONLY, true));
                    }
                    GetMedium_Impl();
                    pImpl->xStorage = xStorage;
                }