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