xref: /aoo41x/main/sw/source/core/edit/edfldexp.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 #include <editsh.hxx>
31 #include <dbfld.hxx>
32 #include <dbmgr.hxx>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <doc.hxx>
37 #include <docary.hxx>
38 #include <ndtxt.hxx>		// GetCurFld
39 #include <txtfld.hxx>
40 #include <fmtfld.hxx>
41 #include <edimp.hxx>
42 #include <flddat.hxx>
43 #include <switerator.hxx>
44 
45 using namespace com::sun::star;
46 using ::rtl::OUString;
47 
48 sal_Bool SwEditShell::IsFieldDataSourceAvailable(String& rUsedDataSource) const
49 {
50     const SwFldTypes * pFldTypes = GetDoc()->GetFldTypes();
51     const sal_uInt16 nSize = pFldTypes->Count();
52     uno::Reference< lang::XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
53     if( !xMgr.is() )
54         return sal_False;
55     uno::Reference<uno::XInterface> xInstance = xMgr->createInstance( OUString::createFromAscii( "com.sun.star.sdb.DatabaseContext" ));
56     uno::Reference<container::XNameAccess> xDBContext(xInstance, uno::UNO_QUERY) ;
57     if(!xDBContext.is())
58         return sal_False;
59     for(sal_uInt16 i = 0; i < nSize; ++i)
60     {
61         SwFieldType& rFldType = *((*pFldTypes)[i]);
62         sal_uInt16 nWhich = rFldType.Which();
63         if(IsUsed(rFldType))
64         {
65             switch(nWhich)
66             {
67                 case RES_DBFLD:
68                 {
69                     SwIterator<SwFmtFld,SwFieldType> aIter( rFldType );
70                     SwFmtFld* pFld = aIter.First();
71                     while(pFld)
72                     {
73                         if(pFld->IsFldInDoc())
74                         {
75                             const SwDBData& rData =
76                                     ((SwDBFieldType*)pFld->GetFld()->GetTyp())->GetDBData();
77 							try
78 							{
79 								return xDBContext->getByName(rData.sDataSource).hasValue();
80 							}
81 							catch(uno::Exception const &)
82 							{
83 								rUsedDataSource = rData.sDataSource;
84 								return sal_False;
85 							}
86                         }
87                         pFld = aIter.Next();
88                     }
89                 }
90                 break;
91             }
92         }
93     }
94     return sal_True;
95 }
96 
97 
98