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 <vcl/waitobj.hxx>
23 #include <comphelper/processfactory.hxx>
25 #include <com/sun/star/sheet/DataImportMode.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
28 #include <com/sun/star/sdb/DatabaseContext.hpp>
29 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
30 #include <com/sun/star/sdb/XCompletedConnection.hpp>
31 #include <com/sun/star/task/InteractionHandler.hpp>
33 using namespace com::sun::star
;
35 #include "dapidata.hxx"
36 #include "scresid.hxx"
38 #include "miscuno.hxx"
39 #include "dpsdbtab.hxx"
41 // entries in the "type" ListBox
42 #define DP_TYPELIST_TABLE 0
43 #define DP_TYPELIST_QUERY 1
44 #define DP_TYPELIST_SQLNAT 3
46 ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg( vcl::Window
* pParent
) :
47 ModalDialog(pParent
, "SelectDataSourceDialog",
48 "modules/scalc/ui/selectdatasource.ui")
50 get(m_pLbDatabase
, "database");
51 get(m_pCbObject
, "datasource");
52 get(m_pLbType
, "type");
54 WaitObject
aWait( this ); // initializing the database service the first time takes a while
60 uno::Reference
<sdb::XDatabaseContext
> xContext
= sdb::DatabaseContext::create(
61 comphelper::getProcessComponentContext() );
62 uno::Sequence
<OUString
> aNames
= xContext
->getElementNames();
63 long nCount
= aNames
.getLength();
64 const OUString
* pArray
= aNames
.getConstArray();
65 for (long nPos
= 0; nPos
< nCount
; nPos
++)
67 OUString aName
= pArray
[nPos
];
68 m_pLbDatabase
->InsertEntry( aName
);
71 catch(uno::Exception
&)
73 OSL_FAIL("exception in database");
76 m_pLbDatabase
->SelectEntryPos( 0 );
77 m_pLbType
->SelectEntryPos( 0 );
81 m_pLbDatabase
->SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
82 m_pLbType
->SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
85 ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
90 void ScDataPilotDatabaseDlg::dispose()
92 m_pLbDatabase
.clear();
95 ModalDialog::dispose();
98 void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc
& rDesc
)
100 sal_uInt16 nSelect
= m_pLbType
->GetSelectEntryPos();
102 rDesc
.aDBName
= m_pLbDatabase
->GetSelectEntry();
103 rDesc
.aObject
= m_pCbObject
->GetText();
105 if (rDesc
.aDBName
.isEmpty() || rDesc
.aObject
.isEmpty())
106 rDesc
.nType
= sheet::DataImportMode_NONE
;
107 else if ( nSelect
== DP_TYPELIST_TABLE
)
108 rDesc
.nType
= sheet::DataImportMode_TABLE
;
109 else if ( nSelect
== DP_TYPELIST_QUERY
)
110 rDesc
.nType
= sheet::DataImportMode_QUERY
;
112 rDesc
.nType
= sheet::DataImportMode_SQL
;
114 rDesc
.bNative
= ( nSelect
== DP_TYPELIST_SQLNAT
);
117 IMPL_LINK_NOARG(ScDataPilotDatabaseDlg
, SelectHdl
)
123 void ScDataPilotDatabaseDlg::FillObjects()
125 m_pCbObject
->Clear();
127 OUString aDatabaseName
= m_pLbDatabase
->GetSelectEntry();
128 if (aDatabaseName
.isEmpty())
131 sal_uInt16 nSelect
= m_pLbType
->GetSelectEntryPos();
132 if ( nSelect
> DP_TYPELIST_QUERY
)
133 return; // only tables and queries
137 // open connection (for tables or queries)
139 uno::Reference
<sdb::XDatabaseContext
> xContext
= sdb::DatabaseContext::create(
140 comphelper::getProcessComponentContext() );
142 uno::Any aSourceAny
= xContext
->getByName( aDatabaseName
);
143 uno::Reference
<sdb::XCompletedConnection
> xSource(
144 ScUnoHelpFunctions::AnyToInterface( aSourceAny
), uno::UNO_QUERY
);
145 if ( !xSource
.is() ) return;
147 uno::Reference
<task::XInteractionHandler
> xHandler(
148 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
149 uno::UNO_QUERY_THROW
);
151 uno::Reference
<sdbc::XConnection
> xConnection
= xSource
->connectWithCompletion( xHandler
);
153 uno::Sequence
<OUString
> aNames
;
154 if ( nSelect
== DP_TYPELIST_TABLE
)
158 uno::Reference
<sdbcx::XTablesSupplier
> xTablesSupp( xConnection
, uno::UNO_QUERY
);
159 if ( !xTablesSupp
.is() ) return;
161 uno::Reference
<container::XNameAccess
> xTables
= xTablesSupp
->getTables();
162 if ( !xTables
.is() ) return;
164 aNames
= xTables
->getElementNames();
170 uno::Reference
<sdb::XQueriesSupplier
> xQueriesSupp( xConnection
, uno::UNO_QUERY
);
171 if ( !xQueriesSupp
.is() ) return;
173 uno::Reference
<container::XNameAccess
> xQueries
= xQueriesSupp
->getQueries();
174 if ( !xQueries
.is() ) return;
176 aNames
= xQueries
->getElementNames();
181 long nCount
= aNames
.getLength();
182 const OUString
* pArray
= aNames
.getConstArray();
183 for( long nPos
=0; nPos
<nCount
; nPos
++ )
185 OUString aName
= pArray
[nPos
];
186 m_pCbObject
->InsertEntry( aName
);
189 catch(uno::Exception
&)
191 // this may happen if an invalid database is selected -> no DBG_ERROR
192 OSL_FAIL("exception in database");
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */