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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* -*- 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 <comphelper/types.hxx>
#include <comphelper/sequence.hxx>
#include "framectr.hxx"
#include "datman.hxx"
#include <toolkit/helper/vclunohelper.hxx>
#include "bibconfig.hxx"
#include <cppuhelper/implbase.hxx>
#include <vcl/event.hxx>
#include <vcl/svapp.hxx>
#include <comphelper/processfactory.hxx>
#include <com/sun/star/form/XConfirmDeleteListener.hpp>
#include <com/sun/star/form/runtime/XFormController.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/sdbcx/Privilege.hpp>
#include <com/sun/star/sdbc/XResultSetUpdate.hpp>
#include <com/sun/star/sdb/FilterDialog.hpp>
#include <com/sun/star/sdb/RowChangeAction.hpp>
#include <com/sun/star/frame/CommandGroup.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
#include <cppuhelper/interfacecontainer.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <sot/exchange.hxx>
#include <sot/formats.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/edit.hxx>
#include <vcl/weld.hxx>
#include <osl/mutex.hxx>

#include <unordered_map>

using namespace osl;
using namespace cppu;
using namespace com::sun::star::sdbc;
using namespace com::sun::star::frame;
using namespace com::sun::star::uno;
using namespace com::sun::star;

namespace {

struct DispatchInfo
{
    const char*   pCommand;
    sal_Int16     nGroupId;
    bool      bActiveConnection;
};

struct CacheDispatchInfo
{
    sal_Int16     nGroupId;
    bool      bActiveConnection;
};

}

// Attention: commands must be sorted by command groups. Implementation is dependent
// on this!!
static const DispatchInfo SupportedCommandsArray[] =
{
    { ".uno:Undo"               ,   frame::CommandGroup::EDIT       , false },
    { ".uno:Cut"                ,   frame::CommandGroup::EDIT       , false },
    { ".uno:Copy"               ,   frame::CommandGroup::EDIT       , false },
    { ".uno:Paste"              ,   frame::CommandGroup::EDIT       , false },
    { ".uno:SelectAll"          ,   frame::CommandGroup::EDIT       , false },
    { ".uno:CloseDoc"           ,   frame::CommandGroup::DOCUMENT   , false },
    { ".uno:StatusBarVisible"   ,   frame::CommandGroup::VIEW       , false },
    { ".uno:AvailableToolbars"  ,   frame::CommandGroup::VIEW       , false },
    { ".uno:Bib/standardFilter" ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/DeleteRecord"   ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/InsertRecord"   ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/query"          ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/autoFilter"     ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/source"         ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/removeFilter"   ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/sdbsource"      ,   frame::CommandGroup::DATA       , true  },
    { ".uno:Bib/Mapping"        ,   frame::CommandGroup::DATA       , true  },
};

typedef std::unordered_map< OUString, CacheDispatchInfo > CmdToInfoCache;

static const CmdToInfoCache& GetCommandToInfoCache()
{
    static CmdToInfoCache aCmdToInfoCache = []() {
        CmdToInfoCache aCache;
        for (const auto& command : SupportedCommandsArray)
        {
            OUString aCommand(OUString::createFromAscii(command.pCommand));

            CacheDispatchInfo aDispatchInfo;
            aDispatchInfo.nGroupId = command.nGroupId;
            aDispatchInfo.bActiveConnection = command.bActiveConnection;
            aCache.emplace(aCommand, aDispatchInfo);
        }
        return aCache;
    }();

    return aCmdToInfoCache;
}


class BibFrameCtrl_Impl : public cppu::WeakImplHelper < XFrameActionListener >
{
public:
    Mutex                               aMutex;
    OMultiTypeInterfaceContainerHelper  aLC;

    BibFrameController_Impl*            pController;

                                        BibFrameCtrl_Impl()
                                            : aLC( aMutex )
                                            , pController(nullptr)
                                        {}

    virtual void                        SAL_CALL frameAction(const FrameActionEvent& aEvent) override;
    virtual void                        SAL_CALL disposing( const lang::EventObject& Source ) override;
};

void BibFrameCtrl_Impl::frameAction(const FrameActionEvent& )
{
}

void BibFrameCtrl_Impl::disposing( const lang::EventObject& /*Source*/ )
{
    ::SolarMutexGuard aGuard;
    if ( pController )
        pController->getFrame()->removeFrameActionListener( this );
}

BibFrameController_Impl::BibFrameController_Impl( const uno::Reference< awt::XWindow > & xComponent,
                                                BibDataManager* pDataManager)
    :xWindow( xComponent )
    ,m_xDatMan( pDataManager )
{
    bDisposing=false;
    mxImpl = new BibFrameCtrl_Impl;
    mxImpl->pController = this;
}

BibFrameController_Impl::~BibFrameController_Impl()
{
    mxImpl->pController = nullptr;
    m_xDatMan.clear();
}

OUString SAL_CALL BibFrameController_Impl::getImplementationName()
{
    return "com.sun.star.comp.extensions.Bibliography";
}

sal_Bool SAL_CALL BibFrameController_Impl::supportsService( const OUString& sServiceName )
{
    return cppu::supportsService( this, sServiceName );
}

css::uno::Sequence< OUString > SAL_CALL BibFrameController_Impl::getSupportedServiceNames()
{
    // return only top level services ...
    // base services are included there and should be asked by uno-rtti.
    return { "com.sun.star.frame.Bibliography" };
}

void BibFrameController_Impl::attachFrame( const uno::Reference< XFrame > & xArg )
{
    xFrame = xArg;
    xFrame->addFrameActionListener( mxImpl.get() );
}

sal_Bool BibFrameController_Impl::attachModel( const uno::Reference< XModel > & /*xModel*/ )
{
    return false;
}

sal_Bool BibFrameController_Impl::suspend( sal_Bool bSuspend )
{
    if ( bSuspend )
        getFrame()->removeFrameActionListener( mxImpl.get() );
    else
        getFrame()->addFrameActionListener( mxImpl.get() );
    return true;
}

uno::Any BibFrameController_Impl::getViewData()
{
    return uno::Any();
}

void BibFrameController_Impl::restoreViewData( const uno::Any& /*Value*/ )
{
}

uno::Reference< XFrame >  BibFrameController_Impl::getFrame()
{
    return xFrame;
}

uno::Reference< XModel >  BibFrameController_Impl::getModel()
{
    return uno::Reference< XModel > ();
}

void BibFrameController_Impl::dispose()
{
    bDisposing = true;
    lang::EventObject aObject;
    aObject.Source = static_cast<XController*>(this);
    mxImpl->aLC.disposeAndClear(aObject);
    m_xDatMan.clear();
    aStatusListeners.clear();
 }

void BibFrameController_Impl::addEventListener( const uno::Reference< lang::XEventListener > & aListener )
{
    mxImpl->aLC.addInterface( cppu::UnoType<lang::XEventListener>::get(), aListener );
}

void BibFrameController_Impl::removeEventListener( const uno::Reference< lang::XEventListener > & aListener )
{
    mxImpl->aLC.removeInterface( cppu::UnoType<lang::XEventListener>::get(), aListener );
}

uno::Reference< frame::XDispatch >  BibFrameController_Impl::queryDispatch( const util::URL& aURL, const OUString& /*aTarget*/, sal_Int32 /*nSearchFlags*/ )
{
    if ( !bDisposing )
    {
        const CmdToInfoCache& rCmdCache = GetCommandToInfoCache();
        CmdToInfoCache::const_iterator pIter = rCmdCache.find( aURL.Complete );
        if ( pIter != rCmdCache.end() )
        {
            if (( m_xDatMan->HasActiveConnection() ) ||
                ( !pIter->second.bActiveConnection ))
                return static_cast<frame::XDispatch*>(this);
        }
    }

    return uno::Reference< frame::XDispatch > ();
}

uno::Sequence<uno::Reference< XDispatch > > BibFrameController_Impl::queryDispatches( const uno::Sequence<DispatchDescriptor>& aDescripts )
{
    uno::Sequence< uno::Reference< XDispatch > > aDispatches( aDescripts.getLength() );
    for ( sal_Int32 i=0; i<aDescripts.getLength(); ++i )
        aDispatches[i] = queryDispatch( aDescripts[i].FeatureURL, aDescripts[i].FrameName, aDescripts[i].SearchFlags );
    return aDispatches;
}

uno::Sequence< ::sal_Int16 > SAL_CALL BibFrameController_Impl::getSupportedCommandGroups()
{
    uno::Sequence< ::sal_Int16 > aDispatchInfo( 4 );

    aDispatchInfo[0] = frame::CommandGroup::EDIT;
    aDispatchInfo[1] = frame::CommandGroup::DOCUMENT;
    aDispatchInfo[2] = frame::CommandGroup::DATA;
    aDispatchInfo[3] = frame::CommandGroup::VIEW;

    return aDispatchInfo;
}

uno::Sequence< frame::DispatchInformation > SAL_CALL BibFrameController_Impl::getConfigurableDispatchInformation( ::sal_Int16 nCommandGroup )
{
    const CmdToInfoCache& rCmdCache = GetCommandToInfoCache();

    frame::DispatchInformation                  aDispatchInfo;
    std::vector< frame::DispatchInformation >   aDispatchInfoVector;

    if (( nCommandGroup == frame::CommandGroup::EDIT ) ||
        ( nCommandGroup == frame::CommandGroup::DOCUMENT ) ||
        ( nCommandGroup == frame::CommandGroup::DATA ) ||
        ( nCommandGroup == frame::CommandGroup::VIEW ))
    {
        bool bGroupFound = false;
        for (auto const& item : rCmdCache)
        {
            if ( item.second.nGroupId == nCommandGroup )
            {
                bGroupFound = true;
                aDispatchInfo.Command = item.first;
                aDispatchInfo.GroupId = item.second.nGroupId;
                aDispatchInfoVector.push_back( aDispatchInfo );
            }
            else if ( bGroupFound )
                break;
        }
    }

    return comphelper::containerToSequence( aDispatchInfoVector );
}

static bool canInsertRecords(const Reference< beans::XPropertySet>& _rxCursorSet)
{
    sal_Int32 nPriv = 0;
    _rxCursorSet->getPropertyValue("Privileges") >>= nPriv;
    return _rxCursorSet.is() && (nPriv & sdbcx::Privilege::INSERT) != 0;
}

bool BibFrameController_Impl::SaveModified(const Reference< form::runtime::XFormController>& xController)
{
    if (!xController.is())
        return false;

    Reference< XResultSetUpdate> _xCursor(xController->getModel(), UNO_QUERY);

    if (!_xCursor.is())
        return false;

    Reference< beans::XPropertySet> _xSet(_xCursor, UNO_QUERY);
    if (!_xSet.is())
        return false;

    // need to save?
    bool  bIsNew        = ::comphelper::getBOOL(_xSet->getPropertyValue("IsNew"));
    bool  bIsModified   = ::comphelper::getBOOL(_xSet->getPropertyValue("IsModified"));
    bool bResult = !bIsModified;
    if (bIsModified)
    {
        try
        {
            if (bIsNew)
                _xCursor->insertRow();
            else
                _xCursor->updateRow();
            bResult = true;
        }
        catch(const Exception&)
        {
            OSL_FAIL("SaveModified: Exception occurred!");
        }
    }
    return bResult;
}

static vcl::Window* lcl_GetFocusChild( vcl::Window const * pParent )
{
    sal_uInt16 nChildren = pParent->GetChildCount();
    for( sal_uInt16 nChild = 0; nChild < nChildren; ++nChild)
    {
        vcl::Window* pChild = pParent->GetChild( nChild );
        if(pChild->HasFocus())
            return pChild;
        vcl::Window* pSubChild = lcl_GetFocusChild( pChild );
        if(pSubChild)
            return pSubChild;
    }
    return nullptr;
}

//class XDispatch
void BibFrameController_Impl::dispatch(const util::URL& _rURL, const uno::Sequence< beans::PropertyValue >& aArgs)
{
    if ( bDisposing )
        return;

    ::SolarMutexGuard aGuard;
    weld::Window* pParent = Application::GetFrameWeld(xWindow);
    weld::WaitObject aWaitObject(pParent);

    OUString aCommand( _rURL.Path);
    if(aCommand == "Bib/Mapping")
    {
        m_xDatMan->CreateMappingDialog(pParent);
    }
    else if(aCommand == "Bib/source")
    {
        ChangeDataSource(aArgs);
    }
    else if(aCommand == "Bib/sdbsource")
    {
        OUString aURL = m_xDatMan->CreateDBChangeDialog(pParent);
        if(!aURL.isEmpty())
        {
            try
            {
                uno::Sequence< beans::PropertyValue > aNewDataSource(2);
                beans::PropertyValue* pProps = aNewDataSource.getArray();
                pProps[0].Value <<= OUString();
                pProps[1].Value <<= aURL;
                ChangeDataSource(aNewDataSource);
            }
            catch(const Exception&)
            {
                OSL_FAIL("Exception caught while changing the data source");
            }
        }
    }
    else if(aCommand == "Bib/autoFilter")
    {
        sal_uInt16 nCount = aStatusListeners.size();
        for ( sal_uInt16 n=0; n<nCount; n++ )
        {
            BibStatusDispatch *pObj = aStatusListeners[n].get();
            if ( pObj->aURL.Path == "Bib/removeFilter" )
            {
                FeatureStateEvent  aEvent;
                aEvent.FeatureURL = pObj->aURL;
                aEvent.IsEnabled  = true;
                aEvent.Requery    = false;
                aEvent.Source     = static_cast<XDispatch *>(this);
                pObj->xListener->statusChanged( aEvent );
                //break; because there are more than one
            }
        }

        const beans::PropertyValue* pPropertyValue = aArgs.getConstArray();
        uno::Any aValue=pPropertyValue[0].Value;
        OUString aQuery;
        aValue >>= aQuery;<--- aValue is assigned

        aValue=pPropertyValue[1].Value;<--- aValue is overwritten<--- Variable 'aValue' is assigned a value that is never used.
        OUString aQueryField;
        aValue >>= aQueryField;<--- Variable 'aValue' is assigned a value that is never used.
        BibConfig* pConfig = BibModul::GetConfig();
        pConfig->setQueryField(aQueryField);
        m_xDatMan->startQueryWith(aQuery);
    }
    else if(aCommand == "Bib/standardFilter")
    {
        try
        {
            uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();

            // create the dialog object
            uno::Reference< ui::dialogs::XExecutableDialog > xDialog = sdb::FilterDialog::createWithQuery(xContext, m_xDatMan->getParser(),
                       Reference<sdbc::XRowSet>(m_xDatMan->getForm(), uno::UNO_QUERY_THROW), xWindow);
            // execute it
            if ( xDialog->execute( ) )
            {
                // the dialog has been executed successfully, and the filter on the query composer
                // has been changed
                OUString sNewFilter = m_xDatMan->getParser()->getFilter();
                m_xDatMan->setFilter( sNewFilter );
            }
        }
        catch( const uno::Exception& )
        {
            TOOLS_WARN_EXCEPTION( "extensions.biblio", "BibFrameController_Impl::dispatch" );
        }

        sal_uInt16 nCount = aStatusListeners.size();
        for ( sal_uInt16 n=0; n<nCount; n++ )
        {
            BibStatusDispatch *pObj = aStatusListeners[n].get();
            if ( pObj->aURL.Path == "Bib/removeFilter" && m_xDatMan->getParser().is())
            {
                FeatureStateEvent  aEvent;
                aEvent.FeatureURL = pObj->aURL;
                aEvent.IsEnabled  = !m_xDatMan->getParser()->getFilter().isEmpty();
                aEvent.Requery    = false;
                aEvent.Source     = static_cast<XDispatch *>(this);
                pObj->xListener->statusChanged( aEvent );
            }
        }
    }
    else if(aCommand == "Bib/removeFilter")
    {
        RemoveFilter();
    }
    else if( _rURL.Complete == "slot:5503" || aCommand == "CloseDoc" )
    {
        Application::PostUserEvent( LINK( this, BibFrameController_Impl,
                                    DisposeHdl ) );

    }
    else if(aCommand == "Bib/InsertRecord")
    {
        Reference<form::runtime::XFormController > xFormCtrl = m_xDatMan->GetFormController();
        if(SaveModified(xFormCtrl))
        {
            try
            {
                Reference< sdbc::XResultSet >  xCursor( m_xDatMan->getForm(), UNO_QUERY );
                xCursor->last();

                Reference< XResultSetUpdate >  xUpdateCursor( m_xDatMan->getForm(), UNO_QUERY );
                xUpdateCursor->moveToInsertRow();
            }
            catch(const Exception&)
            {
                OSL_FAIL("Exception in last() or moveToInsertRow()");
            }
        }
    }
    else if(aCommand == "Bib/DeleteRecord")
    {
        Reference< css::sdbc::XResultSet >  xCursor(m_xDatMan->getForm(), UNO_QUERY);
        Reference< XResultSetUpdate >       xUpdateCursor(xCursor, UNO_QUERY);
        Reference< beans::XPropertySet >    xSet(m_xDatMan->getForm(), UNO_QUERY);
        bool  bIsNew  = ::comphelper::getBOOL(xSet->getPropertyValue("IsNew"));
        if(!bIsNew)
        {
            sal_uInt32 nCount = 0;
            xSet->getPropertyValue("RowCount") >>= nCount;
            // determine next position
            bool bSuccess = false;
            bool bLeft = false;
            bool bRight = false;
            try
            {
                bLeft = xCursor->isLast() && nCount > 1;
                bRight= !xCursor->isLast();
                // ask for confirmation
                Reference< form::XConfirmDeleteListener >  xConfirm(m_xDatMan->GetFormController(),UNO_QUERY);
                if (xConfirm.is())
                {
                    sdb::RowChangeEvent aEvent;
                    aEvent.Source.set(xCursor, UNO_QUERY);
                    aEvent.Action = sdb::RowChangeAction::DELETE;
                    aEvent.Rows = 1;
                    bSuccess = xConfirm->confirmDelete(aEvent);
                }

                // delete it
                if (bSuccess)
                    xUpdateCursor->deleteRow();
            }
            catch(const Exception&)
            {
                bSuccess = false;
            }
            if (bSuccess)
            {
                if (bLeft || bRight)
                    xCursor->relative(bRight ? 1 : -1);
                else
                {
                    bool bCanInsert = canInsertRecords(xSet);
                    // can another entry be inserted?
                    try
                    {
                        if (bCanInsert)
                            xUpdateCursor->moveToInsertRow();
                        else
                            // move data entry to reset state
                            xCursor->first();
                    }
                    catch(const Exception&)
                    {
                        OSL_FAIL("DeleteRecord: exception caught!");
                    }
                }
            }
        }
    }
    else if(aCommand == "Cut")
    {
        vcl::Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
        if(pChild)
        {
            KeyEvent aEvent( 0, KeyFuncType::CUT );
            pChild->KeyInput( aEvent );
        }
    }
    else if(aCommand == "Copy")
    {
        vcl::Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
        if(pChild)
        {
            KeyEvent aEvent( 0, KeyFuncType::COPY );
            pChild->KeyInput( aEvent );
        }
    }
    else if(aCommand == "Paste")
    {
        vcl::Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
        if(pChild)
        {
            KeyEvent aEvent( 0, KeyFuncType::PASTE );
            pChild->KeyInput( aEvent );
        }
    }
}
IMPL_LINK_NOARG( BibFrameController_Impl, DisposeHdl, void*, void )
{
    xFrame->dispose();
};

void BibFrameController_Impl::addStatusListener(
    const uno::Reference< frame::XStatusListener > & aListener,
    const util::URL& aURL)
{
    BibConfig* pConfig = BibModul::GetConfig();
    // create a new Reference and insert into listener array
    aStatusListeners.push_back( std::make_unique<BibStatusDispatch>( aURL, aListener ) );

    // send first status synchronously
    FeatureStateEvent aEvent;
    aEvent.FeatureURL = aURL;
    aEvent.Requery    = false;
    aEvent.Source     = static_cast<XDispatch *>(this);
    if ( aURL.Path == "StatusBarVisible" )
    {
        aEvent.IsEnabled  = false;
        aEvent.State <<= false;
    }
    else if ( aURL.Path == "Bib/hierarchical" )
    {
        aEvent.IsEnabled  = true;
        aEvent.State <<= OUString();
    }
    else if(aURL.Path == "Bib/MenuFilter")
    {
        aEvent.IsEnabled  = true;
        aEvent.FeatureDescriptor=m_xDatMan->getQueryField();

        aEvent.State <<= m_xDatMan->getQueryFields();

    }
    else if ( aURL.Path == "Bib/source")
    {
        aEvent.IsEnabled  = true;
        aEvent.FeatureDescriptor=m_xDatMan->getActiveDataTable();

        aEvent.State <<= m_xDatMan->getDataSources();
    }
    else if( aURL.Path == "Bib/sdbsource" ||
             aURL.Path == "Bib/Mapping" ||
             aURL.Path == "Bib/autoFilter" ||
             aURL.Path == "Bib/standardFilter" )
    {
        aEvent.IsEnabled  = true;
    }
    else if(aURL.Path == "Bib/query")
    {
        aEvent.IsEnabled  = true;
        aEvent.State <<= pConfig->getQueryText();
    }
    else if (aURL.Path == "Bib/removeFilter" )
    {
        OUString aFilterStr=m_xDatMan->getFilter();
        aEvent.IsEnabled  = !aFilterStr.isEmpty();
    }
    else if(aURL.Path == "Cut")
    {
        vcl::Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
        Edit* pEdit = dynamic_cast<Edit*>( pChild );
        if( pEdit )
            aEvent.IsEnabled  = !pEdit->IsReadOnly() && pEdit->GetSelection().Len();
    }
    if(aURL.Path == "Copy")
    {
        vcl::Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
        Edit* pEdit = dynamic_cast<Edit*>( pChild );
        if( pEdit )
            aEvent.IsEnabled  = pEdit->GetSelection().Len() > 0;
    }
    else if(aURL.Path == "Paste" )
    {
        aEvent.IsEnabled  = false;
        vcl::Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
        if(pChild)
        {
            uno::Reference< datatransfer::clipboard::XClipboard > xClip = pChild->GetClipboard();
            if(xClip.is())
            {
                uno::Reference< datatransfer::XTransferable > xDataObj;

                try
                    {
                        SolarMutexReleaser aReleaser;
                        xDataObj = xClip->getContents();
                    }
                catch( const uno::Exception& )
                    {
                    }

                if ( xDataObj.is() )
                {
                    datatransfer::DataFlavor aFlavor;
                    SotExchange::GetFormatDataFlavor( SotClipboardFormatId::STRING, aFlavor );
                    try
                    {
                        uno::Any aData = xDataObj->getTransferData( aFlavor );
                        OUString aText;
                        aData >>= aText;
                        aEvent.IsEnabled  = !aText.isEmpty();
                    }
                    catch( const uno::Exception& )
                    {
                    }
                }
            }
        }
    }
    else if(aURL.Path == "Bib/DeleteRecord")
    {
        Reference< beans::XPropertySet >    xSet(m_xDatMan->getForm(), UNO_QUERY);
        bool  bIsNew  = ::comphelper::getBOOL(xSet->getPropertyValue("IsNew"));
        if(!bIsNew)
        {
            sal_uInt32 nCount = 0;
            xSet->getPropertyValue("RowCount") >>= nCount;
            aEvent.IsEnabled  = nCount > 0;
        }
    }
    else if (aURL.Path == "Bib/InsertRecord")
    {
        Reference< beans::XPropertySet >  xSet(m_xDatMan->getForm(), UNO_QUERY);
        aEvent.IsEnabled = canInsertRecords(xSet);
    }
    aListener->statusChanged( aEvent );
}

void BibFrameController_Impl::removeStatusListener(
    const uno::Reference< frame::XStatusListener > & aObject, const util::URL& aURL)
{
    // search listener array for given listener
    // for checking equality always "cast" to XInterface
    if ( bDisposing )
        return;

    sal_uInt16 nCount = aStatusListeners.size();
    for ( sal_uInt16 n=0; n<nCount; n++ )
    {
        BibStatusDispatch *pObj = aStatusListeners[n].get();
        bool bFlag=pObj->xListener.is();
        if (!bFlag || (pObj->xListener == aObject &&
            ( aURL.Complete.isEmpty() || pObj->aURL.Path == aURL.Path  )))
        {
            aStatusListeners.erase( aStatusListeners.begin() + n );
            break;
        }
    }
}

void BibFrameController_Impl::RemoveFilter()
{
    OUString aQuery;
    m_xDatMan->startQueryWith(aQuery);

    sal_uInt16 nCount = aStatusListeners.size();

    bool bRemoveFilter=false;
    bool bQueryText=false;

    for ( sal_uInt16 n=0; n<nCount; n++ )
    {
        BibStatusDispatch *pObj = aStatusListeners[n].get();
        if ( pObj->aURL.Path == "Bib/removeFilter" )
        {
            FeatureStateEvent  aEvent;
            aEvent.FeatureURL = pObj->aURL;
            aEvent.IsEnabled  = false;
            aEvent.Requery    = false;
            aEvent.Source     = static_cast<XDispatch *>(this);
            pObj->xListener->statusChanged( aEvent );
            bRemoveFilter=true;
        }
        else if(pObj->aURL.Path == "Bib/query")
        {
            FeatureStateEvent  aEvent;
            aEvent.FeatureURL = pObj->aURL;
            aEvent.IsEnabled  = true;
            aEvent.Requery    = false;
            aEvent.Source     = static_cast<XDispatch *>(this);
            aEvent.State <<= aQuery;
            pObj->xListener->statusChanged( aEvent );
            bQueryText=true;
        }

        if(bRemoveFilter && bQueryText)
            break;

    }
}

void BibFrameController_Impl::ChangeDataSource(const uno::Sequence< beans::PropertyValue >& aArgs)
{
    const beans::PropertyValue* pPropertyValue = aArgs.getConstArray();
    uno::Any aValue=pPropertyValue[0].Value;<--- Variable 'aValue' is assigned a value that is never used.
    OUString aDBTableName;
    aValue >>= aDBTableName;<--- Variable 'aValue' is assigned a value that is never used.


    if(aArgs.getLength() > 1)
    {
        uno::Any aDB = pPropertyValue[1].Value;<--- Variable 'aDB' is assigned a value that is never used.
        OUString aURL;
        aDB >>= aURL;<--- Variable 'aDB' is assigned a value that is never used.
        m_xDatMan->setActiveDataSource(aURL);
        aDBTableName = m_xDatMan->getActiveDataTable();<--- Variable 'aDBTableName' is assigned a value that is never used.
    }
    else
    {
        Reference<css::form::XLoadable> xLoadable(m_xDatMan.get());
        xLoadable->unload();
        m_xDatMan->setActiveDataTable(aDBTableName);
        m_xDatMan->updateGridModel();
        xLoadable->load();
    }


    sal_uInt16 nCount = aStatusListeners.size();

    bool bMenuFilter=false;
    bool bQueryText=false;
    for ( sal_uInt16 n=0; n<nCount; n++ )
    {
        BibStatusDispatch *pObj = aStatusListeners[n].get();
        if (pObj->aURL.Path == "Bib/MenuFilter")
        {
            FeatureStateEvent  aEvent;
            aEvent.FeatureURL = pObj->aURL;
            aEvent.IsEnabled  = true;
            aEvent.Requery    = false;
            aEvent.Source     = static_cast<XDispatch *>(this);
            aEvent.FeatureDescriptor=m_xDatMan->getQueryField();

            uno::Sequence<OUString> aStringSeq=m_xDatMan->getQueryFields();
            aEvent.State <<= aStringSeq;

            pObj->xListener->statusChanged( aEvent );
            bMenuFilter=true;
        }
        else if (pObj->aURL.Path == "Bib/query")
        {
            FeatureStateEvent  aEvent;
            aEvent.FeatureURL = pObj->aURL;
            aEvent.IsEnabled  = true;
            aEvent.Requery    = false;
            aEvent.Source     = static_cast<XDispatch *>(this);
            BibConfig* pConfig = BibModul::GetConfig();
            aEvent.State <<= pConfig->getQueryText();
            pObj->xListener->statusChanged( aEvent );
            bQueryText=true;
        }

        if (bMenuFilter && bQueryText)
            break;

    }
}

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