tdf#116944 Warn user before database migration

Warn user with a pop-up dialog before migration.

To do that we have to know the database URL at UI level. In order to get
that I updated XDataSource interface with getConnectionURL().

The dialog offers two options: proceed with or without migration. If the
user choose "yes", we store that information in XDataSource. The
migration itself will be made in buildLowConnection().

Change-Id: I1f0d03da6352f7a0a8d989da79c4b2fe60a03ca1
Reviewed-on: https://gerrit.libreoffice.org/52876
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Tamás Bunth <btomi96@gmail.com>
diff --git a/dbaccess/Library_dbu.mk b/dbaccess/Library_dbu.mk
index 0ea5284..d1b5a53 100644
--- a/dbaccess/Library_dbu.mk
+++ b/dbaccess/Library_dbu.mk
@@ -152,6 +152,7 @@
    dbaccess/source/ui/dlg/textconnectionsettings \
    dbaccess/source/ui/dlg/UserAdmin \
    dbaccess/source/ui/dlg/UserAdminDlg \
    dbaccess/source/ui/dlg/migrwarndlg \
    dbaccess/source/ui/misc/asyncmodaldialog \
    dbaccess/source/ui/misc/charsets \
    dbaccess/source/ui/misc/controllerframe \
diff --git a/dbaccess/UIConfig_dbaccess.mk b/dbaccess/UIConfig_dbaccess.mk
index 4ac2e8a..eacb9eb 100644
--- a/dbaccess/UIConfig_dbaccess.mk
+++ b/dbaccess/UIConfig_dbaccess.mk
@@ -80,6 +80,7 @@
    dbaccess/uiconfig/ui/useradmindialog \
    dbaccess/uiconfig/ui/useradminpage \
    dbaccess/uiconfig/ui/userdetailspage \
    dbaccess/uiconfig/ui/migrwarndlg \
))

# vim: set noet sw=4 ts=4:
diff --git a/dbaccess/source/core/dataaccess/datasource.cxx b/dbaccess/source/core/dataaccess/datasource.cxx
index 9093cd6..d06413f 100644
--- a/dbaccess/source/core/dataaccess/datasource.cxx
+++ b/dbaccess/source/core/dataaccess/datasource.cxx
@@ -575,22 +575,24 @@
    m_pImpl.clear();
}

OUString SAL_CALL ODatabaseSource::getConnectionUrl()
{
    return m_pImpl->m_sConnectURL;
}

Reference< XConnection > ODatabaseSource::buildLowLevelConnection(const OUString& _rUid, const OUString& _rPwd)
{
    Reference< XConnection > xReturn;

    Reference< XDriverManager > xManager;

    bool bNeedMigration = false;
    OUString sMigrEnvVal;
    osl_getEnvironment(OUString("DBACCESS_HSQL_MIGRATION").pData,
            &sMigrEnvVal.pData);
    if( m_pImpl->m_sConnectURL == "sdbc:embedded:hsqldb" &&
            !sMigrEnvVal.isEmpty())
    {
    bool bNeedMigration =  m_pImpl->m_sConnectURL == "sdbc:embedded:hsqldb" &&
            (m_bMigationNeeded || !sMigrEnvVal.isEmpty());
    if(bNeedMigration)
        m_pImpl->m_sConnectURL = "sdbc:embedded:firebird";
        bNeedMigration = true;
    }

    try {
        xManager.set( ConnectionPool::create( m_pImpl->m_aContext ), UNO_QUERY_THROW );
diff --git a/dbaccess/source/core/dataaccess/datasource.hxx b/dbaccess/source/core/dataaccess/datasource.hxx
index 14328e1..a330ccc 100644
--- a/dbaccess/source/core/dataaccess/datasource.hxx
+++ b/dbaccess/source/core/dataaccess/datasource.hxx
@@ -84,6 +84,7 @@
    using ODatabaseSource_Base::rBHelper;
    // note: this thing uses the ref-count of "this", see OBookmarkContainer::acquire!
    OBookmarkContainer m_Bookmarks;
    bool m_bMigationNeeded = false;
    ::comphelper::OInterfaceContainerHelper2       m_aFlushListeners;

private:
@@ -162,6 +163,9 @@
    virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getConnection( const OUString& user, const OUString& password ) override;
    virtual void SAL_CALL setLoginTimeout( sal_Int32 seconds ) override;
    virtual sal_Int32 SAL_CALL getLoginTimeout(  ) override;
    virtual void SAL_CALL setMigrationNeeded( sal_Bool bNeeded ) override { m_bMigationNeeded = bNeeded; }
    virtual sal_Bool SAL_CALL getMigrationNeeded() override { return m_bMigationNeeded; }
    virtual OUString SAL_CALL getConnectionUrl() override;

//::css::sdb::XBookmarksSupplier
    virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getBookmarks(  ) override;
diff --git a/dbaccess/source/core/inc/warndlg.hxx b/dbaccess/source/core/inc/warndlg.hxx
new file mode 100644
index 0000000..54cfe88
--- /dev/null
+++ b/dbaccess/source/core/inc/warndlg.hxx
@@ -0,0 +1,29 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#ifndef INCLUDED_DBACCESS_SOURCE_CORE_INC_WARNDLG_HXX
#define INCLUDED_DBACCESS_SOURCE_CORE_INC_WARNDLG_HXX

#include <vcl/weld.hxx>

namespace dbaccess
{
class MigrationWarnDialog : public weld::MessageDialogController
{
private:
    std::unique_ptr<weld::Button> m_xOkBtn;
    std::unique_ptr<weld::Button> m_xLaterBtn;

public:
    MigrationWarnDialog(weld::Window* pParent);
};
}

#endif // INCLUDED_DBACCESS_SOURCE_CORE_INC_WARNDLG_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/dbaccess/source/core/misc/warndlg.cxx b/dbaccess/source/core/misc/warndlg.cxx
new file mode 100644
index 0000000..e8446b6
--- /dev/null
+++ b/dbaccess/source/core/misc/warndlg.cxx
@@ -0,0 +1,22 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <warndlg.hxx>

namespace dbaccess
{
MigrationWarnDialog::MigrationWarnDialog(weld::Window* pParent)
    : MessageDialogController(pParent, "TODOUIfile", "MigrationWarnDialog", "ask")
    , m_xOkBtn(m_xBuilder->weld_button("yes"))
    , m_xLaterBtn(m_xBuilder->weld_button("later"))
{
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/dbaccess/source/ui/dlg/migrwarndlg.cxx b/dbaccess/source/ui/dlg/migrwarndlg.cxx
new file mode 100644
index 0000000..9281dc1
--- /dev/null
+++ b/dbaccess/source/ui/dlg/migrwarndlg.cxx
@@ -0,0 +1,22 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <migrwarndlg.hxx>

namespace dbaui
{
MigrationWarnDialog::MigrationWarnDialog(weld::Window* pParent)
    : MessageDialogController(pParent, "dbaccess/ui/migrwarndlg.ui", "MigrationWarnDialog")
    , m_xOkBtn(m_xBuilder->weld_button("yes"))
    , m_xLaterBtn(m_xBuilder->weld_button("later"))
{
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/dbaccess/source/ui/inc/migrwarndlg.hxx b/dbaccess/source/ui/inc/migrwarndlg.hxx
new file mode 100644
index 0000000..7f18ad0
--- /dev/null
+++ b/dbaccess/source/ui/inc/migrwarndlg.hxx
@@ -0,0 +1,29 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#ifndef INCLUDED_DBACCESS_SOURCE_CORE_INC_WARNDLG_HXX
#define INCLUDED_DBACCESS_SOURCE_CORE_INC_WARNDLG_HXX

#include <vcl/weld.hxx>

namespace dbaui
{
class MigrationWarnDialog : public weld::MessageDialogController
{
private:
    std::unique_ptr<weld::Button> m_xOkBtn;
    std::unique_ptr<weld::Button> m_xLaterBtn;

public:
    MigrationWarnDialog(weld::Window* pParent);
};
}

#endif // INCLUDED_DBACCESS_SOURCE_CORE_INC_WARNDLG_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/dbaccess/source/ui/misc/datasourceconnector.cxx b/dbaccess/source/ui/misc/datasourceconnector.cxx
index 0496a4e..434f000 100644
--- a/dbaccess/source/ui/misc/datasourceconnector.cxx
+++ b/dbaccess/source/ui/misc/datasourceconnector.cxx
@@ -41,6 +41,7 @@
#include <cppuhelper/exc_hlp.hxx>
#include <strings.hrc>
#include <strings.hxx>
#include <migrwarndlg.hxx>

namespace dbaui
{
@@ -114,6 +115,12 @@
            DBG_UNHANDLED_EXCEPTION("dbaccess");
        }

        if(_xDataSource->getConnectionUrl().startsWithIgnoreAsciiCase("sdbc:embedded:hsqldb"))
        {
            MigrationWarnDialog aWarnDlg{m_pErrorMessageParent->GetFrameWeld()};
            _xDataSource->setMigrationNeeded(aWarnDlg.run() == RET_OK);
        }

        // try to connect
        SQLExceptionInfo aInfo;
        try
diff --git a/dbaccess/uiconfig/ui/migrwarndlg.ui b/dbaccess/uiconfig/ui/migrwarndlg.ui
new file mode 100644
index 0000000..8fa8e49
--- /dev/null
+++ b/dbaccess/uiconfig/ui/migrwarndlg.ui
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.2 -->
<interface domain="sfx">
  <requires lib="gtk+" version="3.18"/>
  <object class="GtkMessageDialog" id="MigrationWarnDialog">
    <property name="can_focus">False</property>
    <property name="type_hint">dialog</property>
    <property name="title" translatable="yes" context="migrationwarndialog|MigrationWarnDialog">Confirm Migration</property>
    <property name="resizable">False</property>
    <property name="modal">True</property>
    <child internal-child="vbox">
      <object class="GtkBox">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
            <child>
              <object class="GtkButton" id="yes">
                <property name="label" translatable="yes" context="migrationwarndialog|yes">yes</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="later">
                <property name="label" translatable="yes" context="migrationwarndialog|later">later</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes" context="migrationwarndialog|dialogmessage">The document contains embedded HSQL data, which is deprecated.
Would you like to migrate to Firebird now?</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
    <action-widgets>
      <action-widget response="-5">yes</action-widget>
      <action-widget response="-6">later</action-widget>
    </action-widgets>
  </object>
</interface>
diff --git a/offapi/com/sun/star/sdbc/XDataSource.idl b/offapi/com/sun/star/sdbc/XDataSource.idl
index c169b89..f8b5bda 100644
--- a/offapi/com/sun/star/sdbc/XDataSource.idl
+++ b/offapi/com/sun/star/sdbc/XDataSource.idl
@@ -34,6 +34,11 @@
 */
published interface XDataSource: com::sun::star::uno::XInterface
{
    /** indicates whether database migration is needed or not.
    */
    [attribute] boolean MigrationNeeded;

    string getConnectionUrl();

    /** attempts to establish a database connection.
        @param user
diff --git a/offapi/type_reference/offapi.idl b/offapi/type_reference/offapi.idl
index eb20847..99ad946 100644
--- a/offapi/type_reference/offapi.idl
+++ b/offapi/type_reference/offapi.idl
@@ -10212,6 +10212,8 @@
   };
   module sdbc {
    published interface XDataSource {
     [attribute] boolean MigrationNeeded;
     string getConnectionUrl();
     interface ::com::sun::star::uno::XInterface;
     ::com::sun::star::sdbc::XConnection getConnection([in] string user, [in] string password) raises (::com::sun::star::sdbc::SQLException);
     void setLoginTimeout([in] long seconds) raises (::com::sun::star::sdbc::SQLException);