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_sw.hxx" 26 #include <editsh.hxx> 27 #include <dbfld.hxx> 28 #include <dbmgr.hxx> 29 #include <com/sun/star/container/XNameAccess.hpp> 30 #include <comphelper/processfactory.hxx> 31 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 32 #include <doc.hxx> 33 #include <docary.hxx> 34 #include <ndtxt.hxx> // GetCurFld 35 #include <txtfld.hxx> 36 #include <fmtfld.hxx> 37 #include <edimp.hxx> 38 #include <flddat.hxx> 39 #include <switerator.hxx> 40 41 using namespace com::sun::star; 42 using ::rtl::OUString; 43 44 sal_Bool SwEditShell::IsFieldDataSourceAvailable(String& rUsedDataSource) const 45 { 46 const SwFldTypes * pFldTypes = GetDoc()->GetFldTypes(); 47 const sal_uInt16 nSize = pFldTypes->Count(); 48 uno::Reference< lang::XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() ); 49 if( !xMgr.is() ) 50 return sal_False; 51 uno::Reference<uno::XInterface> xInstance = xMgr->createInstance( OUString::createFromAscii( "com.sun.star.sdb.DatabaseContext" )); 52 uno::Reference<container::XNameAccess> xDBContext(xInstance, uno::UNO_QUERY) ; 53 if(!xDBContext.is()) 54 return sal_False; 55 for(sal_uInt16 i = 0; i < nSize; ++i) 56 { 57 SwFieldType& rFldType = *((*pFldTypes)[i]); 58 sal_uInt16 nWhich = rFldType.Which(); 59 if(IsUsed(rFldType)) 60 { 61 switch(nWhich) 62 { 63 case RES_DBFLD: 64 { 65 SwIterator<SwFmtFld,SwFieldType> aIter( rFldType ); 66 SwFmtFld* pFmtFld = aIter.First(); 67 while(pFmtFld) 68 { 69 if(pFmtFld->IsFldInDoc()) 70 { 71 const SwDBData& rData = 72 ((SwDBFieldType*)pFmtFld->GetField()->GetTyp())->GetDBData(); 73 try 74 { 75 return xDBContext->getByName(rData.sDataSource).hasValue(); 76 } 77 catch(uno::Exception const &) 78 { 79 rUsedDataSource = rData.sDataSource; 80 return sal_False; 81 } 82 } 83 pFmtFld = aIter.Next(); 84 } 85 } 86 break; 87 } 88 } 89 } 90 return sal_True; 91 } 92 93 94