tdf#151741 - BASIC Organizer: fill various URLs for libraries

Otherwise, libraries cannot be renamed in the BASIC organizer.

Change-Id: I6c78cdee12f8d6128acf68e889fca463ed782477
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148483
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
diff --git a/basctl/source/basicide/moduldl2.cxx b/basctl/source/basicide/moduldl2.cxx
index 12650fc..2b21a6f 100644
--- a/basctl/source/basicide/moduldl2.cxx
+++ b/basctl/source/basicide/moduldl2.cxx
@@ -1320,6 +1320,22 @@ void createLibImpl(weld::Window* pWin, const ScriptDocument& rDocument,
            if ( !rDocument.createModule( aLibName, aModName, true, sModuleCode ) )
                throw Exception("could not create module " + aModName, nullptr);

            // tdf#151741 - store all libraries to the file system, otherwise they
            // cannot be renamed/moved since the SfxLibraryContainer::renameLibrary
            // moves the foldes/files on the file system
            Reference<script::XLibraryContainer2> xModLibContainer(
                rDocument.getLibraryContainer(E_SCRIPTS), UNO_QUERY);
            Reference<script::XLibraryContainer2> xDlgLibContainer(
                rDocument.getLibraryContainer(E_DIALOGS), UNO_QUERY);
            Reference<script::XPersistentLibraryContainer> xModPersLibContainer(xModLibContainer,
                                                                                UNO_QUERY);
            if (xModPersLibContainer.is())
                xModPersLibContainer->storeLibraries();
            Reference<script::XPersistentLibraryContainer> xDlgPersLibContainer(xDlgLibContainer,
                                                                                UNO_QUERY);
            if (xDlgPersLibContainer.is())
                xDlgPersLibContainer->storeLibraries();

            SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, rDocument, aLibName, aModName, TYPE_MODULE );
            if (SfxDispatcher* pDispatcher = GetDispatcher())
                pDispatcher->ExecuteList(SID_BASICIDE_SBXINSERTED,
diff --git a/basic/source/uno/namecont.cxx b/basic/source/uno/namecont.cxx
index 7716649e..ca8beee 100644
--- a/basic/source/uno/namecont.cxx
+++ b/basic/source/uno/namecont.cxx
@@ -2146,6 +2146,9 @@ Reference< XNameContainer > SAL_CALL SfxLibraryContainer::createLibrary( const O
    pNewLib->maLibElementFileExtension = maLibElementFileExtension;

    createVariableURL( pNewLib->maUnexpandedStorageURL, Name, maInfoFileName, true );
    // tdf#151741 - fill various storage URLs for the newly created library
    checkStorageURL(pNewLib->maUnexpandedStorageURL, pNewLib->maLibInfoFileURL,
                    pNewLib->maStorageURL, pNewLib->maUnexpandedStorageURL);

    Reference< XNameAccess > xNameAccess( pNewLib );
    Any aElement;
@@ -2491,10 +2494,6 @@ void SAL_CALL SfxLibraryContainer::renameLibrary( const OUString& Name, const OU
    }
    loadLibrary( Name );

    // Remove from container
    maNameContainer->removeByName( Name );
    maModifiable.setModified( true );

    // Rename library folder, but not for linked libraries
    bool bMovedSuccessful = true;

@@ -2505,6 +2504,14 @@ void SAL_CALL SfxLibraryContainer::renameLibrary( const OUString& Name, const OU
        bMovedSuccessful = false;

        OUString aLibDirPath = pImplLib->maStorageURL;
        // tdf#151741 - fill various storage URLs for the library
        // These URLs should not be empty for newly created libraries after
        // the change in SfxLibraryContainer::createLibrary.
        if (aLibDirPath.isEmpty())
        {
            checkStorageURL(pImplLib->maUnexpandedStorageURL, pImplLib->maLibInfoFileURL,
                            pImplLib->maStorageURL, pImplLib->maUnexpandedStorageURL);
        }

        INetURLObject aDestInetObj( o3tl::getToken(maLibraryPath, 1, ';'));
        aDestInetObj.insertName( NewName, true, INetURLObject::LAST_SEGMENT,
@@ -2581,12 +2588,13 @@ void SAL_CALL SfxLibraryContainer::renameLibrary( const OUString& Name, const OU

                bMovedSuccessful = true;
                pImplLib->implSetModified( true );
                // Remove old library from container
                maNameContainer->removeByName( Name );
                maModifiable.setModified( true );
            }
        }
        catch(const Exception& )
        {
            // Restore old library
            maNameContainer->insertByName( Name, aLibAny ) ;
        }
    }