tdf#112369 allow to disable the add command button in menu customize

Instead of showing an alert dialog reporting that a command is already present
in a given menu, enable or disable the add command button when needed.

Change-Id: I52b9477896d4775ae2033c057fa1b5bfccb6a749
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115057
Tested-by: Jenkins
Reviewed-by: Heiko Tietze <heiko.tietze@documentfoundation.org>
diff --git a/cui/source/customize/SvxMenuConfigPage.cxx b/cui/source/customize/SvxMenuConfigPage.cxx
index 3181a78..34cee13 100644
--- a/cui/source/customize/SvxMenuConfigPage.cxx
+++ b/cui/source/customize/SvxMenuConfigPage.cxx
@@ -172,8 +172,17 @@ void SvxMenuConfigPage::UpdateButtonStates()

    m_xInsertBtn->set_sensitive(pMenuData != nullptr);

    m_xAddCommandButton->set_sensitive(pMenuData != nullptr);
    m_xRemoveCommandButton->set_sensitive(pMenuData != nullptr);
    SvxConfigEntry* selectedCmd = CreateCommandFromSelection(GetScriptURL());

    m_xAddCommandButton->set_sensitive(
        pMenuData != nullptr && !IsCommandInMenuList(selectedCmd, pMenuData->GetEntries()));

    delete selectedCmd;

    if (bIsValidSelection)
    {
        m_xRemoveCommandButton->set_sensitive(pMenuData != nullptr);
    }

    //Handle the gear button
    if (pMenuData && m_bIsMenuBar)
diff --git a/cui/source/customize/cfg.cxx b/cui/source/customize/cfg.cxx
index 3ab8e42..de32e10 100644
--- a/cui/source/customize/cfg.cxx
+++ b/cui/source/customize/cfg.cxx
@@ -1395,18 +1395,14 @@ SvxEntries* SvxConfigPage::FindParentForChild(
    return nullptr;
}

int SvxConfigPage::AddFunction(int nTarget, bool bAllowDuplicates)
SvxConfigEntry *SvxConfigPage::CreateCommandFromSelection(const OUString &aURL)
{
    OUString aURL = GetScriptURL();
    SvxConfigEntry* pParent = GetTopLevelSelection();

    if ( aURL.isEmpty() || pParent == nullptr )
    {
        return -1;
    }

    OUString aDisplayName;

    if ( aURL.isEmpty() ) {
        return nullptr;
    }

    auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(aURL, m_aModuleId);

    if ( typeid(*pCurrentSaveInData) == typeid(ContextMenuSaveInData) )
@@ -1416,30 +1412,64 @@ int SvxConfigPage::AddFunction(int nTarget, bool bAllowDuplicates)
    else
        aDisplayName = vcl::CommandInfoProvider::GetLabelForCommand(aProperties);

    SvxConfigEntry* pNewEntryData =
    SvxConfigEntry* toret =
        new SvxConfigEntry( aDisplayName, aURL, false, /*bParentData*/false );
    pNewEntryData->SetUserDefined();

    toret->SetUserDefined();

    if ( aDisplayName.isEmpty() )
        pNewEntryData->SetName( GetSelectedDisplayName() );
        toret->SetName( GetSelectedDisplayName() );

    // check that this function is not already in the menu
    if ( !bAllowDuplicates )
    return toret;
}

bool SvxConfigPage::IsCommandInMenuList(const SvxConfigEntry *pEntryData,
                                        const SvxEntries *pEntries)
{
    bool toret = false;

    if ( pEntries != nullptr
      && pEntryData != nullptr )
    {
        for (auto const& entry : *pParent->GetEntries())
        for (auto const& entry : *pEntries)
        {
            if ( entry->GetCommand() == pNewEntryData->GetCommand() )
            {
                std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetFrameWeld(),
                                                          VclMessageType::Info, VclButtonsType::Ok, CuiResId(RID_SVXSTR_MNUCFG_ALREADY_INCLUDED)));
                xBox->run();
                delete pNewEntryData;
                return -1;
            }
                if ( entry->GetCommand() == pEntryData->GetCommand() )
                {
                    toret = true;
                    break;
                }
        }
    }

    return AppendEntry(pNewEntryData, nTarget);
    return toret;
}

int SvxConfigPage::AddFunction(int nTarget, bool bAllowDuplicates)
{
    int toret = -1;
    OUString aURL = GetScriptURL();
    SvxConfigEntry* pParent = GetTopLevelSelection();

    if ( aURL.isEmpty() || pParent == nullptr )
    {
        return -1;
    }


    SvxConfigEntry * pNewEntryData = CreateCommandFromSelection( aURL );

    // check that this function is not already in the menu
    if ( !bAllowDuplicates
      && IsCommandInMenuList( pNewEntryData, pParent->GetEntries() )
    )
    {
        delete pNewEntryData;
    } else {
        toret = AppendEntry( pNewEntryData, nTarget );
    }

    UpdateButtonStates();
    return toret;
}

int SvxConfigPage::AppendEntry(
@@ -1599,6 +1629,8 @@ IMPL_LINK_NOARG(SvxConfigPage, SelectFunctionHdl, weld::TreeView&, void)

        m_xDescriptionField->set_text("");
    }

    UpdateButtonStates();
}

IMPL_LINK_NOARG(SvxConfigPage, ImplUpdateDataHdl, Timer*, void)
diff --git a/cui/source/inc/cfg.hxx b/cui/source/inc/cfg.hxx
index c5b7c48..c1b492c 100644
--- a/cui/source/inc/cfg.hxx
+++ b/cui/source/inc/cfg.hxx
@@ -466,6 +466,11 @@ protected:

    void            ReloadTopLevelListBox( SvxConfigEntry const * pSelection = nullptr );

    static bool     IsCommandInMenuList(const SvxConfigEntry *pEntryData,
                                        const SvxEntries *pEntries);

    SvxConfigEntry *CreateCommandFromSelection(const OUString &aURL);

public:

    virtual ~SvxConfigPage() override;