Resolves tdf#149944 - UI issues with Base's User Administration dialog

Buttons for add, delete user and change password moved into a menu

Change-Id: Ibddda5269f0a8a9219538833b1f871b041dd5341
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137754
Tested-by: Jenkins
Reviewed-by: Heiko Tietze <heiko.tietze@documentfoundation.org>
diff --git a/dbaccess/inc/strings.hrc b/dbaccess/inc/strings.hrc
index ea57f4e..f8370a1 100644
--- a/dbaccess/inc/strings.hrc
+++ b/dbaccess/inc/strings.hrc
@@ -420,6 +420,9 @@
#define STR_EXCEPTION_INFO                          NC_("STR_EXCEPTION_INFO", "Information")
#define STR_EXCEPTION_DETAILS                       NC_("STR_EXCEPTION_DETAILS", "Details")

#define STR_ADD_USER                                NC_("STR_ADD_USER", "Add User")
#define STR_DELETE_USER                             NC_("STR_DELETE_USER", "Delete User")
#define STR_CHANGE_PASSWORD                         NC_("STR_CHANGE_PASSWORD", "Change Password")
#define STR_QUERY_USERADMIN_DELETE_USER             NC_("STR_QUERY_USERADMIN_DELETE_USER", "Do you really want to delete the user?")
#define STR_USERADMIN_NOT_AVAILABLE                 NC_("STR_USERADMIN_NOT_AVAILABLE", "The database does not support user administration." )
#define STR_ERROR_PASSWORDS_NOT_IDENTICAL           NC_("STR_ERROR_PASSWORDS_NOT_IDENTICAL", "The passwords do not match. Please enter the password again.")
diff --git a/dbaccess/source/ui/dlg/UserAdmin.cxx b/dbaccess/source/ui/dlg/UserAdmin.cxx
index b601c49..d7396dc 100644
--- a/dbaccess/source/ui/dlg/UserAdmin.cxx
+++ b/dbaccess/source/ui/dlg/UserAdmin.cxx
@@ -49,6 +49,10 @@ using namespace comphelper;

namespace {

#define MNI_ACTION_ADD_USER "add"
#define MNI_ACTION_DEL_USER "delete"
#define MNI_ACTION_CHANGE_PASSWORD "password"

class OPasswordDialog : public weld::GenericDialogController
{
    std::unique_ptr<weld::Frame> m_xUser;
@@ -111,20 +115,87 @@ IMPL_LINK(OPasswordDialog, ModifiedHdl, weld::Entry&, rEdit, void)
// OUserAdmin
OUserAdmin::OUserAdmin(weld::Container* pPage, weld::DialogController* pController,const SfxItemSet& _rAttrSet)
    : OGenericAdministrationPage(pPage, pController, "dbaccess/ui/useradminpage.ui", "UserAdminPage", _rAttrSet)
    , mxActionBar(m_xBuilder->weld_menu_button("action_menu"))
    , m_xUSER(m_xBuilder->weld_combo_box("user"))
    , m_xNEWUSER(m_xBuilder->weld_button("add"))
    , m_xCHANGEPWD(m_xBuilder->weld_button("changepass"))
    , m_xDELETEUSER(m_xBuilder->weld_button("delete"))
    , m_xTable(m_xBuilder->weld_container("table"))
    , m_xTableCtrlParent(m_xTable->CreateChildFrame())
    , m_xTableCtrl(VclPtr<OTableGrantControl>::Create(m_xTableCtrlParent))
{
    mxActionBar->append_item(MNI_ACTION_ADD_USER, DBA_RES(STR_ADD_USER));
    mxActionBar->append_item(MNI_ACTION_DEL_USER, DBA_RES(STR_DELETE_USER));
    mxActionBar->append_item(MNI_ACTION_CHANGE_PASSWORD, DBA_RES(STR_CHANGE_PASSWORD));
    mxActionBar->connect_selected(LINK(this,OUserAdmin,MenuSelectHdl));

    m_xTableCtrl->Show();

    m_xUSER->connect_changed(LINK(this, OUserAdmin, ListDblClickHdl));
    m_xNEWUSER->connect_clicked(LINK(this, OUserAdmin, UserHdl));
    m_xCHANGEPWD->connect_clicked(LINK(this, OUserAdmin, UserHdl));
    m_xDELETEUSER->connect_clicked(LINK(this, OUserAdmin, UserHdl));
}

IMPL_LINK(OUserAdmin, MenuSelectHdl, const OString&, rIdent, void)
{
    try
    {
        if (rIdent == MNI_ACTION_ADD_USER) {
            SfxPasswordDialog aPwdDlg(GetFrameWeld());
            aPwdDlg.ShowExtras(SfxShowExtras::ALL);
            if (aPwdDlg.run())
            {
                Reference<XDataDescriptorFactory> xUserFactory(m_xUsers,UNO_QUERY);
                Reference<XPropertySet> xNewUser = xUserFactory->createDataDescriptor();
                if(xNewUser.is())
                {
                    xNewUser->setPropertyValue(PROPERTY_NAME,Any(aPwdDlg.GetUser()));
                    xNewUser->setPropertyValue(PROPERTY_PASSWORD,Any(aPwdDlg.GetPassword()));
                    Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
                    if(xAppend.is())
                        xAppend->appendByDescriptor(xNewUser);
                }
            }
        }
        else if (rIdent == MNI_ACTION_DEL_USER) {
            if (m_xUsers.is() && m_xUsers->hasByName(GetUser()))
            {
                Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
                if(xDrop.is())
                {
                    std::unique_ptr<weld::MessageDialog> xQry(Application::CreateMessageDialog(GetFrameWeld(),
                                                            VclMessageType::Question, VclButtonsType::YesNo,
                                                            DBA_RES(STR_QUERY_USERADMIN_DELETE_USER)));
                    if (xQry->run() == RET_YES)
                        xDrop->dropByName(GetUser());
                }
            }
        }
        else if (rIdent == MNI_ACTION_CHANGE_PASSWORD) {
            OUString sName = GetUser();
            if(m_xUsers->hasByName(sName))
            {
                Reference<XUser> xUser;
                m_xUsers->getByName(sName) >>= xUser;
                if(xUser.is())
                {
                    OPasswordDialog aDlg(GetFrameWeld(), sName);
                    if (aDlg.run() == RET_OK)
                    {
                        OUString sNewPassword,sOldPassword;
                        sNewPassword = aDlg.GetNewPassword();
                        sOldPassword = aDlg.GetOldPassword();

                        if(!sNewPassword.isEmpty())
                            xUser->changePassword(sOldPassword,sNewPassword);
                    }
                }
            }
        }
        FillUserNames();
    }
    catch(const SQLException& e)
    {
        ::dbtools::showError(::dbtools::SQLExceptionInfo(e), GetDialogController()->getDialog()->GetXWindow(), m_xORB);
    }
    catch(Exception& )
    {
    }
}

OUserAdmin::~OUserAdmin()
@@ -173,11 +244,11 @@ void OUserAdmin::FillUserNames()
    }

    Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
    m_xNEWUSER->set_sensitive(xAppend.is());
    mxActionBar->set_item_sensitive(MNI_ACTION_ADD_USER, xAppend.is());
    Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
    m_xDELETEUSER->set_sensitive(xDrop.is());
    mxActionBar->set_item_sensitive(MNI_ACTION_DEL_USER, xDrop.is());
    mxActionBar->set_item_sensitive(MNI_ACTION_CHANGE_PASSWORD, m_xUsers.is());

    m_xCHANGEPWD->set_sensitive(m_xUsers.is());
    m_xTableCtrl->Enable(m_xUsers.is());
}

@@ -186,77 +257,6 @@ std::unique_ptr<SfxTabPage> OUserAdmin::Create( weld::Container* pPage, weld::Di
    return std::make_unique<OUserAdmin>( pPage, pController, *_rAttrSet );
}

IMPL_LINK(OUserAdmin, UserHdl, weld::Button&, rButton, void)
{
    try
    {
        if (&rButton == m_xNEWUSER.get())
        {
            SfxPasswordDialog aPwdDlg(GetFrameWeld());
            aPwdDlg.ShowExtras(SfxShowExtras::ALL);
            if (aPwdDlg.run())
            {
                Reference<XDataDescriptorFactory> xUserFactory(m_xUsers,UNO_QUERY);
                Reference<XPropertySet> xNewUser = xUserFactory->createDataDescriptor();
                if(xNewUser.is())
                {
                    xNewUser->setPropertyValue(PROPERTY_NAME,Any(aPwdDlg.GetUser()));
                    xNewUser->setPropertyValue(PROPERTY_PASSWORD,Any(aPwdDlg.GetPassword()));
                    Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
                    if(xAppend.is())
                        xAppend->appendByDescriptor(xNewUser);
                }
            }
        }
        else if (&rButton == m_xCHANGEPWD.get())
        {
            OUString sName = GetUser();

            if(m_xUsers->hasByName(sName))
            {
                Reference<XUser> xUser;
                m_xUsers->getByName(sName) >>= xUser;
                if(xUser.is())
                {
                    OPasswordDialog aDlg(GetFrameWeld(), sName);
                    if (aDlg.run() == RET_OK)
                    {
                        OUString sNewPassword,sOldPassword;
                        sNewPassword = aDlg.GetNewPassword();
                        sOldPassword = aDlg.GetOldPassword();

                        if(!sNewPassword.isEmpty())
                            xUser->changePassword(sOldPassword,sNewPassword);
                    }
                }
            }
        }
        else
        {// delete user
            if(m_xUsers.is() && m_xUsers->hasByName(GetUser()))
            {
                Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
                if(xDrop.is())
                {
                    std::unique_ptr<weld::MessageDialog> xQry(Application::CreateMessageDialog(GetFrameWeld(),
                                                              VclMessageType::Question, VclButtonsType::YesNo,
                                                              DBA_RES(STR_QUERY_USERADMIN_DELETE_USER)));
                    if (xQry->run() == RET_YES)
                        xDrop->dropByName(GetUser());
                }
            }
        }
        FillUserNames();
    }
    catch(const SQLException& e)
    {
        ::dbtools::showError(::dbtools::SQLExceptionInfo(e), GetDialogController()->getDialog()->GetXWindow(), m_xORB);
    }
    catch(Exception& )
    {
    }
}

IMPL_LINK_NOARG(OUserAdmin, ListDblClickHdl, weld::ComboBox&, void)
{
    m_xTableCtrl->setUserName(GetUser());
diff --git a/dbaccess/source/ui/dlg/UserAdmin.hxx b/dbaccess/source/ui/dlg/UserAdmin.hxx
index e9c2a13..045d8c3 100644
--- a/dbaccess/source/ui/dlg/UserAdmin.hxx
+++ b/dbaccess/source/ui/dlg/UserAdmin.hxx
@@ -32,10 +32,8 @@ namespace dbaui

class OUserAdmin final : public OGenericAdministrationPage
{
    std::unique_ptr<weld::MenuButton> mxActionBar;
    std::unique_ptr<weld::ComboBox> m_xUSER;
    std::unique_ptr<weld::Button> m_xNEWUSER;
    std::unique_ptr<weld::Button> m_xCHANGEPWD;
    std::unique_ptr<weld::Button> m_xDELETEUSER;
    std::unique_ptr<weld::Container> m_xTable;
    css::uno::Reference<css::awt::XWindow> m_xTableCtrlParent;
    VclPtr<OTableGrantControl> m_xTableCtrl; // show the grant rights of one user
@@ -48,7 +46,7 @@ class OUserAdmin final : public OGenericAdministrationPage

    // methods
    DECL_LINK(ListDblClickHdl, weld::ComboBox&, void);
    DECL_LINK(UserHdl, weld::Button&, void);
    DECL_LINK(MenuSelectHdl, const OString&, void);

    void        FillUserNames();

diff --git a/dbaccess/uiconfig/ui/useradminpage.ui b/dbaccess/uiconfig/ui/useradminpage.ui
index 4d649ea..e76349a 100644
--- a/dbaccess/uiconfig/ui/useradminpage.ui
+++ b/dbaccess/uiconfig/ui/useradminpage.ui
@@ -1,44 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<!-- Generated with glade 3.38.2 -->
<interface domain="dba">
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkMenu" id="menu">
    <property name="visible">True</property>
    <property name="can-focus">False</property>
  </object>
  <object class="GtkBox" id="UserAdminPage">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="can-focus">False</property>
    <property name="hexpand">True</property>
    <property name="vexpand">True</property>
    <property name="border_width">6</property>
    <property name="border-width">6</property>
    <property name="orientation">vertical</property>
    <property name="spacing">12</property>
    <child>
      <object class="GtkFrame" id="frame1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="can-focus">False</property>
        <property name="margin-start">6</property>
        <property name="margin-end">6</property>
        <property name="margin-top">6</property>
        <property name="hexpand">True</property>
        <property name="label_xalign">0</property>
        <property name="shadow_type">none</property>
        <property name="label-xalign">0</property>
        <property name="shadow-type">none</property>
        <child>
          <object class="GtkBox" id="box1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="can-focus">False</property>
            <property name="margin-top">6</property>
            <property name="hexpand">True</property>
            <property name="orientation">vertical</property>
            <property name="spacing">6</property>
            <property name="margin-top">6</property>
            <child>
              <object class="GtkBox" id="box2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="can-focus">False</property>
                <property name="hexpand">True</property>
                <property name="spacing">12</property>
                <child>
                  <object class="GtkLabel" id="label3">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="can-focus">False</property>
                    <property name="halign">start</property>
                    <property name="hexpand">True</property>
                    <property name="label" translatable="yes" context="useradminpage|label3">Us_er:</property>
                    <property name="use_underline">True</property>
                    <property name="use-underline">True</property>
                    <property name="xalign">0</property>
                  </object>
                  <packing>
@@ -50,7 +56,7 @@
                <child>
                  <object class="GtkComboBoxText" id="user">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="can-focus">False</property>
                    <property name="hexpand">True</property>
                  </object>
                  <packing>
@@ -59,54 +65,19 @@
                    <property name="position">1</property>
                  </packing>
                </child>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkBox" id="box3">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="hexpand">True</property>
                <property name="spacing">6</property>
                <child>
                  <object class="GtkButton" id="add">
                    <property name="label" translatable="yes" context="useradminpage|add">_Add User...</property>
                  <object class="GtkMenuButton" id="action_menu">
                    <property name="label" translatable="yes" context="templatedlg|action_menu|label">_Manage</property>
                    <property name="use-underline">True</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <property name="use_underline">True</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkButton" id="changepass">
                    <property name="label" translatable="yes" context="useradminpage|changepass">Change _Password...</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <property name="use_underline">True</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkButton" id="delete">
                    <property name="label" translatable="yes" context="useradminpage|delete">_Delete User...</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <property name="use_underline">True</property>
                    <property name="can-focus">True</property>
                    <property name="focus-on-click">False</property>
                    <property name="receives-default">True</property>
                    <property name="halign">end</property>
                    <property name="popup">menu</property>
                    <child>
                      <placeholder/>
                    </child>
                  </object>
                  <packing>
                    <property name="expand">False</property>
@@ -118,7 +89,7 @@
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
@@ -126,7 +97,7 @@
        <child type="label">
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes" context="useradminpage|label1">User Selection</property>
            <attributes>
              <attribute name="weight" value="bold"/>
@@ -143,20 +114,23 @@
    <child>
      <object class="GtkFrame" id="frame2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="can-focus">False</property>
        <property name="margin-start">6</property>
        <property name="margin-end">6</property>
        <property name="margin-bottom">6</property>
        <property name="hexpand">True</property>
        <property name="vexpand">True</property>
        <property name="label_xalign">0</property>
        <property name="shadow_type">none</property>
        <property name="label-xalign">0</property>
        <property name="shadow-type">none</property>
        <child>
          <object class="GtkBox" id="table">
            <property name="height-request">150</property>
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="margin-top">6</property>
            <property name="hexpand">True</property>
            <property name="vexpand">True</property>
            <property name="visible">True</property>
            <property name="height_request">150</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <property name="margin-top">6</property>
            <child>
              <placeholder/>
            </child>
@@ -165,7 +139,7 @@
        <child type="label">
          <object class="GtkLabel" id="label2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes" context="useradminpage|label2">Access Rights for Selected User</property>
            <attributes>
              <attribute name="weight" value="bold"/>