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