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