LibreOffice Module sc (master)  1
dapidata.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 #undef SC_DLLIMPLEMENTATION
21 
23 #include <tools/diagnose_ex.h>
24 
25 #include <com/sun/star/sheet/DataImportMode.hpp>
26 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
27 #include <com/sun/star/sdb/DatabaseContext.hpp>
28 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
29 #include <com/sun/star/sdb/XCompletedConnection.hpp>
30 #include <com/sun/star/task/InteractionHandler.hpp>
31 
32 using namespace com::sun::star;
33 
34 #include <dapidata.hxx>
35 #include <dpsdbtab.hxx>
36 
37 // entries in the "type" ListBox
38 #define DP_TYPELIST_TABLE 0
39 #define DP_TYPELIST_QUERY 1
40 #define DP_TYPELIST_SQLNAT 3
41 
43  : GenericDialogController(pParent, "modules/scalc/ui/selectdatasource.ui", "SelectDataSourceDialog")
44  , m_xLbDatabase(m_xBuilder->weld_combo_box("database"))
45  , m_xCbObject(m_xBuilder->weld_combo_box("datasource"))
46  , m_xLbType(m_xBuilder->weld_combo_box("type"))
47 {
48  weld::WaitObject aWait(pParent); // initializing the database service the first time takes a while
49 
50  try
51  {
52  // get database names
53 
54  uno::Reference<sdb::XDatabaseContext> xContext = sdb::DatabaseContext::create(
56  const uno::Sequence<OUString> aNames = xContext->getElementNames();
57  for( const OUString& aName : aNames )
58  {
59  m_xLbDatabase->append_text(aName);
60  }
61  }
62  catch(uno::Exception&)
63  {
64  TOOLS_WARN_EXCEPTION( "sc", "exception in database");
65  }
66 
67  m_xLbDatabase->set_active(0);
68  m_xLbType->set_active(0);
69 
70  FillObjects();
71 
72  m_xLbDatabase->connect_changed( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
73  m_xLbType->connect_changed( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
74 }
75 
77 {
78 }
79 
81 {
82  const sal_Int32 nSelect = m_xLbType->get_active();
83 
84  rDesc.aDBName = m_xLbDatabase->get_active_text();
85  rDesc.aObject = m_xCbObject->get_active_text();
86 
87  if (rDesc.aDBName.isEmpty() || rDesc.aObject.isEmpty())
88  rDesc.nType = sheet::DataImportMode_NONE;
89  else if ( nSelect == DP_TYPELIST_TABLE )
90  rDesc.nType = sheet::DataImportMode_TABLE;
91  else if ( nSelect == DP_TYPELIST_QUERY )
92  rDesc.nType = sheet::DataImportMode_QUERY;
93  else
94  rDesc.nType = sheet::DataImportMode_SQL;
95 
96  rDesc.bNative = ( nSelect == DP_TYPELIST_SQLNAT );
97 }
98 
100 {
101  FillObjects();
102 }
103 
105 {
106  m_xCbObject->clear();
107 
108  OUString aDatabaseName = m_xLbDatabase->get_active_text();
109  if (aDatabaseName.isEmpty())
110  return;
111 
112  const int nSelect = m_xLbType->get_active();
113  if ( nSelect > DP_TYPELIST_QUERY )
114  return; // only tables and queries
115 
116  try
117  {
118  // open connection (for tables or queries)
119 
120  uno::Reference<sdb::XDatabaseContext> xContext = sdb::DatabaseContext::create(
122 
123  uno::Any aSourceAny = xContext->getByName( aDatabaseName );
124  uno::Reference<sdb::XCompletedConnection> xSource(aSourceAny, uno::UNO_QUERY);
125  if ( !xSource.is() ) return;
126 
127  uno::Reference<task::XInteractionHandler> xHandler(
128  task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), nullptr),
129  uno::UNO_QUERY_THROW);
130 
131  uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion( xHandler );
132 
133  uno::Reference<container::XNameAccess> xItems;
134  if ( nSelect == DP_TYPELIST_TABLE )
135  {
136  // get all tables
137 
138  uno::Reference<sdbcx::XTablesSupplier> xTablesSupp( xConnection, uno::UNO_QUERY );
139  if ( !xTablesSupp.is() ) return;
140 
141  xItems = xTablesSupp->getTables();
142  }
143  else
144  {
145  // get all queries
146 
147  uno::Reference<sdb::XQueriesSupplier> xQueriesSupp( xConnection, uno::UNO_QUERY );
148  if ( !xQueriesSupp.is() ) return;
149 
150  xItems = xQueriesSupp->getQueries();
151  }
152 
153  if ( !xItems.is() ) return;
154 
155  // fill list
156  const uno::Sequence<OUString> aNames = xItems->getElementNames();
157  for( const OUString& aName : aNames )
158  {
159  m_xCbObject->append_text(aName);
160  }
161  }
162  catch(uno::Exception&)
163  {
164  // this may happen if an invalid database is selected -> no DBG_ERROR
165  TOOLS_WARN_EXCEPTION( "sc", "exception in database");
166  }
167 }
168 
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define DP_TYPELIST_QUERY
Definition: dapidata.cxx:39
void GetValues(ScImportSourceDesc &rDesc)
Definition: dapidata.cxx:80
virtual ~ScDataPilotDatabaseDlg() override
Definition: dapidata.cxx:76
#define TOOLS_WARN_EXCEPTION(area, stream)
std::unique_ptr< weld::ComboBox > m_xLbDatabase
Definition: dapidata.hxx:29
IMPL_LINK_NOARG(ScDataPilotDatabaseDlg, SelectHdl, weld::ComboBox &, void)
Definition: dapidata.cxx:99
std::unique_ptr< weld::ComboBox > m_xCbObject
Definition: dapidata.hxx:30
OUString aDBName
Definition: dpsdbtab.hxx:35
css::sheet::DataImportMode nType
Definition: dpsdbtab.hxx:37
OUString aObject
Definition: dpsdbtab.hxx:36
OUString aName
if(aStr!=aBuf) UpdateName_Impl(m_xFollowLb.get()
Reference< XComponentContext > getProcessComponentContext()
ScDataPilotDatabaseDlg(weld::Window *pParent)
Definition: dapidata.cxx:42
#define DP_TYPELIST_TABLE
Definition: dapidata.cxx:38
std::unique_ptr< weld::ComboBox > m_xLbType
Definition: dapidata.hxx:31
#define DP_TYPELIST_SQLNAT
Definition: dapidata.cxx:40