tdf#138883 Prevent renaming Templates

*Disable ok button added a tooltip when renaming to an existing template
*Check while typing.

Change-Id: Iec7266940a1cde1a086ba612c0f2f42dd3e6fc73
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113719
Tested-by: Jenkins
Reviewed-by: Heiko Tietze <heiko.tietze@documentfoundation.org>
diff --git a/include/sfx2/inputdlg.hxx b/include/sfx2/inputdlg.hxx
index 1ca2b9b..adf9c2a 100644
--- a/include/sfx2/inputdlg.hxx
+++ b/include/sfx2/inputdlg.hxx
@@ -19,12 +19,18 @@ private:
    std::unique_ptr<weld::Entry> m_xEntry;
    std::unique_ptr<weld::Label> m_xLabel;
    std::unique_ptr<weld::Button> m_xHelp;
    std::unique_ptr<weld::Button> m_xOk;
    std::function<bool(OUString)> mCheckEntry;
    DECL_LINK(EntryChangedHdl, weld::Entry&, void);

public:
    InputDialog(weld::Widget* pParent, const OUString& rLabelText);
    OUString GetEntryText() const;
    void SetEntryText(const OUString& rStr);
    void HideHelpBtn();
    void SetEntryMessageType(weld::EntryMessageType aType);
    void SetTooltip(const OUString& rStr);
    void setCheckEntry(std::function<bool(OUString)> aFunc);
};

#endif // INCLUDED_SFX2_SOURCE_INC_INPUTDLG_HXX
diff --git a/include/sfx2/strings.hrc b/include/sfx2/strings.hrc
index c5d3c00..0967a31 100644
--- a/include/sfx2/strings.hrc
+++ b/include/sfx2/strings.hrc
@@ -70,6 +70,7 @@
#define STR_MSG_ERROR_IMPORT                    NC_("STR_MSG_ERROR_IMPORT", "Error importing the following templates to $1:\n$2")
#define STR_MSG_ERROR_DELETE_TEMPLATE           NC_("STR_MSG_ERROR_DELETE_TEMPLATE", "The following templates cannot be deleted:\n$1")
#define STR_MSG_ERROR_DELETE_FOLDER             NC_("STR_MSG_ERROR_DELETE_FOLDER", "The following folders cannot be deleted:\n$1")
#define STR_TOOLTIP_ERROR_RENAME_TEMPLATE       NC_("STR_TOOLTIP_ERROR_RENAME_TEMPLATE", "There is another template with the name $1 in $2.")
#define STR_QMSG_SEL_FOLDER_DELETE              NC_("STR_QMSG_SEL_FOLDER_DELETE", "Do you want to delete the selected category?")
#define STR_QMSG_TEMPLATE_OVERWRITE             NC_("STR_QMSG_TEMPLATE_OVERWRITE", "A template named $1 already exists in $2. Do you want to overwrite it?")
#define STR_QMSG_SEL_TEMPLATE_DELETE            NC_("STR_QMSG_SEL_TEMPLATE_DELETE", "Do you want to delete the selected templates?")
diff --git a/sfx2/source/control/templatedlglocalview.cxx b/sfx2/source/control/templatedlglocalview.cxx
index 0adc30d..d4a8f94 100644
--- a/sfx2/source/control/templatedlglocalview.cxx
+++ b/sfx2/source/control/templatedlglocalview.cxx
@@ -142,6 +142,24 @@ void TemplateDlgLocalView::ContextMenuSelectHdl(std::string_view rIdent)
        aTitleEditDlg.SetEntryText(sOldTitle);
        aTitleEditDlg.HideHelpBtn();

        auto aCurRegionItems = getFilteredItems([&](const TemplateItemProperties& rItem) {
            return rItem.aRegionName == getRegionName(maSelectedItem->mnRegionId);
        });
        OUString sTooltip(SfxResId(STR_TOOLTIP_ERROR_RENAME_TEMPLATE));
        sTooltip = sTooltip.replaceFirst("$2", getRegionName(maSelectedItem->mnRegionId));
        aTitleEditDlg.setCheckEntry([&](OUString sNewTitle) {
            if (sNewTitle.isEmpty() || sNewTitle == sOldTitle)
                return true;
            for (const auto& rItem : aCurRegionItems)
            {
                if (rItem.aName == sNewTitle)
                {
                    aTitleEditDlg.SetTooltip(sTooltip.replaceFirst("$1", sNewTitle));
                    return false;
                }
            }
            return true;
        });
        if (!aTitleEditDlg.run())
            return;
        OUString sNewTitle = comphelper::string::strip(aTitleEditDlg.GetEntryText(), ' ');
@@ -149,8 +167,8 @@ void TemplateDlgLocalView::ContextMenuSelectHdl(std::string_view rIdent)
        if (!sNewTitle.isEmpty() && sNewTitle != sOldTitle)
        {
            maSelectedItem->setTitle(sNewTitle);
            ListView::rename(OUString::number(maSelectedItem->mnId), maSelectedItem->maTitle);
        }
        ListView::rename(OUString::number(maSelectedItem->mnId), maSelectedItem->maTitle);
    }
    else if (rIdent == "delete")
    {
diff --git a/sfx2/source/control/templatelocalview.cxx b/sfx2/source/control/templatelocalview.cxx
index 4dcbaa3..baf630c 100644
--- a/sfx2/source/control/templatelocalview.cxx
+++ b/sfx2/source/control/templatelocalview.cxx
@@ -205,6 +205,24 @@ void TemplateLocalView::ContextMenuSelectHdl(std::string_view  rIdent)
        aTitleEditDlg.SetEntryText(sOldTitle);
        aTitleEditDlg.HideHelpBtn();

        auto aCurRegionItems = getFilteredItems([&](const TemplateItemProperties& rItem) {
            return rItem.aRegionName == getRegionName(maSelectedItem->mnRegionId);
        });
        OUString sTooltip(SfxResId(STR_TOOLTIP_ERROR_RENAME_TEMPLATE));
        sTooltip = sTooltip.replaceFirst("$2", getRegionName(maSelectedItem->mnRegionId));
        aTitleEditDlg.setCheckEntry([&](OUString sNewTitle) {
            if (sNewTitle.isEmpty() || sNewTitle == sOldTitle)
                return true;
            for (const auto& rItem : aCurRegionItems)
            {
                if (rItem.aName == sNewTitle)
                {
                    aTitleEditDlg.SetTooltip(sTooltip.replaceFirst("$1", sNewTitle));
                    return false;
                }
            }
            return true;
        });
        if (!aTitleEditDlg.run())
            return;
        OUString sNewTitle = comphelper::string::strip(aTitleEditDlg.GetEntryText(), ' ');
diff --git a/sfx2/source/dialog/inputdlg.cxx b/sfx2/source/dialog/inputdlg.cxx
index e2e5862..243f2fa 100644
--- a/sfx2/source/dialog/inputdlg.cxx
+++ b/sfx2/source/dialog/inputdlg.cxx
@@ -14,6 +14,7 @@ InputDialog::InputDialog(weld::Widget* pParent, const OUString& rLabelText)
    , m_xEntry(m_xBuilder->weld_entry("entry"))
    , m_xLabel(m_xBuilder->weld_label("label"))
    , m_xHelp(m_xBuilder->weld_button("help"))
    , m_xOk(m_xBuilder->weld_button("ok"))
{
    m_xLabel->set_label(rLabelText);
}
@@ -28,4 +29,39 @@ void InputDialog::SetEntryText(const OUString& rStr)
    m_xEntry->set_position(-1);
}

void InputDialog::SetEntryMessageType(weld::EntryMessageType aType)
{
    m_xEntry->set_message_type(aType);
    if (aType == weld::EntryMessageType::Error)
    {
        m_xEntry->select_region(0, -1);
        m_xEntry->grab_focus();
        m_xOk->set_sensitive(false);
    }
    else
    {
        m_xOk->set_sensitive(true);
        SetTooltip("");
    }
}

void InputDialog::SetTooltip(const OUString& rStr)
{
    m_xEntry->set_tooltip_text(rStr);
    m_xOk->set_tooltip_text(rStr);
}

void InputDialog::setCheckEntry(std::function<bool(OUString)> aFunc)
{
    mCheckEntry = aFunc;
    m_xEntry->connect_changed(LINK(this, InputDialog, EntryChangedHdl));
}

IMPL_LINK_NOARG(InputDialog, EntryChangedHdl, weld::Entry&, void)
{
    if (mCheckEntry(m_xEntry->get_text()))
        SetEntryMessageType(weld::EntryMessageType::Normal);
    else
        SetEntryMessageType(weld::EntryMessageType::Error);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */