tdf#139906 Show warning message when data source is not avaible.

CurrentDatabaseDataSource config item holds a database name for a
specific file. When document has CurrentDatabaseDataSource config
item but LibreOffice doesn't have in registered databases we should
notify the user at load time and put a button to fix the problem.

Change-Id: Ia0a6fd53985fc2fb82ce37d3962b3f479c20a647
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113296
Tested-by: Jenkins
Reviewed-by: Gülşah Köse <gulsah.kose@collabora.com>
diff --git a/sw/inc/strings.hrc b/sw/inc/strings.hrc
index 3d28229..b4017a0 100644
--- a/sw/inc/strings.hrc
+++ b/sw/inc/strings.hrc
@@ -1402,6 +1402,9 @@
#define STR_AUTOMARK_NO                         NC_("createautomarkdialog|no", "No")

#define STR_WRAP_PANEL_CUSTOM_STR               NC_("sidebarwrap|customlabel", "Custom")
#define STR_DATASOURCE_NOT_AVAILABLE            NC_("STR_DATASOURCE_NOT_AVAILABLE", "Data source is not available. Mail merge wizard will not work properly.")
#define STR_EXCHANGE_DATABASE                   NC_("STR_EXCHANGE_DATABASE", "Exchange Database")


#endif

diff --git a/sw/inc/view.hxx b/sw/inc/view.hxx
index 2b7fe95..4232d31 100644
--- a/sw/inc/view.hxx
+++ b/sw/inc/view.hxx
@@ -555,6 +555,8 @@ public:
    // form control has been activated
    DECL_LINK( FormControlActivated, LinkParamNone*, void );

    DECL_LINK( ExchangeDatabaseHandler, weld::Button&, void);

    // edit links
    void            EditLinkDlg();
    void            AutoCaption(const sal_uInt16 nType, const SvGlobalName *pOleId = nullptr);
@@ -624,6 +626,10 @@ public:
    std::shared_ptr<SwMailMergeConfigItem> const & GetMailMergeConfigItem() const;
    std::shared_ptr<SwMailMergeConfigItem> EnsureMailMergeConfigItem(const SfxItemSet* pArgs = nullptr);

    OUString GetDataSourceName() const;
    static bool IsDataSourceAvailable(const OUString sDataSourceName);
    void AppendDataSourceInfobar();

    void ExecFormatPaintbrush(SfxRequest const &);
    void StateFormatPaintbrush(SfxItemSet &);

diff --git a/sw/source/uibase/uiview/view.cxx b/sw/source/uibase/uiview/view.cxx
index 97ae1d9..fc63c55 100644
--- a/sw/source/uibase/uiview/view.cxx
+++ b/sw/source/uibase/uiview/view.cxx
@@ -34,6 +34,8 @@
#include <sfx2/docfile.hxx>
#include <sfx2/objface.hxx>
#include <sfx2/request.hxx>
#include <sfx2/event.hxx>
#include <sfx2/infobar.hxx>
#include <svx/ruler.hxx>
#include <svx/srchdlg.hxx>
#include <svx/fmshell.hxx>
@@ -66,6 +68,7 @@
#include <gloshdl.hxx>
#include <usrpref.hxx>
#include <srcview.hxx>
#include <strings.hrc>
#include <doc.hxx>
#include <IDocumentUndoRedo.hxx>
#include <IDocumentSettingAccess.hxx>
@@ -84,6 +87,10 @@
#include <com/sun/star/frame/XLayoutManager.hpp>
#include <com/sun/star/scanner/ScannerContext.hpp>
#include <com/sun/star/scanner/XScannerManager2.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sdb/XDatabaseContext.hpp>
#include <com/sun/star/sdb/DatabaseContext.hpp>
#include <com/sun/star/sdbc/XDataSource.hpp>
#include <toolkit/helper/vclunohelper.hxx>
#include <sal/log.hxx>

@@ -105,6 +112,8 @@ using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::scanner;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdbc;

#define SWVIEWFLAGS SfxViewShellFlags::HAS_PRINTOPTIONS

@@ -203,6 +212,11 @@ IMPL_LINK_NOARG(SwView, FormControlActivated, LinkParamNone*, void)
    }
}

IMPL_LINK_NOARG(SwView, ExchangeDatabaseHandler, weld::Button&, void)
{
    GetDispatcher().Execute(FN_CHANGE_DBFIELD);
}

namespace
{
uno::Reference<frame::XLayoutManager> getLayoutManager(const SfxViewFrame& rViewFrame)
@@ -1629,7 +1643,25 @@ void SwView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
    }
    else
    {
        if (auto pSfxEventHint = dynamic_cast<const SfxEventHint*>(&rHint))
        {
            switch( pSfxEventHint->GetEventId() )
            {
                case SfxEventHintId::CreateDoc:
                case SfxEventHintId::OpenDoc:
                {
                    OUString sDataSourceName = GetDataSourceName();
                    if ( !sDataSourceName.isEmpty() && !IsDataSourceAvailable(sDataSourceName))
                        AppendDataSourceInfobar();
                }
                break;
                default:
                    break;
            }
        }

        SfxHintId nId = rHint.GetId();

        switch ( nId )
        {
            // sub shells will be destroyed by the
@@ -1710,7 +1742,6 @@ void SwView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
                    GetViewFrame()->GetBindings().Invalidate(aSlotRedLine);
                }
                break;

            default: break;
        }
    }
@@ -1873,6 +1904,38 @@ tools::Rectangle SwView::getLOKVisibleArea() const
        return tools::Rectangle();
}

OUString SwView::GetDataSourceName() const
{
    uno::Reference<lang::XMultiServiceFactory> xFactory(GetDocShell()->GetModel(), uno::UNO_QUERY);
    uno::Reference<beans::XPropertySet> xSettings(
        xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY);
    OUString sDataSourceName = "";
    xSettings->getPropertyValue("CurrentDatabaseDataSource") >>= sDataSourceName;

    return sDataSourceName;
}

bool SwView::IsDataSourceAvailable(const OUString sDataSourceName)
{
    uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
    Reference< XDatabaseContext> xDatabaseContext = DatabaseContext::create(xContext);

    return xDatabaseContext->hasByName(sDataSourceName);
}

void SwView::AppendDataSourceInfobar()
{
    auto pInfoBar = GetViewFrame()->AppendInfoBar("datasource", "",
                                  SwResId(STR_DATASOURCE_NOT_AVAILABLE),
                                  InfobarType::WARNING);
    if (!pInfoBar)
        return;

    weld::Button& rBtn = pInfoBar->addButton();
    rBtn.set_label(SwResId(STR_EXCHANGE_DATABASE));
    rBtn.connect_clicked(LINK(this, SwView, ExchangeDatabaseHandler));
}

namespace sw {

void InitPrintOptionsFromApplication(SwPrintData & o_rData, bool const bWeb)