1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #undef SC_DLLIMPLEMENTATION
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/diagnose_ex.hxx>
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>
32 #include <dapidata.hxx>
33 #include <dpsdbtab.hxx>
35 using namespace com::sun::star
;
37 // entries in the "type" ListBox
38 #define DP_TYPELIST_TABLE 0
39 #define DP_TYPELIST_QUERY 1
40 #define DP_TYPELIST_SQLNAT 3
42 ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg(weld::Window
* pParent
)
43 : GenericDialogController(pParent
, u
"modules/scalc/ui/selectdatasource.ui"_ustr
, u
"SelectDataSourceDialog"_ustr
)
44 , m_xLbDatabase(m_xBuilder
->weld_combo_box(u
"database"_ustr
))
45 , m_xCbObject(m_xBuilder
->weld_combo_box(u
"datasource"_ustr
))
46 , m_xLbType(m_xBuilder
->weld_combo_box(u
"type"_ustr
))
48 weld::WaitObject
aWait(pParent
); // initializing the database service the first time takes a while
54 uno::Reference
<sdb::XDatabaseContext
> xContext
= sdb::DatabaseContext::create(
55 comphelper::getProcessComponentContext() );
56 const uno::Sequence
<OUString
> aNames
= xContext
->getElementNames();
57 for( const OUString
& aName
: aNames
)
59 m_xLbDatabase
->append_text(aName
);
62 catch(uno::Exception
&)
64 TOOLS_WARN_EXCEPTION( "sc", "exception in database");
67 m_xLbDatabase
->set_active(0);
68 m_xLbType
->set_active(0);
72 m_xLbDatabase
->connect_changed( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
73 m_xLbType
->connect_changed( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
76 ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
80 void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc
& rDesc
)
82 const sal_Int32 nSelect
= m_xLbType
->get_active();
84 rDesc
.aDBName
= m_xLbDatabase
->get_active_text();
85 rDesc
.aObject
= m_xCbObject
->get_active_text();
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
;
94 rDesc
.nType
= sheet::DataImportMode_SQL
;
96 rDesc
.bNative
= ( nSelect
== DP_TYPELIST_SQLNAT
);
99 IMPL_LINK_NOARG(ScDataPilotDatabaseDlg
, SelectHdl
, weld::ComboBox
&, void)
104 void ScDataPilotDatabaseDlg::FillObjects()
106 m_xCbObject
->clear();
108 OUString aDatabaseName
= m_xLbDatabase
->get_active_text();
109 if (aDatabaseName
.isEmpty())
112 const int nSelect
= m_xLbType
->get_active();
113 if ( nSelect
> DP_TYPELIST_QUERY
)
114 return; // only tables and queries
118 // open connection (for tables or queries)
120 uno::Reference
<sdb::XDatabaseContext
> xContext
= sdb::DatabaseContext::create(
121 comphelper::getProcessComponentContext() );
123 uno::Any aSourceAny
= xContext
->getByName( aDatabaseName
);
124 uno::Reference
<sdb::XCompletedConnection
> xSource(aSourceAny
, uno::UNO_QUERY
);
125 if ( !xSource
.is() ) return;
127 uno::Reference
<task::XInteractionHandler
> xHandler(
128 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), nullptr),
129 uno::UNO_QUERY_THROW
);
131 uno::Reference
<sdbc::XConnection
> xConnection
= xSource
->connectWithCompletion( xHandler
);
133 uno::Reference
<container::XNameAccess
> xItems
;
134 if ( nSelect
== DP_TYPELIST_TABLE
)
138 uno::Reference
<sdbcx::XTablesSupplier
> xTablesSupp( xConnection
, uno::UNO_QUERY
);
139 if ( !xTablesSupp
.is() ) return;
141 xItems
= xTablesSupp
->getTables();
147 uno::Reference
<sdb::XQueriesSupplier
> xQueriesSupp( xConnection
, uno::UNO_QUERY
);
148 if ( !xQueriesSupp
.is() ) return;
150 xItems
= xQueriesSupp
->getQueries();
153 if ( !xItems
.is() ) return;
156 const uno::Sequence
<OUString
> aNames
= xItems
->getElementNames();
157 for( const OUString
& aName
: aNames
)
159 m_xCbObject
->append_text(aName
);
162 catch(uno::Exception
&)
164 // this may happen if an invalid database is selected -> no DBG_ERROR
165 TOOLS_WARN_EXCEPTION( "sc", "exception in database");
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */