summaryrefslogtreecommitdiffstats
path: root/dbaccess/source/ui/dlg/adtabdlg.cxx
blob: 809b483cd19a21e820db0211bca528a7228d21d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <adtabdlg.hxx>
#include <tools/diagnose_ex.h>
#include <core_resource.hxx>
#include <strings.hrc>
#include <connectivity/dbtools.hxx>
#include <com/sun/star/container/XContainer.hpp>
#include <com/sun/star/sdb/application/DatabaseObject.hpp>
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#include <com/sun/star/sdbcx/XViewsSupplier.hpp>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <imageprovider.hxx>
#include <comphelper/containermultiplexer.hxx>
#include <cppuhelper/basemutex.hxx>
#include <algorithm>

// slot ids
using namespace dbaui;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;
using namespace dbtools;

TableObjectListFacade::~TableObjectListFacade()
{
}

namespace {

class TableListFacade : public ::cppu::BaseMutex
                    ,   public TableObjectListFacade
                    ,   public ::comphelper::OContainerListener
{
    OTableTreeListBox&           m_rTableList;
    Reference< XConnection >    m_xConnection;
    ::rtl::Reference< comphelper::OContainerListenerAdapter>
                                m_pContainerListener;
    bool                        m_bAllowViews;

public:
    TableListFacade(OTableTreeListBox& _rTableList, const Reference< XConnection >& _rxConnection)
        : ::comphelper::OContainerListener(m_aMutex)
        ,m_rTableList( _rTableList )
        ,m_xConnection( _rxConnection )
        ,m_bAllowViews(true)
    {
    }
    virtual ~TableListFacade() override;

private:
    virtual void    updateTableObjectList( bool _bAllowViews ) override;
    virtual OUString  getSelectedName( OUString& _out_rAliasName ) const override;
    virtual bool    isLeafSelected() const override;
    // OContainerListener
    virtual void _elementInserted( const css::container::ContainerEvent& _rEvent ) override;
    virtual void _elementRemoved( const  css::container::ContainerEvent& _rEvent ) override;
    virtual void _elementReplaced( const css::container::ContainerEvent& _rEvent ) override;
};

}

TableListFacade::~TableListFacade()
{
    if ( m_pContainerListener.is() )
        m_pContainerListener->dispose();
}

OUString TableListFacade::getSelectedName( OUString& _out_rAliasName ) const
{
    weld::TreeView& rTableList = m_rTableList.GetWidget();
    std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());

    if (!rTableList.get_selected(xEntry.get()))
        return OUString();

    OUString aCatalog, aSchema, aTableName;
    std::unique_ptr<weld::TreeIter> xSchema(rTableList.make_iterator(xEntry.get()));
    if (rTableList.iter_parent(*xSchema))
    {
        auto xAll = m_rTableList.getAllObjectsEntry();
        if (!xAll || !xSchema->equal(*xAll))
        {
            std::unique_ptr<weld::TreeIter> xCatalog(rTableList.make_iterator(xSchema.get()));
            if (rTableList.iter_parent(*xCatalog))
            {
                if (!xAll || !xCatalog->equal(*xAll))
                    aCatalog = rTableList.get_text(*xCatalog, 0);
            }
            aSchema = rTableList.get_text(*xSchema, 0);
        }
    }
    aTableName = rTableList.get_text(*xEntry, 0);

    OUString aComposedName;
    try
    {
        Reference< XDatabaseMetaData > xMeta( m_xConnection->getMetaData(), UNO_SET_THROW );
        if (  aCatalog.isEmpty()
            && !aSchema.isEmpty()
            && xMeta->supportsCatalogsInDataManipulation()
            && !xMeta->supportsSchemasInDataManipulation() )
        {
            aCatalog = aSchema;
            aSchema.clear();
        }

        aComposedName = ::dbtools::composeTableName(
            xMeta, aCatalog, aSchema, aTableName, false, ::dbtools::EComposeRule::InDataManipulation );
    }
    catch ( const Exception& )
    {
        DBG_UNHANDLED_EXCEPTION("dbaccess");
    }

    _out_rAliasName = aTableName;
    return aComposedName;
}

void TableListFacade::_elementInserted( const container::ContainerEvent& /*_rEvent*/ )
{
    updateTableObjectList(m_bAllowViews);
}

void TableListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
{
    updateTableObjectList(m_bAllowViews);
}

void TableListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
{
}

void TableListFacade::updateTableObjectList( bool _bAllowViews )
{
    m_bAllowViews = _bAllowViews;
    weld::TreeView& rTableList = m_rTableList.GetWidget();
    rTableList.clear();
    try
    {
        Reference< XTablesSupplier > xTableSupp( m_xConnection, UNO_QUERY_THROW );

        Reference< XViewsSupplier > xViewSupp;
        Reference< XNameAccess > xTables, xViews;
        Sequence< OUString > sTables, sViews;

        xTables = xTableSupp->getTables();
        if ( xTables.is() )
        {
            if ( !m_pContainerListener.is() )
            {
                Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
                if ( xContainer.is() )
                    m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
            }
            sTables = xTables->getElementNames();
        }

        xViewSupp.set( xTableSupp, UNO_QUERY );
        if ( xViewSupp.is() )
        {
            xViews = xViewSupp->getViews();
            if ( xViews.is() )
                sViews = xViews->getElementNames();
        }

        // if no views are allowed remove the views also out the table name filter
        if ( !_bAllowViews )
        {
            const OUString* pTableBegin  = sTables.getConstArray();
            const OUString* pTableEnd    = pTableBegin + sTables.getLength();
            std::vector< OUString > aTables(pTableBegin,pTableEnd);

            const OUString* pViewBegin = sViews.getConstArray();
            const OUString* pViewEnd   = pViewBegin + sViews.getLength();
            ::comphelper::UStringMixEqual aEqualFunctor;
            for(;pViewBegin != pViewEnd;++pViewBegin)
                aTables.erase(std::remove_if(aTables.begin(),aTables.end(),
                                             [&aEqualFunctor, pViewBegin](const OUString& lhs)
                                             { return aEqualFunctor(lhs, *pViewBegin); } )
                              , aTables.end());
            sTables = Sequence< OUString>(aTables.data(), aTables.size());
            sViews = Sequence< OUString>();
        }

        m_rTableList.UpdateTableList( m_xConnection, sTables, sViews );

        std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
        bool bEntry = rTableList.get_iter_first(*xEntry);
        while (bEntry && rTableList.iter_has_child(*xEntry))
        {
            rTableList.expand_row(*xEntry);
            bEntry = rTableList.iter_next(*xEntry);
        }
        if (bEntry)
            rTableList.select(*xEntry);
    }
    catch( const Exception& )
    {
        DBG_UNHANDLED_EXCEPTION("dbaccess");
    }
}

bool TableListFacade::isLeafSelected() const
{
    weld::TreeView& rTableList = m_rTableList.GetWidget();
    std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
    const bool bEntry = rTableList.get_selected(xEntry.get());
    return bEntry && !rTableList.iter_has_child(*xEntry);
}

namespace {

class QueryListFacade : public ::cppu::BaseMutex
                    ,   public TableObjectListFacade
                    ,   public ::comphelper::OContainerListener
{
    weld::TreeView&             m_rQueryList;
    Reference< XConnection >    m_xConnection;
    ::rtl::Reference< comphelper::OContainerListenerAdapter>
                                m_pContainerListener;

public:
    QueryListFacade( weld::TreeView& _rQueryList, const Reference< XConnection >& _rxConnection )
        : ::comphelper::OContainerListener(m_aMutex)
        ,m_rQueryList( _rQueryList )
        ,m_xConnection( _rxConnection )
    {
    }
    virtual ~QueryListFacade() override;

private:
    virtual void    updateTableObjectList( bool _bAllowViews ) override;
    virtual OUString  getSelectedName( OUString& _out_rAliasName ) const override;
    virtual bool    isLeafSelected() const override;
    // OContainerListener
    virtual void _elementInserted( const css::container::ContainerEvent& _rEvent ) override;
    virtual void _elementRemoved( const  css::container::ContainerEvent& _rEvent ) override;
    virtual void _elementReplaced( const css::container::ContainerEvent& _rEvent ) override;
};

}

QueryListFacade::~QueryListFacade()
{
    if ( m_pContainerListener.is() )
        m_pContainerListener->dispose();
}

void QueryListFacade::_elementInserted( const container::ContainerEvent& _rEvent )
{
    OUString sName;
    if ( _rEvent.Accessor >>= sName )
    {
        OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));
        m_rQueryList.append("", sName, aQueryImage);
    }
}

void QueryListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
{
    updateTableObjectList(true);
}

void QueryListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
{
}

void QueryListFacade::updateTableObjectList( bool /*_bAllowViews*/ )
{
    m_rQueryList.clear();
    try
    {
        OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));

        Reference< XQueriesSupplier > xSuppQueries( m_xConnection, UNO_QUERY_THROW );
        Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_SET_THROW );
        if ( !m_pContainerListener.is() )
        {
            Reference< XContainer> xContainer(xQueries,UNO_QUERY_THROW);
            m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
        }
        const Sequence< OUString > aQueryNames = xQueries->getElementNames();

        for ( auto const & name : aQueryNames )
            m_rQueryList.append("", name, aQueryImage);
    }
    catch( const Exception& )
    {
        DBG_UNHANDLED_EXCEPTION("dbaccess");
    }
}

OUString QueryListFacade::getSelectedName( OUString& _out_rAliasName ) const
{
    OUString sSelected;
    std::unique_ptr<weld::TreeIter> xEntry(m_rQueryList.make_iterator());
    const bool bEntry = m_rQueryList.get_selected(xEntry.get());
    if (bEntry)
        sSelected = _out_rAliasName = m_rQueryList.get_text(*xEntry, 0);
    return sSelected;
}

bool QueryListFacade::isLeafSelected() const
{
    std::unique_ptr<weld::TreeIter> xEntry(m_rQueryList.make_iterator());
    const bool bEntry = m_rQueryList.get_selected(xEntry.get());
    return bEntry && !m_rQueryList.iter_has_child(*xEntry);

}

OAddTableDlg::OAddTableDlg(weld::Window* pParent, IAddTableDialogContext& _rContext)
   : GenericDialogController(pParent, "dbaccess/ui/tablesjoindialog.ui", "TablesJoinDialog")
   , m_rContext(_rContext)
   , m_xCaseTables(m_xBuilder->weld_radio_button("tables"))
   , m_xCaseQueries(m_xBuilder->weld_radio_button("queries"))
   // false means: do not show any buttons
   , m_xTableList(new OTableTreeListBox(m_xBuilder->weld_tree_view("tablelist"), false))
   , m_xQueryList(m_xBuilder->weld_tree_view("querylist"))
   , m_xAddButton(m_xBuilder->weld_button("add"))
   , m_xCloseButton(m_xBuilder->weld_button("close"))
{
    weld::TreeView& rTableList = m_xTableList->GetWidget();
    Size aSize(rTableList.get_approximate_digit_width() * 23,
               rTableList.get_height_rows(15));
    rTableList.set_size_request(aSize.Width(), aSize.Height());
    m_xQueryList->set_size_request(aSize.Width(), aSize.Height());

    m_xCaseTables->connect_toggled(LINK(this, OAddTableDlg, OnTypeSelected));
    m_xAddButton->connect_clicked( LINK( this, OAddTableDlg, AddClickHdl ) );
    m_xCloseButton->connect_clicked( LINK( this, OAddTableDlg, CloseClickHdl ) );
    rTableList.connect_row_activated( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
    rTableList.connect_changed( LINK( this, OAddTableDlg, TableListSelectHdl ) );
    m_xQueryList->connect_row_activated( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
    m_xQueryList->connect_changed( LINK( this, OAddTableDlg, TableListSelectHdl ) );

    rTableList.set_selection_mode(SelectionMode::Single);
    m_xTableList->SuppressEmptyFolders();

    m_xQueryList->set_selection_mode(SelectionMode::Single);

    if ( !m_rContext.allowQueries() )
    {
        m_xCaseTables->hide();
        m_xCaseQueries->hide();
    }

    m_xDialog->set_title(getDialogTitleForContext(m_rContext));
}

OAddTableDlg::~OAddTableDlg()
{
}

void OAddTableDlg::impl_switchTo( ObjectList _eList )
{
    switch ( _eList )
    {
    case Tables:
        m_xTableList->GetWidget().show(); m_xCaseTables->set_active(true);
        m_xQueryList->hide(); m_xCaseQueries->set_active(false);
        m_xCurrentList.reset( new TableListFacade( *m_xTableList, m_rContext.getConnection() ) );
        m_xTableList->GetWidget().grab_focus();
        break;

    case Queries:
        m_xTableList->GetWidget().hide(); m_xCaseTables->set_active(false);
        m_xQueryList->show();  m_xCaseQueries->set_active(true);
        m_xCurrentList.reset( new QueryListFacade( *m_xQueryList, m_rContext.getConnection() ) );
        m_xQueryList->grab_focus();
        break;
    }
    m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
}

void OAddTableDlg::Update()
{
    if (!m_xCurrentList)
        impl_switchTo( Tables );
    else
        m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
}

IMPL_LINK_NOARG( OAddTableDlg, AddClickHdl, weld::Button&, void )
{
    TableListDoubleClickHdl(m_xTableList->GetWidget());
}

IMPL_LINK_NOARG(OAddTableDlg, TableListDoubleClickHdl, weld::TreeView&, bool)
{
    if ( impl_isAddAllowed() )
    {
        if ( m_xCurrentList->isLeafSelected() )
        {
            OUString sSelectedName, sAliasName;
            sSelectedName = m_xCurrentList->getSelectedName( sAliasName );

            m_rContext.addTableWindow( sSelectedName, sAliasName );
        }
        if ( !impl_isAddAllowed() )
            m_xDialog->response(RET_CLOSE);
    }
    return true;
}

IMPL_LINK_NOARG( OAddTableDlg, TableListSelectHdl, weld::TreeView&, void )
{
    m_xAddButton->set_sensitive( m_xCurrentList->isLeafSelected() );
}

IMPL_LINK_NOARG( OAddTableDlg, CloseClickHdl, weld::Button&, void )
{
    m_xDialog->response(RET_CLOSE);
}

IMPL_LINK_NOARG(OAddTableDlg, OnTypeSelected, weld::Toggleable&, void)
{
    if ( m_xCaseTables->get_active() )
        impl_switchTo( Tables );
    else
        impl_switchTo( Queries );
}

void OAddTableDlg::OnClose()
{
    m_rContext.onWindowClosing();
}

bool OAddTableDlg::impl_isAddAllowed()
{
    return  m_rContext.allowAddition();
}

OUString OAddTableDlg::getDialogTitleForContext( IAddTableDialogContext const & _rContext )
{
    OUString sTitle;

    if ( _rContext.allowQueries() )
        sTitle = DBA_RES( STR_ADD_TABLE_OR_QUERY );
    else
        sTitle = DBA_RES( STR_ADD_TABLES );

    return sTitle;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */