LibreOffice Module dbaccess (master) 1
dlgsave.cxx
Go to the documentation of this file.
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#include <dlgsave.hxx>
21#include <core_resource.hxx>
22#include <strings.hrc>
23#include <com/sun/star/sdb/CommandType.hpp>
24#include <com/sun/star/sdbc/XRow.hpp>
26#include <UITools.hxx>
27#include <objectnamecheck.hxx>
28#include <utility>
30
31using namespace dbaui;
32using namespace dbtools;
33using namespace ::com::sun::star::uno;
34using namespace ::com::sun::star::container;
35using namespace ::com::sun::star::sdb;
36using namespace ::com::sun::star::sdbc;
37using namespace ::com::sun::star::lang;
38
39
40IMPL_LINK(OSaveAsDlg, TextFilterHdl, OUString&, rTest, bool)
41{
42 OUString sCorrected;
43 if (m_aChecker.checkString(rTest, sCorrected))
44 rTest = sCorrected;
45 return true;
46}
47
48namespace
49{
50typedef Reference< XResultSet > (SAL_CALL XDatabaseMetaData::*FGetMetaStrings)();
51
52void lcl_fillComboList( weld::ComboBox& _rList, const Reference< XConnection >& _rxConnection,
53 FGetMetaStrings GetAll, const OUString& _rCurrent )
54{
55 try {
56 Reference< XDatabaseMetaData > xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW );
57
58 Reference< XResultSet > xRes = (xMetaData.get()->*GetAll)();
59 Reference< XRow > xRow( xRes, UNO_QUERY_THROW );
60 OUString sValue;
61 while ( xRes->next() ) {
62 sValue = xRow->getString( 1 );
63 if ( !xRow->wasNull() )
64 _rList.append_text( sValue );
65 }
66
67 int nPos = _rList.find_text( _rCurrent );
68 if (nPos != -1)
69 _rList.set_active( nPos );
70 else
71 _rList.set_active( 0 );
72 } catch( const Exception& ) {
73 DBG_UNHANDLED_EXCEPTION("dbaccess");
74 }
75}
76}
77
78OSaveAsDlg::OSaveAsDlg( weld::Window * pParent,
79 sal_Int32 _rType,
80 const Reference< XComponentContext >& _rxContext,
81 const Reference< XConnection>& _xConnection,
82 const OUString& rDefault,
83 const IObjectNameCheck& _rObjectNameCheck,
84 SADFlags _nFlags)
85 : GenericDialogController(pParent, "dbaccess/ui/savedialog.ui", "SaveDialog")
86 , m_xContext( _rxContext )
87 , m_aName(rDefault)
88 , m_rObjectNameCheck( _rObjectNameCheck )
89 , m_nType(_rType)
90 , m_nFlags(_nFlags)
91 , m_aChecker(OUString())
92 , m_xDescription(m_xBuilder->weld_label("descriptionft"))
93 , m_xCatalogLbl(m_xBuilder->weld_label("catalogft"))
94 , m_xCatalog(m_xBuilder->weld_combo_box("catalog"))
95 , m_xSchemaLbl(m_xBuilder->weld_label("schemaft"))
96 , m_xSchema(m_xBuilder->weld_combo_box("schema"))
97 , m_xLabel(m_xBuilder->weld_label("titleft"))
98 , m_xTitle(m_xBuilder->weld_entry("title"))
99 , m_xPB_OK(m_xBuilder->weld_button("ok"))
100{
101 if ( _xConnection.is() )
102 m_xMetaData = _xConnection->getMetaData();
103
104 if (m_xMetaData.is())
105 {
106 OUString sExtraNameChars(m_xMetaData->getExtraNameCharacters());
107 m_aChecker.setAllowedChars(sExtraNameChars);
108 }
109
110 m_xTitle->connect_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
111 m_xSchema->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
112 m_xCatalog->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
113
114 switch (_rType) {
116 implInitOnlyTitle(DBA_RES(STR_QRY_LABEL));
117 break;
118
120 OSL_ENSURE( m_xMetaData.is(), "OSaveAsDlg::OSaveAsDlg: no meta data for entering table names: this will crash!" );
121 {
122 m_xLabel->set_label(DBA_RES(STR_TBL_LABEL));
123 if(m_xMetaData.is() && !m_xMetaData->supportsCatalogsInTableDefinitions()) {
124 m_xCatalogLbl->hide();
125 m_xCatalog->hide();
126 } else {
127 // now fill the catalogs
128 lcl_fillComboList( *m_xCatalog, _xConnection,
129 &XDatabaseMetaData::getCatalogs, _xConnection->getCatalog() );
130 }
131
132 if ( !m_xMetaData->supportsSchemasInTableDefinitions()) {
133 m_xSchemaLbl->hide();
134 m_xSchema->hide();
135 } else {
136 lcl_fillComboList( *m_xSchema, _xConnection,
137 &XDatabaseMetaData::getSchemas, m_xMetaData->getUserName() );
138 }
139
140 OSL_ENSURE(m_xMetaData.is(),"The metadata can not be null!");
141 if(m_aName.indexOf('.') != -1) {
142 OUString sCatalog,sSchema,sTable;
143 ::dbtools::qualifiedNameComponents(m_xMetaData,
144 m_aName,
145 sCatalog,
146 sSchema,
147 sTable,
148 ::dbtools::EComposeRule::InDataManipulation);
149
150 int nPos = m_xCatalog->find_text(sCatalog);
151 if (nPos != -1)
152 m_xCatalog->set_active(nPos);
153
154 if ( !sSchema.isEmpty() ) {
155 nPos = m_xSchema->find_text(sSchema);
156 if (nPos != -1)
157 m_xSchema->set_active(nPos);
158 }
159 m_xTitle->set_text(sTable);
160 } else
161 m_xTitle->set_text(m_aName);
162 m_xTitle->select_region(0, -1);
163
164 sal_Int32 nLength = m_xMetaData.is() ? m_xMetaData->getMaxTableNameLength() : 0;
165 if (nLength)
166 {
167 m_xTitle->set_max_length(nLength);
168 m_xSchema->set_entry_max_length(nLength);
169 m_xCatalog->set_entry_max_length(nLength);
170 }
171
172 bool bCheck = _xConnection.is() && isSQL92CheckEnabled(_xConnection);
173 m_aChecker.setCheck(bCheck); // enable non valid sql chars as well
174 }
175 break;
176
177 default:
178 OSL_FAIL( "OSaveAsDlg::OSaveAsDlg: Type not supported yet!" );
179 }
180
181 implInit();
182}
183
184OSaveAsDlg::OSaveAsDlg(weld::Window * pParent,
185 const Reference< XComponentContext >& _rxContext,
186 const OUString& rDefault,
187 const OUString& _sLabel,
188 const IObjectNameCheck& _rObjectNameCheck,
189 SADFlags _nFlags)
190 : GenericDialogController(pParent, "dbaccess/ui/savedialog.ui", "SaveDialog")
191 , m_xContext( _rxContext )
192 , m_aName(rDefault)
193 , m_rObjectNameCheck( _rObjectNameCheck )
194 , m_nType(CommandType::COMMAND)
195 , m_nFlags(_nFlags)
196 , m_aChecker(OUString())
197 , m_xDescription(m_xBuilder->weld_label("descriptionft"))
198 , m_xCatalogLbl(m_xBuilder->weld_label("catalogft"))
199 , m_xCatalog(m_xBuilder->weld_combo_box("catalog"))
200 , m_xSchemaLbl(m_xBuilder->weld_label("schemaft"))
201 , m_xSchema(m_xBuilder->weld_combo_box("schema"))
202 , m_xLabel(m_xBuilder->weld_label("titleft"))
203 , m_xTitle(m_xBuilder->weld_entry("title"))
204 , m_xPB_OK(m_xBuilder->weld_button("ok"))
205{
206 m_xTitle->connect_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
207 m_xSchema->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
208 m_xCatalog->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
209 implInitOnlyTitle(_sLabel);
210 implInit();
211}
212
213OSaveAsDlg::~OSaveAsDlg()
214{
215}
216
218{
219 m_aName = m_xTitle->get_text();
220
221 OUString sNameToCheck( m_aName );
222
223 if ( m_nType == CommandType::TABLE ) {
224 sNameToCheck = ::dbtools::composeTableName(
225 m_xMetaData,
226 getCatalog(),
227 getSchema(),
228 sNameToCheck,
229 false, // no quoting
230 ::dbtools::EComposeRule::InDataManipulation
231 );
232 }
233
234 SQLExceptionInfo aNameError;
235 if ( m_rObjectNameCheck.isNameValid( sNameToCheck, aNameError ) )
236 m_xDialog->response(RET_OK);
237
238 showError(aNameError, m_xDialog->GetXWindow(), m_xContext);
239 m_xTitle->grab_focus();
240}
241
243{
244 m_xPB_OK->set_sensitive(!m_xTitle->get_text().isEmpty());
245}
246
247void OSaveAsDlg::implInitOnlyTitle(const OUString& _rLabel)
248{
249 m_xLabel->set_label(_rLabel);
250 m_xCatalogLbl->hide();
251 m_xCatalog->hide();
252 m_xSchemaLbl->hide();
253 m_xSchema->hide();
254
255 m_xTitle->set_text(m_aName);
256 m_aChecker.setCheck(false); // enable non valid sql chars as well
257}
258
260{
262 // hide the description window
263 m_xDescription->hide();
264 }
265
267 m_xDialog->set_title( DBA_RES( STR_TITLE_PASTE_AS ) );
269 m_xDialog->set_title( DBA_RES( STR_TITLE_RENAME ) );
270
271 m_xPB_OK->connect_clicked(LINK(this,OSaveAsDlg,ButtonClickHdl));
272 m_xTitle->connect_changed(LINK(this,OSaveAsDlg,EditModifyHdl));
273 m_xTitle->grab_focus();
274}
275
276const OUString& OSaveAsDlg::getName() const
277{
278 return m_aName;
279}
281{
282 return m_xCatalog->get_visible() ? m_xCatalog->get_active_text() : OUString();
283}
284OUString OSaveAsDlg::getSchema() const
285{
286 return m_xSchema->get_visible() ? m_xSchema->get_active_text() : OUString();
287}
288
289/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sSchema
OptionalString sCatalog
Reference< XExecutableDialog > m_xDialog
interface encapsulating the check for the validity of an object name
void setCheck(bool _bCheck)
Definition: SqlNameEdit.hxx:42
std::unique_ptr< weld::Label > m_xDescription
Definition: dlgsave.hxx:60
std::unique_ptr< weld::Label > m_xLabel
Definition: dlgsave.hxx:65
std::unique_ptr< weld::Entry > m_xTitle
Definition: dlgsave.hxx:66
const OUString & getName() const
Definition: dlgsave.cxx:276
std::unique_ptr< weld::Label > m_xSchemaLbl
Definition: dlgsave.hxx:63
std::unique_ptr< weld::ComboBox > m_xSchema
Definition: dlgsave.hxx:64
OUString getSchema() const
Definition: dlgsave.cxx:284
std::unique_ptr< weld::Button > m_xPB_OK
Definition: dlgsave.hxx:67
void implInitOnlyTitle(const OUString &_rLabel)
Definition: dlgsave.cxx:247
std::unique_ptr< weld::Label > m_xCatalogLbl
Definition: dlgsave.hxx:61
SADFlags m_nFlags
Definition: dlgsave.hxx:56
OUString getCatalog() const
Definition: dlgsave.cxx:280
std::unique_ptr< weld::ComboBox > m_xCatalog
Definition: dlgsave.hxx:62
OSQLNameChecker m_aChecker
Definition: dlgsave.hxx:58
OUString m_aName
Definition: dlgsave.hxx:52
std::unique_ptr< weld::Label > m_xLabel
virtual int find_text(const OUString &rStr) const=0
virtual void set_active(int pos)=0
void append_text(const OUString &rStr)
std::shared_ptr< weld::Dialog > m_xDialog
Reference< XComponentContext > m_xContext
#define DBA_RES(id)
#define DBG_UNHANDLED_EXCEPTION(...)
IMPL_LINK(OSaveAsDlg, TextFilterHdl, OUString &, rTest, bool)
Definition: dlgsave.cxx:40
SADFlags
Definition: dlgsave.hxx:35
@ AdditionalDescription
sal_Int32 m_nFlags
sal_uInt16 nPos
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
bool isSQL92CheckEnabled(const css::uno::Reference< css::sdbc::XConnection > &_xConnection)
check if SQL92 name checking is enabled
void showError(const SQLExceptionInfo &_rInfo, const Reference< XWindow > &_xParent, const Reference< XComponentContext > &_rxContext)
OUString m_aName
RET_OK
sal_Int32 nLength