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