LibreOffice Module sw (master)  1
edfldexp.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <editsh.hxx>
21 #include <dbfld.hxx>
22 #include <dbmgr.hxx>
24 #include <com/sun/star/sdb/DatabaseContext.hpp>
25 #include <doc.hxx>
27 #include <docary.hxx>
28 #include <txtfld.hxx>
29 #include <fmtfld.hxx>
30 #include <edimp.hxx>
31 #include <flddat.hxx>
32 #include <calbck.hxx>
33 
34 using namespace com::sun::star;
35 
36 bool SwEditShell::IsFieldDataSourceAvailable(OUString& rUsedDataSource) const
37 {
38  const SwFieldTypes * pFieldTypes = GetDoc()->getIDocumentFieldsAccess().GetFieldTypes();
39  uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
40  uno::Reference<sdb::XDatabaseContext> xDBContext = sdb::DatabaseContext::create(xContext);
41  for(const auto & pFieldType : *pFieldTypes)
42  {
43  if(IsUsed(*pFieldType) && pFieldType->Which() == SwFieldIds::Database)
44  {
45  SwIterator<SwFormatField,SwFieldType> aIter( *pFieldType );
46  SwFormatField* pFormatField = aIter.First();
47  while(pFormatField)
48  {
49  if(pFormatField->IsFieldInDoc())
50  {
51  const SwDBData& rData =
52  static_cast<SwDBFieldType*>(pFormatField->GetField()->GetTyp())->GetDBData();
53  try
54  {
55  return xDBContext->getByName(rData.sDataSource).hasValue();
56  }
57  catch(uno::Exception const &)
58  {
59  rUsedDataSource = rData.sDataSource;
60  return false;
61  }
62  }
63  pFormatField = aIter.Next();
64  }
65  }
66  }
67  return true;
68 }
69 
70 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SwField * GetField() const
Definition: fmtfld.hxx:70
OUString sDataSource
Definition: swdbdata.hxx:30
TElementType * Next()
Definition: calbck.hxx:373
For old documents the Field-Which IDs must be preserved !!!
bool IsFieldDataSourceAvailable(OUString &rUsedDataSource) const
Check whether DB fields point to an available data source and returns it.
Definition: edfldexp.cxx:36
TElementType * First()
Definition: calbck.hxx:342
SwFieldType * GetTyp() const
Definition: fldbas.hxx:383
bool IsFieldInDoc() const
Definition: atrfld.cxx:329
Reference< XComponentContext > getProcessComponentContext()