xref: /AOO41X/main/extensions/source/bibliography/framectr.cxx (revision 2a97ec55f1442d65917e8c8b82a55ab76c9ff676)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 #include <vcl/waitobj.hxx>
27 #include <cppuhelper/interfacecontainer.hxx>
28 #include <com/sun/star/util/URL.hpp>
29 #include <osl/mutex.hxx>
30 #include <vcl/msgbox.hxx>
31 #include <tools/debug.hxx>
32 #include <vcl/stdtext.hxx>
33 #include <comphelper/types.hxx>
34 #include <comphelper/sequence.hxx>
35 #include "framectr.hxx"
36 #include "datman.hxx"
37 #include "bibresid.hxx"
38 #include "bib.hrc"
39 #include <toolkit/helper/vclunohelper.hxx>
40 #include "bibconfig.hxx"
41 #include <cppuhelper/implbase1.hxx> // helper for implementations
42 #include <vcl/svapp.hxx>
43 #include "bibliography.hrc"
44 #include <comphelper/processfactory.hxx>
45 #include <com/sun/star/form/XConfirmDeleteListener.hpp>
46 #include <com/sun/star/form/runtime/XFormController.hpp>
47 #include <com/sun/star/beans/PropertyState.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
50 #include <com/sun/star/sdbcx/Privilege.hpp>
51 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
52 #include <com/sun/star/sdb/RowChangeAction.hpp>
53 #include <com/sun/star/frame/CommandGroup.hpp>
54 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
55 #include <sot/exchange.hxx>
56 #include <sot/formats.hxx>
57 #include <vcl/edit.hxx>
58 #include <vos/mutex.hxx>
59 
60 #include <hash_map>
61 
62 using namespace osl;
63 using namespace cppu;
64 using namespace rtl;
65 using namespace com::sun::star::sdbc;
66 using namespace com::sun::star::frame;
67 using namespace com::sun::star::uno;
68 using namespace com::sun::star;
69 
70 #define C2U(cChar) OUString::createFromAscii(cChar)
71 
72 struct DispatchInfo
73 {
74     const char*   pCommand;
75     sal_Int16     nGroupId;
76     sal_Bool      bActiveConnection;
77 };
78 
79 struct CacheDispatchInfo
80 {
81     sal_Int16     nGroupId;
82     sal_Bool      bActiveConnection;
83 };
84 
85 // Attention: commands must be sorted by command groups. Implementation is dependent
86 // on this!!
87 static DispatchInfo SupportedCommandsArray[] =
88 {
89     { ".uno:Undo"               ,   frame::CommandGroup::EDIT       , sal_False },
90     { ".uno:Cut"                ,   frame::CommandGroup::EDIT       , sal_False },
91     { ".uno:Copy"               ,   frame::CommandGroup::EDIT       , sal_False },
92     { ".uno:Paste"              ,   frame::CommandGroup::EDIT       , sal_False },
93     { ".uno:SelectAll"          ,   frame::CommandGroup::EDIT       , sal_False },
94     { ".uno:CloseDoc"           ,   frame::CommandGroup::DOCUMENT   , sal_False },
95     { ".uno:StatusBarVisible"   ,   frame::CommandGroup::VIEW       , sal_False },
96     { ".uno:AvailableToolbars"  ,   frame::CommandGroup::VIEW       , sal_False },
97     { ".uno:Bib/standardFilter" ,   frame::CommandGroup::DATA       , sal_True  },
98     { ".uno:Bib/DeleteRecord"   ,   frame::CommandGroup::DATA       , sal_True  },
99     { ".uno:Bib/InsertRecord"   ,   frame::CommandGroup::DATA       , sal_True  },
100     { ".uno:Bib/query"          ,   frame::CommandGroup::DATA       , sal_True  },
101     { ".uno:Bib/autoFilter"     ,   frame::CommandGroup::DATA       , sal_True  },
102     { ".uno:Bib/source"         ,   frame::CommandGroup::DATA       , sal_True  },
103     { ".uno:Bib/removeFilter"   ,   frame::CommandGroup::DATA       , sal_True  },
104     { ".uno:Bib/sdbsource"      ,   frame::CommandGroup::DATA       , sal_True  },
105     { ".uno:Bib/Mapping"        ,   frame::CommandGroup::DATA       , sal_True  },
106     { 0                         ,   0                               , sal_False }
107 };
108 
109 typedef ::std::hash_map< ::rtl::OUString, CacheDispatchInfo, rtl::OUStringHash, ::std::equal_to< ::rtl::OUString > > CmdToInfoCache;
110 
111 SV_IMPL_PTRARR( BibStatusDispatchArr, BibStatusDispatchPtr );
112 
GetCommandToInfoCache()113 const CmdToInfoCache& GetCommandToInfoCache()
114 {
115     static sal_Bool       bCacheInitialized = sal_False;
116     static CmdToInfoCache aCmdToInfoCache;
117 
118     if ( !bCacheInitialized )
119     {
120         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
121         if ( !bCacheInitialized )
122         {
123             sal_Int32 i( 0 );
124             while ( SupportedCommandsArray[i].pCommand != 0 )
125             {
126                 rtl::OUString aCommand( rtl::OUString::createFromAscii( SupportedCommandsArray[i].pCommand ));
127 
128                 CacheDispatchInfo aDispatchInfo;
129                 aDispatchInfo.nGroupId          = SupportedCommandsArray[i].nGroupId;
130                 aDispatchInfo.bActiveConnection = SupportedCommandsArray[i].bActiveConnection;
131                 aCmdToInfoCache.insert( CmdToInfoCache::value_type( aCommand, aDispatchInfo ));
132                 ++i;
133             }
134             bCacheInitialized = sal_True;
135         }
136     }
137 
138     return aCmdToInfoCache;
139 }
140 
141 
142 class BibFrameCtrl_Impl : public cppu::WeakImplHelper1 < XFrameActionListener >
143 {
144 public:
145     Mutex                               aMutex;
146     OMultiTypeInterfaceContainerHelper  aLC;
147 
148     BibFrameController_Impl*            pController;
149 
BibFrameCtrl_Impl()150                                         BibFrameCtrl_Impl()
151                                             : aLC( aMutex )
152                                             , pController(0)
153                                         {}
154 
155                                         ~BibFrameCtrl_Impl();
156 
157     virtual void                        SAL_CALL frameAction(const FrameActionEvent& aEvent) throw( RuntimeException );
158     virtual void                        SAL_CALL disposing( const lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
159 };
160 
161 
~BibFrameCtrl_Impl()162 BibFrameCtrl_Impl::~BibFrameCtrl_Impl()
163 {
164 }
165 
frameAction(const FrameActionEvent & aEvent)166 void BibFrameCtrl_Impl::frameAction(const FrameActionEvent& aEvent) throw( uno::RuntimeException )
167 {
168     if ( pController && aEvent.Frame == pController->getFrame())
169     {
170         if(aEvent.Action == FrameAction_FRAME_ACTIVATED)
171         {
172             pController->activate();
173         }
174         else if(aEvent.Action == FrameAction_FRAME_DEACTIVATING)
175         {
176             pController->deactivate();
177         }
178     }
179 }
180 
disposing(const lang::EventObject &)181 void BibFrameCtrl_Impl::disposing( const lang::EventObject& /*Source*/ )
182     throw (::com::sun::star::uno::RuntimeException)
183 {
184     vos::OGuard aGuard(Application::GetSolarMutex());
185     if ( pController )
186         pController->getFrame()->removeFrameActionListener( this );
187 }
188 
BibFrameController_Impl(const uno::Reference<awt::XWindow> & xComponent,BibDataManager * pDataManager)189 BibFrameController_Impl::BibFrameController_Impl( const uno::Reference< awt::XWindow > & xComponent,
190                                                 BibDataManager* pDataManager)
191     :xWindow( xComponent )
192     ,m_xDatMan( pDataManager )
193     ,pDatMan( pDataManager )
194     ,pBibMod(NULL)
195 {
196     Window* pParent = VCLUnoHelper::GetWindow( xWindow );
197     pParent->SetUniqueId(UID_BIB_FRAME_WINDOW);
198     bDisposing=sal_False;
199     bHierarchical=sal_True;
200     pImp = new BibFrameCtrl_Impl;
201     pImp->pController = this;
202     pImp->acquire();
203 }
204 
~BibFrameController_Impl()205 BibFrameController_Impl::~BibFrameController_Impl()
206 {
207     pImp->pController = NULL;
208     pImp->release();
209     delete pDatMan;
210     if(pBibMod)
211         CloseBibModul(pBibMod);
212 }
213 
getImplementationName()214 ::rtl::OUString SAL_CALL BibFrameController_Impl::getImplementationName() throw (::com::sun::star::uno::RuntimeException)
215 {
216     return ::rtl::OUString::createFromAscii("com.sun.star.comp.extensions.Bibliography");
217 }
218 
supportsService(const::rtl::OUString & sServiceName)219 sal_Bool SAL_CALL BibFrameController_Impl::supportsService( const ::rtl::OUString& sServiceName ) throw (::com::sun::star::uno::RuntimeException)
220 {
221     return (
222             sServiceName.equalsAscii("com.sun.star.frame.Bibliography") ||
223             sServiceName.equalsAscii("com.sun.star.frame.Controller")
224            );
225 }
226 
getSupportedServiceNames()227 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL BibFrameController_Impl::getSupportedServiceNames() throw (::com::sun::star::uno::RuntimeException)
228 {
229     // return only top level services ...
230     // base services are included there and should be asked by uno-rtti.
231     ::com::sun::star::uno::Sequence< ::rtl::OUString > lNames(1);
232     lNames[0] = ::rtl::OUString::createFromAscii("com.sun.star.frame.Bibliography");
233     return lNames;
234 }
235 
attachFrame(const uno::Reference<XFrame> & xArg)236 void BibFrameController_Impl::attachFrame( const uno::Reference< XFrame > & xArg ) throw (::com::sun::star::uno::RuntimeException)
237 {
238     xFrame = xArg;
239     xFrame->addFrameActionListener( pImp );
240 }
241 
attachModel(const uno::Reference<XModel> &)242 sal_Bool BibFrameController_Impl::attachModel( const uno::Reference< XModel > & /*xModel*/ ) throw (::com::sun::star::uno::RuntimeException)
243 {
244     return sal_False;
245 }
246 
suspend(sal_Bool bSuspend)247 sal_Bool BibFrameController_Impl::suspend( sal_Bool bSuspend ) throw (::com::sun::star::uno::RuntimeException)
248 {
249     if ( bSuspend )
250         getFrame()->removeFrameActionListener( pImp );
251     else
252         getFrame()->addFrameActionListener( pImp );
253     return sal_True;
254 }
255 
getViewData()256 uno::Any BibFrameController_Impl::getViewData() throw (::com::sun::star::uno::RuntimeException)
257 {
258     return uno::Any();
259 }
260 
restoreViewData(const uno::Any &)261 void BibFrameController_Impl::restoreViewData( const uno::Any& /*Value*/ ) throw (::com::sun::star::uno::RuntimeException)
262 {
263 }
264 
getFrame()265 uno::Reference< XFrame >  BibFrameController_Impl::getFrame() throw (::com::sun::star::uno::RuntimeException)
266 {
267     return xFrame;
268 }
269 
getModel()270 uno::Reference< XModel >  BibFrameController_Impl::getModel() throw (::com::sun::star::uno::RuntimeException)
271 {
272     return uno::Reference< XModel > ();
273 }
274 
dispose()275 void BibFrameController_Impl::dispose() throw (::com::sun::star::uno::RuntimeException)
276 {
277     bDisposing = sal_True;
278     lang::EventObject aObject;
279     aObject.Source = (XController*)this;
280     pImp->aLC.disposeAndClear(aObject);
281     m_xDatMan = 0;
282     pDatMan = 0;
283     aStatusListeners.DeleteAndDestroy( 0, aStatusListeners.Count() );
284  }
285 
addEventListener(const uno::Reference<lang::XEventListener> & aListener)286 void BibFrameController_Impl::addEventListener( const uno::Reference< lang::XEventListener > & aListener ) throw (::com::sun::star::uno::RuntimeException)
287 {
288     pImp->aLC.addInterface( ::getCppuType((const Reference< lang::XEventListener >*)0), aListener );
289 }
290 
removeEventListener(const uno::Reference<lang::XEventListener> & aListener)291 void BibFrameController_Impl::removeEventListener( const uno::Reference< lang::XEventListener > & aListener ) throw (::com::sun::star::uno::RuntimeException)
292 {
293     pImp->aLC.removeInterface( ::getCppuType((const Reference< lang::XEventListener >*)0), aListener );
294 }
295 
queryDispatch(const util::URL & aURL,const rtl::OUString &,sal_Int32)296 uno::Reference< frame::XDispatch >  BibFrameController_Impl::queryDispatch( const util::URL& aURL, const rtl::OUString& /*aTarget*/, sal_Int32 /*nSearchFlags*/ ) throw (::com::sun::star::uno::RuntimeException)
297 {
298     if ( !bDisposing )
299     {
300         const CmdToInfoCache& rCmdCache = GetCommandToInfoCache();
301         CmdToInfoCache::const_iterator pIter = rCmdCache.find( aURL.Complete );
302         if ( pIter != rCmdCache.end() )
303         {
304             if (( pDatMan->HasActiveConnection() ) ||
305                 ( !pIter->second.bActiveConnection ))
306                 return (frame::XDispatch*) this;
307         }
308     }
309 
310     return uno::Reference< frame::XDispatch > ();
311 }
312 
queryDispatches(const uno::Sequence<DispatchDescriptor> & aDescripts)313 uno::Sequence<uno::Reference< XDispatch > > BibFrameController_Impl::queryDispatches( const uno::Sequence<DispatchDescriptor>& aDescripts ) throw (::com::sun::star::uno::RuntimeException)
314 {
315     uno::Sequence< uno::Reference< XDispatch > > aDispatches( aDescripts.getLength() );
316     for ( sal_Int32 i=0; i<aDescripts.getLength(); ++i )
317         aDispatches[i] = queryDispatch( aDescripts[i].FeatureURL, aDescripts[i].FrameName, aDescripts[i].SearchFlags );
318     return aDispatches;
319 }
320 
getSupportedCommandGroups()321 uno::Sequence< ::sal_Int16 > SAL_CALL BibFrameController_Impl::getSupportedCommandGroups()
322 throw (::com::sun::star::uno::RuntimeException)
323 {
324     uno::Sequence< ::sal_Int16 > aDispatchInfo( 4 );
325 
326     aDispatchInfo[0] = frame::CommandGroup::EDIT;
327     aDispatchInfo[1] = frame::CommandGroup::DOCUMENT;
328     aDispatchInfo[2] = frame::CommandGroup::DATA;
329     aDispatchInfo[3] = frame::CommandGroup::VIEW;
330 
331     return aDispatchInfo;
332 }
333 
getConfigurableDispatchInformation(::sal_Int16 nCommandGroup)334 uno::Sequence< frame::DispatchInformation > SAL_CALL BibFrameController_Impl::getConfigurableDispatchInformation( ::sal_Int16 nCommandGroup )
335 throw (::com::sun::star::uno::RuntimeException)
336 {
337     const CmdToInfoCache& rCmdCache = GetCommandToInfoCache();
338 
339     sal_Bool                                    bGroupFound( sal_False );
340     frame::DispatchInformation                  aDispatchInfo;
341     std::list< frame::DispatchInformation >     aDispatchInfoList;
342 
343     if (( nCommandGroup == frame::CommandGroup::EDIT ) ||
344         ( nCommandGroup == frame::CommandGroup::DOCUMENT ) ||
345         ( nCommandGroup == frame::CommandGroup::DATA ) ||
346         ( nCommandGroup == frame::CommandGroup::VIEW ))
347     {
348         CmdToInfoCache::const_iterator pIter = rCmdCache.begin();
349         while ( pIter != rCmdCache.end() )
350         {
351             if ( pIter->second.nGroupId == nCommandGroup )
352             {
353                 bGroupFound = sal_True;
354                 aDispatchInfo.Command = pIter->first;
355                 aDispatchInfo.GroupId = pIter->second.nGroupId;
356                 aDispatchInfoList.push_back( aDispatchInfo );
357             }
358             else if ( bGroupFound )
359                 break;
360 
361             ++pIter;
362         }
363     }
364 
365     ::com::sun::star::uno::Sequence< ::com::sun::star::frame::DispatchInformation > aSeq =
366         comphelper::containerToSequence< ::com::sun::star::frame::DispatchInformation, std::list< ::com::sun::star::frame::DispatchInformation > >( aDispatchInfoList );
367 
368     return aSeq;
369 }
370 
canInsertRecords(const Reference<beans::XPropertySet> & _rxCursorSet)371 sal_Bool canInsertRecords(const Reference< beans::XPropertySet>& _rxCursorSet)
372 {
373     sal_Int32 nPriv = 0;
374     _rxCursorSet->getPropertyValue(C2U("Privileges")) >>= nPriv;
375     return ((_rxCursorSet.is() && (nPriv & sdbcx::Privilege::INSERT) != 0));
376 }
377 /* -----------------------------08.05.2002 08:58------------------------------
378 
379  ---------------------------------------------------------------------------*/
SaveModified(const Reference<form::runtime::XFormController> & xController)380 sal_Bool BibFrameController_Impl::SaveModified(const Reference< form::runtime::XFormController>& xController)
381 {
382     if (!xController.is())
383         return sal_False;
384     sal_Bool bInserted = sal_False;
385 
386     Reference< XResultSetUpdate> _xCursor = Reference< XResultSetUpdate>(xController->getModel(), UNO_QUERY);
387 
388     if (!_xCursor.is())
389         return sal_False;
390 
391     Reference< beans::XPropertySet> _xSet = Reference< beans::XPropertySet>(_xCursor, UNO_QUERY);
392     if (!_xSet.is())
393         return sal_False;
394 
395     // mu� gespeichert werden ?
396     sal_Bool  bIsNew        = ::comphelper::getBOOL(_xSet->getPropertyValue(C2U("IsNew")));
397     sal_Bool  bIsModified   = ::comphelper::getBOOL(_xSet->getPropertyValue(C2U("IsModified")));
398     sal_Bool bResult = !bIsModified;
399     if (bIsModified)
400     {
401         try
402         {
403             if (bIsNew)
404                 _xCursor->insertRow();
405             else
406                 _xCursor->updateRow();
407             bResult = sal_True;
408         }
409         catch(Exception&)
410         {
411             DBG_ERROR("SaveModified: Exception occured!");
412         }
413 
414         bInserted = bIsNew && bResult;
415     }
416     return bResult;
417 }
418 
lcl_GetFocusChild(Window * pParent)419 Window* lcl_GetFocusChild( Window* pParent )
420 {
421     sal_uInt16 nChildren = pParent->GetChildCount();
422     for( sal_uInt16 nChild = 0; nChild < nChildren; ++nChild)
423     {
424         Window* pChild = pParent->GetChild( nChild );
425         if(pChild->HasFocus())
426             return pChild;
427         Window* pSubChild = lcl_GetFocusChild( pChild );
428         if(pSubChild)
429             return pSubChild;
430     }
431     return 0;
432 }
433 
434 //class XDispatch
dispatch(const util::URL & _rURL,const uno::Sequence<beans::PropertyValue> & aArgs)435 void BibFrameController_Impl::dispatch(const util::URL& _rURL, const uno::Sequence< beans::PropertyValue >& aArgs) throw (::com::sun::star::uno::RuntimeException)
436 {
437     if ( !bDisposing )
438     {
439         vos::OGuard aGuard(Application::GetSolarMutex());
440         Window* pParent = VCLUnoHelper::GetWindow( xWindow );
441         WaitObject aWaitObject( pParent );
442 
443         String aCommand( _rURL.Path);
444         if(aCommand.EqualsAscii("Bib/Mapping"))
445         {
446             pDatMan->CreateMappingDialog(pParent);
447         }
448         else if(aCommand.EqualsAscii("Bib/source"))
449         {
450             ChangeDataSource(aArgs);
451         }
452         else if(aCommand.EqualsAscii("Bib/sdbsource"))
453         {
454             rtl::OUString aURL = pDatMan->CreateDBChangeDialog(pParent);
455             if(aURL.getLength())
456             {
457                 try
458                 {
459                     uno::Sequence< beans::PropertyValue > aNewDataSource(2);
460                     beans::PropertyValue* pProps = aNewDataSource.getArray();
461                     pProps[0].Value <<= rtl::OUString();
462                     pProps[1].Value <<= aURL;
463                     ChangeDataSource(aNewDataSource);
464                 }
465                 catch(const Exception&)
466                 {
467                     DBG_ERROR("Exception catched while changing the data source");
468                 }
469             }
470         }
471         else if(aCommand.EqualsAscii("Bib/autoFilter"))
472         {
473             sal_uInt16 nCount = aStatusListeners.Count();
474             for ( sal_uInt16 n=0; n<nCount; n++ )
475             {
476                 BibStatusDispatch *pObj = aStatusListeners[n];
477                 if ( pObj->aURL.Path == C2U("Bib/removeFilter") )
478                 {
479                     FeatureStateEvent  aEvent;
480                     aEvent.FeatureURL = pObj->aURL;
481                     aEvent.IsEnabled  = sal_True;
482                     aEvent.Requery    = sal_False;
483                     aEvent.Source     = (XDispatch *) this;
484                     pObj->xListener->statusChanged( aEvent );
485                     //break; because there are more than one
486                 }
487             }
488 
489             const beans::PropertyValue* pPropertyValue = aArgs.getConstArray();
490             uno::Any aValue=pPropertyValue[0].Value;
491             rtl::OUString aQuery;
492             aValue >>= aQuery;
493 
494             aValue=pPropertyValue[1].Value;
495             rtl::OUString aQueryField;
496             aValue >>= aQueryField;
497             BibConfig* pConfig = BibModul::GetConfig();
498             pConfig->setQueryField(aQueryField);
499             pDatMan->startQueryWith(aQuery);
500         }
501         else if(aCommand.EqualsAscii("Bib/standardFilter"))
502         {
503             try
504             {
505                 uno::Reference< lang::XMultiServiceFactory > xORB = ::comphelper::getProcessServiceFactory();
506 
507                 // build the arguments for the filter dialog to be created
508                 Sequence< Any > aDialogCreationArgs( 3 );
509                 Any* pDialogCreationArgs = aDialogCreationArgs.getArray();
510                 // the query composer
511                 *pDialogCreationArgs++ <<= beans::PropertyValue( ::rtl::OUString::createFromAscii( "QueryComposer" ),
512                                                         -1,
513                                                         makeAny( pDatMan->getParser() ),
514                                                         beans::PropertyState_DIRECT_VALUE
515                                                       );
516 
517                 // the rowset
518                 *pDialogCreationArgs++ <<= beans::PropertyValue( ::rtl::OUString::createFromAscii( "RowSet" ),
519                                                         -1,
520                                                         makeAny( pDatMan->getForm() ),
521                                                         beans::PropertyState_DIRECT_VALUE
522                                                       );
523                 // the parent window for the dialog
524                 *pDialogCreationArgs++ <<= beans::PropertyValue( ::rtl::OUString::createFromAscii( "ParentWindow" ),
525                                                         -1,
526                                                         makeAny( xWindow ),
527                                                         beans::PropertyState_DIRECT_VALUE
528                                                       );
529 
530                 // create the dialog object
531                 const ::rtl::OUString sDialogServiceName = ::rtl::OUString::createFromAscii( "com.sun.star.sdb.FilterDialog" );
532                 uno::Reference< uno::XInterface > xDialog = xORB->createInstanceWithArguments(
533                     sDialogServiceName,
534                     aDialogCreationArgs
535                 );
536                 if ( !xDialog.is() )
537                 {
538                     ShowServiceNotAvailableError( VCLUnoHelper::GetWindow( xWindow ), sDialogServiceName, sal_True );
539                 }
540                 else
541                 {
542                     // execute it
543                     uno::Reference< ui::dialogs::XExecutableDialog > xExec( xDialog, UNO_QUERY );
544                     DBG_ASSERT( xExec.is(), "BibFrameController_Impl::dispatch: missing an interface on the dialog!" );
545                     if ( xExec.is() )
546                         if ( xExec->execute( ) )
547                         {
548                             // the dialog has been executed successfully, and the filter on the query composer
549                             // has been changed
550                             ::rtl::OUString sNewFilter = pDatMan->getParser()->getFilter();
551                             pDatMan->setFilter( sNewFilter );
552                         }
553                 }
554             }
555             catch( const uno::Exception& )
556             {
557                 DBG_ERROR( "BibFrameController_Impl::dispatch: caught an exception!" );
558             }
559 
560             sal_uInt16 nCount = aStatusListeners.Count();
561             for ( sal_uInt16 n=0; n<nCount; n++ )
562             {
563                 BibStatusDispatch *pObj = aStatusListeners[n];
564                 if ( pObj->aURL.Path == C2U("Bib/removeFilter") && pDatMan->getParser().is())
565                 {
566                     FeatureStateEvent  aEvent;
567                     aEvent.FeatureURL = pObj->aURL;
568                     aEvent.IsEnabled  = 0 != pDatMan->getParser()->getFilter().getLength();
569                     aEvent.Requery    = sal_False;
570                     aEvent.Source     = (XDispatch *) this;
571                     pObj->xListener->statusChanged( aEvent );
572                 }
573             }
574         }
575         else if(aCommand.EqualsAscii("Bib/removeFilter"))
576         {
577             RemoveFilter();
578         }
579         else if(_rURL.Complete.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("slot:5503")) ||
580                 aCommand.EqualsAscii("CloseDoc"))
581         {
582             Application::PostUserEvent( STATIC_LINK( this, BibFrameController_Impl,
583                                         DisposeHdl ), 0 );
584 
585         }
586         else if(aCommand.EqualsAscii("Bib/InsertRecord"))
587         {
588             Reference<form::runtime::XFormController > xFormCtrl = pDatMan->GetFormController();
589             if(SaveModified(xFormCtrl))
590             {
591                 try
592                 {
593                     Reference< sdbc::XResultSet >  xCursor( pDatMan->getForm(), UNO_QUERY );
594                     xCursor->last();
595 
596                     Reference< XResultSetUpdate >  xUpdateCursor( pDatMan->getForm(), UNO_QUERY );
597                     xUpdateCursor->moveToInsertRow();
598                 }
599                 catch(Exception&)
600                 {
601                     DBG_ERROR("Exception in last() or moveToInsertRow()");
602                 }
603             }
604         }
605         else if(aCommand.EqualsAscii("Bib/DeleteRecord"))
606         {
607             Reference< ::com::sun::star::sdbc::XResultSet >  xCursor(pDatMan->getForm(), UNO_QUERY);
608             Reference< XResultSetUpdate >  xUpdateCursor(xCursor, UNO_QUERY);
609             Reference< beans::XPropertySet >  xSet(pDatMan->getForm(), UNO_QUERY);
610             sal_Bool  bIsNew  = ::comphelper::getBOOL(xSet->getPropertyValue(C2U("IsNew")));
611             if(!bIsNew)
612             {
613                 sal_uInt32 nCount = 0;
614                 xSet->getPropertyValue(C2U("RowCount")) >>= nCount;
615                 // naechste position festellen
616                 sal_Bool bSuccess = sal_False;
617                 sal_Bool bLeft = sal_False;
618                 sal_Bool bRight = sal_False;
619                 try
620                 {
621                     bLeft = xCursor->isLast() && nCount > 1;
622                     bRight= !xCursor->isLast();
623                     // ask for confirmation
624                     Reference< frame::XController > xCtrl = pImp->pController;
625                     Reference< form::XConfirmDeleteListener >  xConfirm(pDatMan->GetFormController(),UNO_QUERY);
626                     if (xConfirm.is())
627                     {
628                         sdb::RowChangeEvent aEvent;
629                         aEvent.Source = Reference< XInterface > (xCursor, UNO_QUERY);
630                         aEvent.Action = sdb::RowChangeAction::DELETE;
631                         aEvent.Rows = 1;
632                         bSuccess = xConfirm->confirmDelete(aEvent);
633                     }
634 
635                     // das Ding loeschen
636                     if (bSuccess)
637                         xUpdateCursor->deleteRow();
638                 }
639                 catch(Exception&)
640                 {
641                     bSuccess = sal_False;
642                 }
643                 if (bSuccess)
644                 {
645                     if (bLeft || bRight)
646                         xCursor->relative(bRight ? 1 : -1);
647                     else
648                     {
649                         sal_Bool bCanInsert = canInsertRecords(xSet);
650                         // kann noch ein Datensatz eingefuegt weden
651                         try
652                         {
653                             if (bCanInsert)
654                                 xUpdateCursor->moveToInsertRow();
655                             else
656                                 // Datensatz bewegen um Stati neu zu setzen
657                                 xCursor->first();
658                         }
659                         catch(Exception&)
660                         {
661                             DBG_ERROR("DeleteRecord : exception caught !");
662                         }
663                     }
664                 }
665             }
666         }
667         else if(aCommand.EqualsAscii("Cut"))
668         {
669             Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
670             if(pChild)
671             {
672                 KeyEvent aEvent( 0, KEYFUNC_CUT );
673                 pChild->KeyInput( aEvent );
674             }
675         }
676         else if(aCommand.EqualsAscii("Copy"))
677         {
678             Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
679             if(pChild)
680             {
681                 KeyEvent aEvent( 0, KEYFUNC_COPY );
682                 pChild->KeyInput( aEvent );
683             }
684         }
685         else if(aCommand.EqualsAscii("Paste"))
686         {
687             Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
688             if(pChild)
689             {
690                 KeyEvent aEvent( 0, KEYFUNC_PASTE );
691                 pChild->KeyInput( aEvent );
692             }
693         }
694     }
695 }
IMPL_STATIC_LINK(BibFrameController_Impl,DisposeHdl,void *,EMPTYARG)696 IMPL_STATIC_LINK( BibFrameController_Impl, DisposeHdl, void*, EMPTYARG )
697 {
698     pThis->xFrame->dispose();
699     return 0;
700 };
701 
702 //-----------------------------------------------------------------------------
addStatusListener(const uno::Reference<frame::XStatusListener> & aListener,const util::URL & aURL)703 void BibFrameController_Impl::addStatusListener(
704     const uno::Reference< frame::XStatusListener > & aListener,
705     const util::URL& aURL)
706     throw (::com::sun::star::uno::RuntimeException)
707 {
708     BibConfig* pConfig = BibModul::GetConfig();
709     // create a new Reference and insert into listener array
710     aStatusListeners.Insert( new BibStatusDispatch( aURL, aListener ), aStatusListeners.Count() );
711 
712     // den ersten Status synchron zusenden
713     FeatureStateEvent aEvent;
714     aEvent.FeatureURL = aURL;
715     aEvent.Requery    = sal_False;
716     aEvent.Source     = (XDispatch *) this;
717     if ( aURL.Path == C2U("StatusBarVisible") )
718     {
719         aEvent.IsEnabled  = sal_False;
720         aEvent.State <<= sal_Bool( sal_False );
721     }
722     else if ( aURL.Path == C2U("Bib/hierarchical") )
723     {
724         aEvent.IsEnabled  = sal_True;
725         const char*  pHier = bHierarchical? "" : "*" ;
726         aEvent.State <<= rtl::OUString::createFromAscii(pHier);
727     }
728     else if(aURL.Path == C2U("Bib/MenuFilter"))
729     {
730         aEvent.IsEnabled  = sal_True;
731         aEvent.FeatureDescriptor=pDatMan->getQueryField();
732 
733         uno::Sequence<rtl::OUString> aStringSeq=pDatMan->getQueryFields();
734         aEvent.State.setValue(&aStringSeq,::getCppuType((uno::Sequence<rtl::OUString>*)0));
735 
736     }
737     else if ( aURL.Path == C2U("Bib/source"))
738     {
739         aEvent.IsEnabled  = sal_True;
740         aEvent.FeatureDescriptor=pDatMan->getActiveDataTable();
741 
742         uno::Sequence<rtl::OUString> aStringSeq=pDatMan->getDataSources();
743         aEvent.State.setValue(&aStringSeq,::getCppuType((uno::Sequence<rtl::OUString>*)0));
744     }
745     else if(aURL.Path == C2U("Bib/sdbsource") ||
746             aURL.Path == C2U("Bib/Mapping") ||
747             aURL.Path == C2U("Bib/autoFilter") ||
748             aURL.Path.equalsAscii("Bib/standardFilter"))
749     {
750         aEvent.IsEnabled  = sal_True;
751     }
752     else if(aURL.Path == C2U("Bib/query"))
753     {
754         aEvent.IsEnabled  = sal_True;
755         aEvent.State <<= pConfig->getQueryText();
756     }
757     else if (aURL.Path == C2U("Bib/removeFilter") )
758     {
759         rtl::OUString aFilterStr=pDatMan->getFilter();
760         aEvent.IsEnabled  = (aFilterStr.getLength() > 0);
761     }
762     else if(aURL.Path == C2U("Cut"))
763     {
764         Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
765         Edit* pEdit = dynamic_cast<Edit*>( pChild );
766         if( pEdit )
767             aEvent.IsEnabled  = !pEdit->IsReadOnly() && pEdit->GetSelection().Len();
768     }
769     if(aURL.Path == C2U("Copy"))
770     {
771         Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
772         Edit* pEdit = dynamic_cast<Edit*>( pChild );
773         if( pEdit )
774             aEvent.IsEnabled  = pEdit->GetSelection().Len() > 0;
775     }
776     else if(aURL.Path == C2U("Paste") )
777     {
778         aEvent.IsEnabled  = sal_False;
779         Window* pChild = lcl_GetFocusChild( VCLUnoHelper::GetWindow( xWindow ) );
780         if(pChild)
781         {
782             uno::Reference< datatransfer::clipboard::XClipboard > xClip = pChild->GetClipboard();
783             if(xClip.is())
784             {
785                 uno::Reference< datatransfer::XTransferable > xDataObj;
786                 const sal_uInt32 nRef = Application::ReleaseSolarMutex();
787                 try
788                 {
789                     xDataObj = xClip->getContents();
790                 }
791                 catch( const uno::Exception& )
792                 {
793                 }
794                 Application::AcquireSolarMutex( nRef );
795 
796                 if ( xDataObj.is() )
797                 {
798                     datatransfer::DataFlavor aFlavor;
799                     SotExchange::GetFormatDataFlavor( SOT_FORMAT_STRING, aFlavor );
800                     try
801                     {
802                         uno::Any aData = xDataObj->getTransferData( aFlavor );
803                         ::rtl::OUString aText;
804                         aData >>= aText;
805                         aEvent.IsEnabled  = aText.getLength() > 0;
806                     }
807                     catch( const uno::Exception& )
808                     {
809                     }
810                 }
811             }
812             uno::Reference< datatransfer::XTransferable > xContents = xClip->getContents(  );
813         }
814     }
815     else if(aURL.Path == C2U("Bib/DeleteRecord"))
816     {
817         Reference< ::com::sun::star::sdbc::XResultSet >  xCursor(pDatMan->getForm(), UNO_QUERY);
818         Reference< XResultSetUpdate >  xUpdateCursor(xCursor, UNO_QUERY);
819         Reference< beans::XPropertySet >  xSet(pDatMan->getForm(), UNO_QUERY);
820         sal_Bool  bIsNew  = ::comphelper::getBOOL(xSet->getPropertyValue(C2U("IsNew")));
821         if(!bIsNew)
822         {
823             sal_uInt32 nCount = 0;
824             xSet->getPropertyValue(C2U("RowCount")) >>= nCount;
825             aEvent.IsEnabled  = nCount > 0;
826         }
827     }
828     else if (aURL.Path == C2U("Bib/InsertRecord"))
829     {
830         Reference< beans::XPropertySet >  xSet(pDatMan->getForm(), UNO_QUERY);
831         aEvent.IsEnabled = canInsertRecords(xSet);
832     }
833     aListener->statusChanged( aEvent );
834 }
835 //-----------------------------------------------------------------------------
removeStatusListener(const uno::Reference<frame::XStatusListener> & aObject,const util::URL & aURL)836 void BibFrameController_Impl::removeStatusListener(
837     const uno::Reference< frame::XStatusListener > & aObject, const util::URL& aURL)
838     throw (::com::sun::star::uno::RuntimeException)
839 {
840     // search listener array for given listener
841     // for checking equality always "cast" to XInterface
842     if ( !bDisposing )
843     {
844         sal_uInt16 nCount = aStatusListeners.Count();
845         for ( sal_uInt16 n=0; n<nCount; n++ )
846         {
847             BibStatusDispatch *pObj = aStatusListeners[n];
848             sal_Bool bFlag=pObj->xListener.is();
849             if (!bFlag || (pObj->xListener == aObject &&
850                 ( !aURL.Complete.getLength() || pObj->aURL.Path == aURL.Path  )))
851             {
852                 aStatusListeners.DeleteAndDestroy( n );
853                 break;
854             }
855         }
856     }
857 }
858 //-----------------------------------------------------------------------------
RemoveFilter()859 void BibFrameController_Impl::RemoveFilter()
860 {
861     rtl::OUString aQuery;
862     pDatMan->startQueryWith(aQuery);
863 
864     sal_uInt16 nCount = aStatusListeners.Count();
865 
866     sal_Bool bRemoveFilter=sal_False;
867     sal_Bool bQueryText=sal_False;
868 
869     for ( sal_uInt16 n=0; n<nCount; n++ )
870     {
871         BibStatusDispatch *pObj = aStatusListeners[n];
872         if ( pObj->aURL.Path == C2U("Bib/removeFilter") )
873         {
874             FeatureStateEvent  aEvent;
875             aEvent.FeatureURL = pObj->aURL;
876             aEvent.IsEnabled  = sal_False;
877             aEvent.Requery    = sal_False;
878             aEvent.Source     = (XDispatch *) this;
879             pObj->xListener->statusChanged( aEvent );
880             bRemoveFilter=sal_True;
881         }
882         else if(pObj->aURL.Path == C2U("Bib/query"))
883         {
884             FeatureStateEvent  aEvent;
885             aEvent.FeatureURL = pObj->aURL;
886             aEvent.IsEnabled  = sal_True;
887             aEvent.Requery    = sal_False;
888             aEvent.Source     = (XDispatch *) this;
889             aEvent.State <<= aQuery;
890             pObj->xListener->statusChanged( aEvent );
891             bQueryText=sal_True;
892         }
893 
894         if(bRemoveFilter && bQueryText)
895             break;
896 
897     }
898 }
899 //-----------------------------------------------------------------------------
ChangeDataSource(const uno::Sequence<beans::PropertyValue> & aArgs)900 void BibFrameController_Impl::ChangeDataSource(const uno::Sequence< beans::PropertyValue >& aArgs)
901 {
902     const beans::PropertyValue* pPropertyValue = aArgs.getConstArray();
903     uno::Any aValue=pPropertyValue[0].Value;
904     rtl::OUString aDBTableName;
905     aValue >>= aDBTableName;
906 
907 
908     if(aArgs.getLength() > 1)
909     {
910         uno::Any aDB = pPropertyValue[1].Value;
911         rtl::OUString aURL;
912         aDB >>= aURL;
913         pDatMan->setActiveDataSource(aURL);
914         aDBTableName = pDatMan->getActiveDataTable();
915     }
916     else
917     {
918         m_xDatMan->unload();
919         pDatMan->setActiveDataTable(aDBTableName);
920         pDatMan->updateGridModel();
921         m_xDatMan->load();
922     }
923 
924 
925     sal_uInt16 nCount = aStatusListeners.Count();
926 
927     sal_Bool bMenuFilter=sal_False;
928     sal_Bool bQueryText=sal_False;
929     for ( sal_uInt16 n=0; n<nCount; n++ )
930     {
931         BibStatusDispatch *pObj = aStatusListeners[n];
932         if(COMPARE_EQUAL == pObj->aURL.Path.compareToAscii("Bib/MenuFilter"))
933         {
934             FeatureStateEvent  aEvent;
935             aEvent.FeatureURL = pObj->aURL;
936             aEvent.IsEnabled  = sal_True;
937             aEvent.Requery    = sal_False;
938             aEvent.Source     = (XDispatch *) this;
939             aEvent.FeatureDescriptor=pDatMan->getQueryField();
940 
941             uno::Sequence<rtl::OUString> aStringSeq=pDatMan->getQueryFields();
942             aEvent.State  = makeAny( aStringSeq );
943 
944             pObj->xListener->statusChanged( aEvent );
945             bMenuFilter=sal_True;
946         }
947         else if(COMPARE_EQUAL == pObj->aURL.Path.compareToAscii("Bib/query"))
948         {
949             FeatureStateEvent  aEvent;
950             aEvent.FeatureURL = pObj->aURL;
951             aEvent.IsEnabled  = sal_True;
952             aEvent.Requery    = sal_False;
953             aEvent.Source     = (XDispatch *) this;
954             BibConfig* pConfig = BibModul::GetConfig();
955             aEvent.State <<= pConfig->getQueryText();
956             pObj->xListener->statusChanged( aEvent );
957             bQueryText=sal_True;
958         }
959 
960         if (bMenuFilter && bQueryText)
961             break;
962 
963     }
964 }
965 
activate()966 void BibFrameController_Impl::activate()
967 {
968 }
deactivate()969 void BibFrameController_Impl::deactivate()
970 {
971 }
972 
973 
974