Line data Source code
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 :
22 : #include <vcl/waitobj.hxx>
23 : #include <comphelper/processfactory.hxx>
24 :
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>
32 :
33 : using namespace com::sun::star;
34 :
35 : #include "dapidata.hxx"
36 : #include "scresid.hxx"
37 : #include "sc.hrc"
38 : #include "miscuno.hxx"
39 : #include "dpsdbtab.hxx"
40 :
41 : // entries in the "type" ListBox
42 : #define DP_TYPELIST_TABLE 0
43 : #define DP_TYPELIST_QUERY 1
44 : #define DP_TYPELIST_SQLNAT 3
45 :
46 0 : ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg( vcl::Window* pParent ) :
47 : ModalDialog(pParent, "SelectDataSourceDialog",
48 0 : "modules/scalc/ui/selectdatasource.ui")
49 : {
50 0 : get(m_pLbDatabase, "database");
51 0 : get(m_pCbObject, "datasource");
52 0 : get(m_pLbType, "type");
53 :
54 0 : WaitObject aWait( this ); // initializing the database service the first time takes a while
55 :
56 : try
57 : {
58 : // get database names
59 :
60 : uno::Reference<sdb::XDatabaseContext> xContext = sdb::DatabaseContext::create(
61 0 : comphelper::getProcessComponentContext() );
62 0 : uno::Sequence<OUString> aNames = xContext->getElementNames();
63 0 : long nCount = aNames.getLength();
64 0 : const OUString* pArray = aNames.getConstArray();
65 0 : for (long nPos = 0; nPos < nCount; nPos++)
66 : {
67 0 : OUString aName = pArray[nPos];
68 0 : m_pLbDatabase->InsertEntry( aName );
69 0 : }
70 : }
71 0 : catch(uno::Exception&)
72 : {
73 : OSL_FAIL("exception in database");
74 : }
75 :
76 0 : m_pLbDatabase->SelectEntryPos( 0 );
77 0 : m_pLbType->SelectEntryPos( 0 );
78 :
79 0 : FillObjects();
80 :
81 0 : m_pLbDatabase->SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
82 0 : m_pLbType->SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg, SelectHdl ) );
83 0 : }
84 :
85 0 : ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
86 : {
87 0 : disposeOnce();
88 0 : }
89 :
90 0 : void ScDataPilotDatabaseDlg::dispose()
91 : {
92 0 : m_pLbDatabase.clear();
93 0 : m_pCbObject.clear();
94 0 : m_pLbType.clear();
95 0 : ModalDialog::dispose();
96 0 : }
97 :
98 0 : void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc& rDesc )
99 : {
100 0 : sal_uInt16 nSelect = m_pLbType->GetSelectEntryPos();
101 :
102 0 : rDesc.aDBName = m_pLbDatabase->GetSelectEntry();
103 0 : rDesc.aObject = m_pCbObject->GetText();
104 :
105 0 : if (rDesc.aDBName.isEmpty() || rDesc.aObject.isEmpty())
106 0 : rDesc.nType = sheet::DataImportMode_NONE;
107 0 : else if ( nSelect == DP_TYPELIST_TABLE )
108 0 : rDesc.nType = sheet::DataImportMode_TABLE;
109 0 : else if ( nSelect == DP_TYPELIST_QUERY )
110 0 : rDesc.nType = sheet::DataImportMode_QUERY;
111 : else
112 0 : rDesc.nType = sheet::DataImportMode_SQL;
113 :
114 0 : rDesc.bNative = ( nSelect == DP_TYPELIST_SQLNAT );
115 0 : }
116 :
117 0 : IMPL_LINK_NOARG(ScDataPilotDatabaseDlg, SelectHdl)
118 : {
119 0 : FillObjects();
120 0 : return 0;
121 : }
122 :
123 0 : void ScDataPilotDatabaseDlg::FillObjects()
124 : {
125 0 : m_pCbObject->Clear();
126 :
127 0 : OUString aDatabaseName = m_pLbDatabase->GetSelectEntry();
128 0 : if (aDatabaseName.isEmpty())
129 0 : return;
130 :
131 0 : sal_uInt16 nSelect = m_pLbType->GetSelectEntryPos();
132 0 : if ( nSelect > DP_TYPELIST_QUERY )
133 0 : return; // only tables and queries
134 :
135 : try
136 : {
137 : // open connection (for tables or queries)
138 :
139 : uno::Reference<sdb::XDatabaseContext> xContext = sdb::DatabaseContext::create(
140 0 : comphelper::getProcessComponentContext() );
141 :
142 0 : uno::Any aSourceAny = xContext->getByName( aDatabaseName );
143 : uno::Reference<sdb::XCompletedConnection> xSource(
144 0 : ScUnoHelpFunctions::AnyToInterface( aSourceAny ), uno::UNO_QUERY );
145 0 : if ( !xSource.is() ) return;
146 :
147 : uno::Reference<task::XInteractionHandler> xHandler(
148 : task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
149 0 : uno::UNO_QUERY_THROW);
150 :
151 0 : uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion( xHandler );
152 :
153 0 : uno::Sequence<OUString> aNames;
154 0 : if ( nSelect == DP_TYPELIST_TABLE )
155 : {
156 : // get all tables
157 :
158 0 : uno::Reference<sdbcx::XTablesSupplier> xTablesSupp( xConnection, uno::UNO_QUERY );
159 0 : if ( !xTablesSupp.is() ) return;
160 :
161 0 : uno::Reference<container::XNameAccess> xTables = xTablesSupp->getTables();
162 0 : if ( !xTables.is() ) return;
163 :
164 0 : aNames = xTables->getElementNames();
165 : }
166 : else
167 : {
168 : // get all queries
169 :
170 0 : uno::Reference<sdb::XQueriesSupplier> xQueriesSupp( xConnection, uno::UNO_QUERY );
171 0 : if ( !xQueriesSupp.is() ) return;
172 :
173 0 : uno::Reference<container::XNameAccess> xQueries = xQueriesSupp->getQueries();
174 0 : if ( !xQueries.is() ) return;
175 :
176 0 : aNames = xQueries->getElementNames();
177 : }
178 :
179 : // fill list
180 :
181 0 : long nCount = aNames.getLength();
182 0 : const OUString* pArray = aNames.getConstArray();
183 0 : for( long nPos=0; nPos<nCount; nPos++ )
184 : {
185 0 : OUString aName = pArray[nPos];
186 0 : m_pCbObject->InsertEntry( aName );
187 0 : }
188 : }
189 0 : catch(uno::Exception&)
190 : {
191 : // this may happen if an invalid database is selected -> no DBG_ERROR
192 : OSL_FAIL("exception in database");
193 0 : }
194 0 : }
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|