tdf#152266 add an infobar with indicators for macro-like content in doc

Show "macros" and "events" for now if we know that are present so
they can be investigated by the user. There are other things which
could potentially be added in the future.

Change-Id: I981ee7a8e22791cd15405894f30fee659ba0b7ba
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143897
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/basctl/source/basicide/basides1.cxx b/basctl/source/basicide/basides1.cxx
index fd0fb7a..430b795 100644
--- a/basctl/source/basicide/basides1.cxx
+++ b/basctl/source/basicide/basides1.cxx
@@ -340,10 +340,10 @@ void Shell::ExecuteGlobal( SfxRequest& rReq )
            if ( rReq.GetArgs() )
            {
                const SfxUInt16Item &rTabId = rReq.GetArgs()->Get(SID_BASICIDE_ARG_TABID );
                Organize(rReq.GetFrameWeld(), rTabId.GetValue());
                Organize(rReq.GetFrameWeld(), nullptr, rTabId.GetValue());
            }
            else
                Organize(rReq.GetFrameWeld(), 0);
                Organize(rReq.GetFrameWeld(), nullptr, 0);
        }
        break;
        case SID_BASICIDE_CHOOSEMACRO:
diff --git a/basctl/source/basicide/basobj2.cxx b/basctl/source/basicide/basobj2.cxx
index d145d14..708b1ce 100644
--- a/basctl/source/basicide/basobj2.cxx
+++ b/basctl/source/basicide/basobj2.cxx
@@ -62,18 +62,19 @@ extern "C" {

        return pScriptURL;
    }
    SAL_DLLPUBLIC_EXPORT void basicide_macro_organizer(void *pParent, sal_Int16 nTabId)
    SAL_DLLPUBLIC_EXPORT void basicide_macro_organizer(void *pParent, void* pDocFrame_AsXFrame, sal_Int16 nTabId)
    {
        SAL_INFO("basctl.basicide","in basicide_macro_organizer");
        basctl::Organize(static_cast<weld::Window*>(pParent), nTabId);
        Reference< frame::XFrame > aDocFrame( static_cast< frame::XFrame* >( pDocFrame_AsXFrame ) );
        basctl::Organize(static_cast<weld::Window*>(pParent), aDocFrame, nTabId);
    }
}

void Organize(weld::Window* pParent, sal_Int16 tabId)
void Organize(weld::Window* pParent, const css::uno::Reference<css::frame::XFrame>& xDocFrame, sal_Int16 tabId)
{
    EnsureIde();

    auto xDlg(std::make_shared<OrganizeDialog>(pParent, tabId));
    auto xDlg(std::make_shared<OrganizeDialog>(pParent, xDocFrame, tabId));
    weld::DialogController::runAsync(xDlg, [](int) {});
}

diff --git a/basctl/source/basicide/macrodlg.cxx b/basctl/source/basicide/macrodlg.cxx
index db424be..b1097b9 100644
--- a/basctl/source/basicide/macrodlg.cxx
+++ b/basctl/source/basicide/macrodlg.cxx
@@ -742,7 +742,7 @@ IMPL_LINK(MacroChooser, ButtonHdl, weld::Button&, rButton, void)
        StoreMacroDescription();

        m_xBasicBox->get_selected(m_xBasicBoxIter.get());
        auto xDlg(std::make_shared<OrganizeDialog>(m_xDialog.get(), 0));
        auto xDlg(std::make_shared<OrganizeDialog>(m_xDialog.get(), nullptr, 0));
        weld::DialogController::runAsync(xDlg, [this](sal_Int32 nRet) {
            if (nRet == RET_OK) // not only closed
            {
diff --git a/basctl/source/basicide/moduldlg.cxx b/basctl/source/basicide/moduldlg.cxx
index 2525052..617d80e 100644
--- a/basctl/source/basicide/moduldlg.cxx
+++ b/basctl/source/basicide/moduldlg.cxx
@@ -31,6 +31,7 @@
#include <basic/basmgr.hxx>
#include <com/sun/star/script/XLibraryContainerPassword.hpp>
#include <com/sun/star/script/XLibraryContainer2.hpp>
#include <com/sun/star/frame/XController.hpp>
#include <comphelper/processfactory.hxx>
#include <sfx2/app.hxx>
#include <sfx2/dispatch.hxx>
@@ -182,8 +183,24 @@ void Shell::CopyDialogResources(
    io_xISP = ::xmlscript::exportDialogModel( xDialogModel, xContext, rDestDoc.isDocument() ? rDestDoc.getDocument() : Reference< frame::XModel >() );
}

void OrganizeDialog::SetCurrentEntry(const css::uno::Reference<css::frame::XFrame>& xDocFrame)
{
    if (!xDocFrame)
        return;
    Reference<css::frame::XController> xController(xDocFrame->getController());
    if (!xController)
        return;
    Reference<css::frame::XModel> xModel(xController->getModel());
    if (!xModel)
        return;
    ScriptDocument aScriptDocument(xModel);
    EntryDescriptor aDesc(aScriptDocument, LIBRARY_LOCATION_DOCUMENT, OUString(), OUString(), OUString(), OBJ_TYPE_DOCUMENT);
    m_xModulePage->SetCurrentEntry(aDesc);
    m_xDialogPage->SetCurrentEntry(aDesc);
}

// OrganizeDialog
OrganizeDialog::OrganizeDialog(weld::Window* pParent, sal_Int16 tabId )
OrganizeDialog::OrganizeDialog(weld::Window* pParent, const css::uno::Reference<css::frame::XFrame>& xDocFrame, sal_Int16 tabId)
    : GenericDialogController(pParent, "modules/BasicIDE/ui/organizedialog.ui", "OrganizeDialog")
    , m_xTabCtrl(m_xBuilder->weld_notebook("tabcontrol"))
    , m_xModulePage(new ObjectPage(m_xTabCtrl->get_page("modules"), "ModulePage", BrowseMode::Modules, this))
@@ -192,6 +209,8 @@ OrganizeDialog::OrganizeDialog(weld::Window* pParent, sal_Int16 tabId )
{
    m_xTabCtrl->connect_enter_page(LINK(this, OrganizeDialog, ActivatePageHdl));

    SetCurrentEntry(xDocFrame);

    OString sPage;
    if (tabId == 0)
        sPage = "modules";
diff --git a/basctl/source/basicide/moduldlg.hxx b/basctl/source/basicide/moduldlg.hxx
index 7dadebc..37e8567 100644
--- a/basctl/source/basicide/moduldlg.hxx
+++ b/basctl/source/basicide/moduldlg.hxx
@@ -150,6 +150,8 @@ public:
    ObjectPage(weld::Container* pParent, const OString& rName, BrowseMode nMode, OrganizeDialog* pDialog);
    virtual ~ObjectPage() override;

    void                SetCurrentEntry(const EntryDescriptor& rDesc) { m_xBasicBox->SetCurrentEntry(rDesc); }

    virtual void        ActivatePage() override;
};

@@ -206,8 +208,10 @@ private:

    DECL_LINK(ActivatePageHdl, const OString&, void);

    void SetCurrentEntry(const css::uno::Reference<css::frame::XFrame>& xDocFrame);

public:
    OrganizeDialog(weld::Window* pParent, sal_Int16 tabId);
    OrganizeDialog(weld::Window* pParent, const css::uno::Reference<css::frame::XFrame>& xDocFrame, sal_Int16 tabId);
    virtual ~OrganizeDialog() override;
};

diff --git a/basctl/source/inc/basobj.hxx b/basctl/source/inc/basobj.hxx
index 90cb82c..70c603d 100644
--- a/basctl/source/inc/basobj.hxx
+++ b/basctl/source/inc/basobj.hxx
@@ -32,8 +32,7 @@ namespace weld { class Widget; class Window; }

namespace basctl
{
    void            Organize(weld::Window* pParent, sal_Int16 tabId);

    void            Organize(weld::Window* pParent, const css::uno::Reference<css::frame::XFrame>& xDocFrame, sal_Int16 tabId);

    // help methods for the general use:
    SbMethod*       CreateMacro( SbModule* pModule, const OUString& rMacroName );
diff --git a/include/sfx2/app.hxx b/include/sfx2/app.hxx
index 6eff39b..73198c0 100644
--- a/include/sfx2/app.hxx
+++ b/include/sfx2/app.hxx
@@ -30,6 +30,7 @@

#include <sfx2/shell.hxx>

namespace com::sun::star::frame { class XFrame; }
namespace com::sun::star::script { class XLibraryContainer; }

namespace weld { class Window; }
@@ -138,7 +139,9 @@ public:
    // Basic/Scripting
    static bool                 IsXScriptURL( const OUString& rScriptURL );
    static OUString             ChooseScript(weld::Window *pParent);
    static void                 MacroOrganizer(weld::Window* pParent, sal_Int16 nTabId);
    // if xDocFrame is present, then select that document in the macro organizer by default, otherwise it is typically "Application Macros"
    // that is preselected
    static void                 MacroOrganizer(weld::Window* pParent, const css::uno::Reference<css::frame::XFrame>& xDocFrame, sal_Int16 nTabId);
    static ErrCode              CallBasic( const OUString&, BasicManager*, SbxArray *pArgs, SbxValue *pRet );
    static ErrCode              CallAppBasic( const OUString& i_macroName )
                                { return CallBasic( i_macroName, SfxApplication::GetBasicManager(), nullptr, nullptr ); }
diff --git a/include/sfx2/strings.hrc b/include/sfx2/strings.hrc
index be107f5..b7e2c1b 100644
--- a/include/sfx2/strings.hrc
+++ b/include/sfx2/strings.hrc
@@ -145,7 +145,7 @@
#define STR_QUERY_UPDATE_LINKS                  NC_("STR_QUERY_UPDATE_LINKS", "The document %{filename} contains one or more links to external data.\n\nWould you like to change the document, and update all links\nto get the most recent data?")
#define STR_DDE_ERROR                           NC_("STR_DDE_ERROR", "DDE link to %1 for %2 area %3 are not available.")
#define STR_SECURITY_WARNING_NO_HYPERLINKS      NC_("STR_SECURITY_WARNING_NO_HYPERLINKS", "For security reasons, the hyperlink cannot be executed.\nThe stated address will not be opened.")
#define RID_SECURITY_WARNING_TITLE                  NC_("RID_SECURITY_WARNING_TITLE", "Security Warning")
#define RID_SECURITY_WARNING_TITLE              NC_("RID_SECURITY_WARNING_TITLE", "Security Warning")
#define RID_SVXSTR_XMLSEC_QUERY_LOSINGSIGNATURE NC_("RID_SVXSTR_XMLSEC_QUERY_LOSINGSIGNATURE", "Saving will remove all existing signatures.\nDo you want to continue saving the document?")
#define RID_SVXSTR_XMLSEC_QUERY_SAVEBEFORESIGN  NC_("RID_SVXSTR_XMLSEC_QUERY_SAVEBEFORESIGN", "The document has to be saved before it can be signed.\nDo you want to save the document?")
#define STR_QUERY_CANCELCHECKOUT                NC_("STR_QUERY_CANCELCHECKOUT", "This will discard all changes on the server since check-out.\nDo you want to proceed?")
@@ -292,6 +292,9 @@
#define STR_HYPHENATION_BUTTON                  NC_("STR_HYPHENATION_BUTTON", "Learn more")
#define STR_REFRESH_MASTER_PASSWORD             NC_("STR_REFRESH_MASTER_PASSWORD", "The master password is stored in an outdated format, you should refresh it")
#define STR_REFRESH_PASSWORD                    NC_("STR_REFRESH_PASSWORD", "Refresh Password")
#define STR_CONTAINS_MACROS                     NC_("STR_CONTAINS_MACROS", "The document contains macros.")
#define STR_MACROS                              NC_("STR_MACROS", "Macros")
#define STR_EVENTS                              NC_("STR_EVENTS", "Events")

// Translators: default Impress template names
#define STR_TEMPLATE_NAME1                      NC_("STR_TEMPLATE_NAME1", "Grey Elegant")
diff --git a/include/sfx2/viewfrm.hxx b/include/sfx2/viewfrm.hxx
index a7bb8bd78..d8b15fa 100644
--- a/include/sfx2/viewfrm.hxx
+++ b/include/sfx2/viewfrm.hxx
@@ -64,6 +64,8 @@ class SFX2_DLLPUBLIC SfxViewFrame final : public SfxShell, public SfxListener
    DECL_DLLPRIVATE_LINK(GetInvolvedHandler, weld::Button&, void);
    DECL_DLLPRIVATE_LINK(DonationHandler, weld::Button&, void);
    DECL_DLLPRIVATE_LINK(WhatsNewHandler, weld::Button&, void);
    DECL_DLLPRIVATE_LINK(MacroButtonHandler, weld::Button&, void);
    DECL_DLLPRIVATE_LINK(EventButtonHandler, weld::Button&, void);
    DECL_DLLPRIVATE_LINK(SwitchReadOnlyHandler, weld::Button&, void);
    DECL_DLLPRIVATE_LINK(SignDocumentHandler, weld::Button&, void);
    DECL_DLLPRIVATE_LINK(HiddenTrackChangesHandler, weld::Button&, void);
@@ -83,6 +85,7 @@ private:
    /// SfxInterface initializer.
    static void InitInterface_Impl();

    void AppendContainsMacrosInfobar();
public:

    static void             SetViewFrame( SfxViewFrame* );
diff --git a/sfx2/sdi/sfx.sdi b/sfx2/sdi/sfx.sdi
index c56b80a..400746d 100644
--- a/sfx2/sdi/sfx.sdi
+++ b/sfx2/sdi/sfx.sdi
@@ -2341,7 +2341,7 @@ SfxScriptOrganizerItem ScriptOrganizer SID_SCRIPTORGANIZER


SfxScriptOrganizerItem MacroOrganizer SID_MACROORGANIZER
(SfxUInt16Item TabId SID_MACROORGANIZER)
(SfxUInt16Item TabId SID_MACROORGANIZER, SfxBoolItem CurrentDocument FN_PARAM_2)
[
    AutoUpdate = FALSE,
    FastCall = TRUE,
diff --git a/sfx2/source/appl/app.cxx b/sfx2/source/appl/app.cxx
index 760ab1ca..4b5adbd 100644
--- a/sfx2/source/appl/app.cxx
+++ b/sfx2/source/appl/app.cxx
@@ -383,12 +383,12 @@ void SfxApplication::Invalidate( sal_uInt16 nId )
#ifndef DISABLE_DYNLOADING

typedef long (*basicide_handle_basic_error)(void const *);
typedef void (*basicide_macro_organizer)(void *, sal_Int16);
typedef void (*basicide_macro_organizer)(void *, void *, sal_Int16);

#else

extern "C" long basicide_handle_basic_error(void const*);
extern "C" void basicide_macro_organizer(void*, sal_Int16);
extern "C" void basicide_macro_organizer(void*, void*, sal_Int16);

#endif

@@ -500,7 +500,7 @@ SfxApplication::ChooseScript(weld::Window *pParent)
    return aScriptURL;
}

void SfxApplication::MacroOrganizer(weld::Window* pParent, sal_Int16 nTabId)
void SfxApplication::MacroOrganizer(weld::Window* pParent, const uno::Reference<frame::XFrame>& xDocFrame, sal_Int16 nTabId)
{
#if !HAVE_FEATURE_SCRIPTING
    (void) pParent;
@@ -511,11 +511,11 @@ void SfxApplication::MacroOrganizer(weld::Window* pParent, sal_Int16 nTabId)
    basicide_macro_organizer pSymbol = reinterpret_cast<basicide_macro_organizer>(sfx2::getBasctlFunction("basicide_macro_organizer"));

    // call basicide_macro_organizer in basctl
    pSymbol(pParent, nTabId);
    pSymbol(pParent, xDocFrame.get(), nTabId);

#else

    basicide_macro_organizer(pParent, nTabId);
    basicide_macro_organizer(pParent, xDocFrame.get(), nTabId);

#endif

diff --git a/sfx2/source/appl/appserv.cxx b/sfx2/source/appl/appserv.cxx
index d20d8c5..31d12cd 100644
--- a/sfx2/source/appl/appserv.cxx
+++ b/sfx2/source/appl/appserv.cxx
@@ -1544,14 +1544,21 @@ void SfxApplication::OfaExec_Impl( SfxRequest& rReq )
        {
            SAL_INFO("sfx.appl", "handling SID_MACROORGANIZER");
            const SfxItemSet* pArgs = rReq.GetArgs();
            const SfxUInt16Item* pItem;
            sal_Int16 nTabId = 0;
            if(pArgs && (pItem = pArgs->GetItemIfSet(SID_MACROORGANIZER, false) ))
            Reference <XFrame> xFrame;
            if (pArgs)
            {
                nTabId = pItem->GetValue();
                if (const SfxUInt16Item* pItem = pArgs->GetItemIfSet(SID_MACROORGANIZER, false))
                    nTabId = pItem->GetValue();
                if (const SfxBoolItem* pItem = rReq.GetArg<SfxBoolItem>(FN_PARAM_2))
                {
                    // if set then default to showing the macros of the document associated
                    // with this frame
                    if (pItem->GetValue())
                        xFrame = GetRequestFrame(rReq);
                }
            }

            SfxApplication::MacroOrganizer(rReq.GetFrameWeld(), nTabId);
            SfxApplication::MacroOrganizer(rReq.GetFrameWeld(), xFrame, nTabId);
            rReq.Done();
        }
        break;
diff --git a/sfx2/source/view/viewfrm.cxx b/sfx2/source/view/viewfrm.cxx
index 813c92d..68ea4a3 100644
--- a/sfx2/source/view/viewfrm.cxx
+++ b/sfx2/source/view/viewfrm.cxx
@@ -1283,6 +1283,63 @@ void SfxViewFrame::AppendReadOnlyInfobar()
    }
}

void SfxViewFrame::AppendContainsMacrosInfobar()
{
    auto pInfoBar = AppendInfoBar("macro", SfxResId(RID_SECURITY_WARNING_TITLE), SfxResId(STR_CONTAINS_MACROS), InfobarType::WARNING);
    if (!pInfoBar)
        return;

    SfxObjectShell_Impl* pObjImpl = m_xObjSh->Get_Impl();

    // what's the difference between pObjImpl->documentStorageHasMacros() and pObjImpl->aMacroMode.hasMacroLibrary() ?
    if (pObjImpl->aMacroMode.hasMacroLibrary())
    {
        weld::Button& rMacroButton = pInfoBar->addButton();
        rMacroButton.set_label(SfxResId(STR_MACROS));
        rMacroButton.connect_clicked(LINK(this, SfxViewFrame, MacroButtonHandler));
    }

    Reference<XModel> xModel = m_xObjSh->GetModel();
    uno::Reference<document::XEventsSupplier> xSupplier(xModel, uno::UNO_QUERY);
    bool bHasBoundConfigEvents(false);
    if (xSupplier.is())
    {
        css::uno::Reference<css::container::XNameReplace> xDocumentEvents = xSupplier->getEvents();

        Sequence<OUString> eventNames = xDocumentEvents->getElementNames();
        sal_Int32 nEventCount = eventNames.getLength();
        for (sal_Int32 nEvent = 0; nEvent < nEventCount; ++nEvent)
        {
            OUString url;
            try
            {
                Any aAny(xDocumentEvents->getByName(eventNames[nEvent]));
                Sequence<beans::PropertyValue> props;
                if (aAny >>= props)
                {
                    ::comphelper::NamedValueCollection aProps(props);
                    url = aProps.getOrDefault("Script", url);
                }
            }
            catch (const Exception&)
            {
            }
            if (!url.isEmpty())
            {
                bHasBoundConfigEvents = true;
                break;
            }
        }
    }

    if (bHasBoundConfigEvents)
    {
        weld::Button& rEventButton = pInfoBar->addButton();
        rEventButton.set_label(SfxResId(STR_EVENTS));
        rEventButton.connect_clicked(LINK(this, SfxViewFrame, EventButtonHandler));
    }
}

namespace
{
css::uno::Reference<css::frame::XLayoutManager> getLayoutManager(const SfxFrame& rFrame)
@@ -1472,17 +1529,22 @@ void SfxViewFrame::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
                    }
                }

                const bool bEmbedded = m_xObjSh->GetCreateMode() == SfxObjectCreateMode::EMBEDDED;

                // read-only infobar if necessary
                const SfxViewShell *pVSh;
                const SfxShell *pFSh;
                if ( m_xObjSh->IsReadOnly() &&
                    ! m_xObjSh->IsSecurityOptOpenReadOnly() &&
                    ( m_xObjSh->GetCreateMode() != SfxObjectCreateMode::EMBEDDED ||
                    ( !bEmbedded ||
                        (( pVSh = m_xObjSh->GetViewShell()) && (pFSh = pVSh->GetFormShell()) && pFSh->IsDesignMode())))
                {
                    AppendReadOnlyInfobar();
                }

                if (!bEmbedded && m_xObjSh->Get_Impl()->getCurrentMacroExecMode() == css::document::MacroExecMode::NEVER_EXECUTE)
                    AppendContainsMacrosInfobar();

                if (vcl::CommandInfoProvider::GetModuleIdentifier(GetFrame().GetFrameInterface()) == "com.sun.star.text.TextDocument")
                    sfx2::SfxNotebookBar::ReloadNotebookBar(u"modules/swriter/ui/");

@@ -1698,6 +1760,23 @@ IMPL_LINK_NOARG(SfxViewFrame, HyphenationMissingHandler, weld::Button&, void)
    RemoveInfoBar(u"hyphenationmissing");
}

IMPL_LINK_NOARG(SfxViewFrame, MacroButtonHandler, weld::Button&, void)
{
    // start with tab 0 displayed
    SfxUInt16Item aTabItem(SID_MACROORGANIZER, 0);
    SfxBoolItem aCurrentDocItem(FN_PARAM_2, true);
    SfxUnoFrameItem aDocFrame(SID_FILLFRAME, GetFrame().GetFrameInterface());
    GetDispatcher()->ExecuteList(SID_MACROORGANIZER, SfxCallMode::ASYNCHRON,
                                 { &aTabItem, &aCurrentDocItem }, { &aDocFrame });
}

IMPL_LINK_NOARG(SfxViewFrame, EventButtonHandler, weld::Button&, void)
{
    SfxUnoFrameItem aDocFrame(SID_FILLFRAME, GetFrame().GetFrameInterface());
    GetDispatcher()->ExecuteList(SID_CONFIGEVENT, SfxCallMode::ASYNCHRON,
                                 {}, { &aDocFrame });
}

IMPL_LINK_NOARG(SfxViewFrame, RefreshMasterPasswordHdl, weld::Button&, void)
{
    bool bChanged = false;