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 : void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc& rDesc )
86 : {
87 0 : sal_uInt16 nSelect = m_pLbType->GetSelectEntryPos();
88 :
89 0 : rDesc.aDBName = m_pLbDatabase->GetSelectEntry();
90 0 : rDesc.aObject = m_pCbObject->GetText();
91 :
92 0 : if (rDesc.aDBName.isEmpty() || rDesc.aObject.isEmpty())
93 0 : rDesc.nType = sheet::DataImportMode_NONE;
94 0 : else if ( nSelect == DP_TYPELIST_TABLE )
95 0 : rDesc.nType = sheet::DataImportMode_TABLE;
96 0 : else if ( nSelect == DP_TYPELIST_QUERY )
97 0 : rDesc.nType = sheet::DataImportMode_QUERY;
98 : else
99 0 : rDesc.nType = sheet::DataImportMode_SQL;
100 :
101 0 : rDesc.bNative = ( nSelect == DP_TYPELIST_SQLNAT );
102 0 : }
103 :
104 0 : IMPL_LINK_NOARG(ScDataPilotDatabaseDlg, SelectHdl)
105 : {
106 0 : FillObjects();
107 0 : return 0;
108 : }
109 :
110 0 : void ScDataPilotDatabaseDlg::FillObjects()
111 : {
112 0 : m_pCbObject->Clear();
113 :
114 0 : OUString aDatabaseName = m_pLbDatabase->GetSelectEntry();
115 0 : if (aDatabaseName.isEmpty())
116 0 : return;
117 :
118 0 : sal_uInt16 nSelect = m_pLbType->GetSelectEntryPos();
119 0 : if ( nSelect > DP_TYPELIST_QUERY )
120 0 : return; // only tables and queries
121 :
122 : try
123 : {
124 : // open connection (for tables or queries)
125 :
126 : uno::Reference<sdb::XDatabaseContext> xContext = sdb::DatabaseContext::create(
127 0 : comphelper::getProcessComponentContext() );
128 :
129 0 : uno::Any aSourceAny = xContext->getByName( aDatabaseName );
130 : uno::Reference<sdb::XCompletedConnection> xSource(
131 0 : ScUnoHelpFunctions::AnyToInterface( aSourceAny ), uno::UNO_QUERY );
132 0 : if ( !xSource.is() ) return;
133 :
134 : uno::Reference<task::XInteractionHandler> xHandler(
135 : task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
136 0 : uno::UNO_QUERY_THROW);
137 :
138 0 : uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion( xHandler );
139 :
140 0 : uno::Sequence<OUString> aNames;
141 0 : if ( nSelect == DP_TYPELIST_TABLE )
142 : {
143 : // get all tables
144 :
145 0 : uno::Reference<sdbcx::XTablesSupplier> xTablesSupp( xConnection, uno::UNO_QUERY );
146 0 : if ( !xTablesSupp.is() ) return;
147 :
148 0 : uno::Reference<container::XNameAccess> xTables = xTablesSupp->getTables();
149 0 : if ( !xTables.is() ) return;
150 :
151 0 : aNames = xTables->getElementNames();
152 : }
153 : else
154 : {
155 : // get all queries
156 :
157 0 : uno::Reference<sdb::XQueriesSupplier> xQueriesSupp( xConnection, uno::UNO_QUERY );
158 0 : if ( !xQueriesSupp.is() ) return;
159 :
160 0 : uno::Reference<container::XNameAccess> xQueries = xQueriesSupp->getQueries();
161 0 : if ( !xQueries.is() ) return;
162 :
163 0 : aNames = xQueries->getElementNames();
164 : }
165 :
166 : // fill list
167 :
168 0 : long nCount = aNames.getLength();
169 0 : const OUString* pArray = aNames.getConstArray();
170 0 : for( long nPos=0; nPos<nCount; nPos++ )
171 : {
172 0 : OUString aName = pArray[nPos];
173 0 : m_pCbObject->InsertEntry( aName );
174 0 : }
175 : }
176 0 : catch(uno::Exception&)
177 : {
178 : // this may happen if an invalid database is selected -> no DBG_ERROR
179 : OSL_FAIL("exception in database");
180 0 : }
181 0 : }
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|