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 .
21 #undef SC_DLLIMPLEMENTATION
25 //------------------------------------------------------------------
27 #include <vcl/waitobj.hxx>
28 #include <comphelper/processfactory.hxx>
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>
38 using namespace com::sun::star
;
40 #include "dapidata.hxx"
41 #include "scresid.hxx"
43 #include "dapitype.hrc"
44 #include "miscuno.hxx"
45 #include "dpsdbtab.hxx" // ScImportSourceDesc
47 //-------------------------------------------------------------------------
50 // entries in the "type" ListBox
51 #define DP_TYPELIST_TABLE 0
52 #define DP_TYPELIST_QUERY 1
53 #define DP_TYPELIST_SQLNAT 3
55 //-------------------------------------------------------------------------
57 ScDataPilotDatabaseDlg::ScDataPilotDatabaseDlg( Window
* pParent
) :
58 ModalDialog ( pParent
, ScResId( RID_SCDLG_DAPIDATA
) ),
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 aBtnHelp ( this, ScResId( BTN_HELP
) )
73 WaitObject
aWait( this ); // initializing the database service the first time takes a while
79 uno::Reference
<sdb::XDatabaseContext
> xContext
= sdb::DatabaseContext::create(
80 comphelper::getProcessComponentContext() );
81 uno::Sequence
<OUString
> aNames
= xContext
->getElementNames();
82 long nCount
= aNames
.getLength();
83 const OUString
* pArray
= aNames
.getConstArray();
84 for (long nPos
= 0; nPos
< nCount
; nPos
++)
86 String aName
= pArray
[nPos
];
87 aLbDatabase
.InsertEntry( aName
);
90 catch(uno::Exception
&)
92 OSL_FAIL("exception in database");
95 aLbDatabase
.SelectEntryPos( 0 );
96 aLbType
.SelectEntryPos( 0 );
100 aLbDatabase
.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
101 aLbType
.SetSelectHdl( LINK( this, ScDataPilotDatabaseDlg
, SelectHdl
) );
104 ScDataPilotDatabaseDlg::~ScDataPilotDatabaseDlg()
108 void ScDataPilotDatabaseDlg::GetValues( ScImportSourceDesc
& rDesc
)
110 sal_uInt16 nSelect
= aLbType
.GetSelectEntryPos();
112 rDesc
.aDBName
= aLbDatabase
.GetSelectEntry();
113 rDesc
.aObject
= aCbObject
.GetText();
115 if (rDesc
.aDBName
.isEmpty() || rDesc
.aObject
.isEmpty())
116 rDesc
.nType
= sheet::DataImportMode_NONE
;
117 else if ( nSelect
== DP_TYPELIST_TABLE
)
118 rDesc
.nType
= sheet::DataImportMode_TABLE
;
119 else if ( nSelect
== DP_TYPELIST_QUERY
)
120 rDesc
.nType
= sheet::DataImportMode_QUERY
;
122 rDesc
.nType
= sheet::DataImportMode_SQL
;
124 rDesc
.bNative
= ( nSelect
== DP_TYPELIST_SQLNAT
);
127 IMPL_LINK_NOARG(ScDataPilotDatabaseDlg
, SelectHdl
)
133 void ScDataPilotDatabaseDlg::FillObjects()
137 String aDatabaseName
= aLbDatabase
.GetSelectEntry();
138 if (!aDatabaseName
.Len())
141 sal_uInt16 nSelect
= aLbType
.GetSelectEntryPos();
142 if ( nSelect
> DP_TYPELIST_QUERY
)
143 return; // only tables and queries
147 // open connection (for tables or queries)
149 uno::Reference
<sdb::XDatabaseContext
> xContext
= sdb::DatabaseContext::create(
150 comphelper::getProcessComponentContext() );
152 uno::Any aSourceAny
= xContext
->getByName( aDatabaseName
);
153 uno::Reference
<sdb::XCompletedConnection
> xSource(
154 ScUnoHelpFunctions::AnyToInterface( aSourceAny
), uno::UNO_QUERY
);
155 if ( !xSource
.is() ) return;
157 uno::Reference
<task::XInteractionHandler
> xHandler(
158 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
159 uno::UNO_QUERY_THROW
);
161 uno::Reference
<sdbc::XConnection
> xConnection
= xSource
->connectWithCompletion( xHandler
);
163 uno::Sequence
<OUString
> aNames
;
164 if ( nSelect
== DP_TYPELIST_TABLE
)
168 uno::Reference
<sdbcx::XTablesSupplier
> xTablesSupp( xConnection
, uno::UNO_QUERY
);
169 if ( !xTablesSupp
.is() ) return;
171 uno::Reference
<container::XNameAccess
> xTables
= xTablesSupp
->getTables();
172 if ( !xTables
.is() ) return;
174 aNames
= xTables
->getElementNames();
180 uno::Reference
<sdb::XQueriesSupplier
> xQueriesSupp( xConnection
, uno::UNO_QUERY
);
181 if ( !xQueriesSupp
.is() ) return;
183 uno::Reference
<container::XNameAccess
> xQueries
= xQueriesSupp
->getQueries();
184 if ( !xQueries
.is() ) return;
186 aNames
= xQueries
->getElementNames();
191 long nCount
= aNames
.getLength();
192 const OUString
* pArray
= aNames
.getConstArray();
193 for( long nPos
=0; nPos
<nCount
; nPos
++ )
195 String aName
= pArray
[nPos
];
196 aCbObject
.InsertEntry( aName
);
199 catch(uno::Exception
&)
201 // this may happen if an invalid database is selected -> no DBG_ERROR
202 OSL_FAIL("exception in database");
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */