tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / dbaccess / source / ui / dlg / dlgsave.cxx
blob0bb460428d46f84bbcaf9c6f2de3e24d93572424
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
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>
25 #include <connectivity/dbtools.hxx>
26 #include <UITools.hxx>
27 #include <objectnamecheck.hxx>
28 #include <utility>
29 #include <comphelper/diagnose_ex.hxx>
31 using namespace dbaui;
32 using namespace dbtools;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::sdb;
35 using namespace ::com::sun::star::sdbc;
38 IMPL_LINK(OSaveAsDlg, TextFilterHdl, OUString&, rTest, bool)
40 OUString sCorrected;
41 if (m_aChecker.checkString(rTest, sCorrected))
42 rTest = sCorrected;
43 return true;
46 namespace
48 typedef Reference< XResultSet > (SAL_CALL XDatabaseMetaData::*FGetMetaStrings)();
50 void lcl_fillComboList( weld::ComboBox& _rList, const Reference< XConnection >& _rxConnection,
51 FGetMetaStrings GetAll, const OUString& _rCurrent )
53 try {
54 Reference< XDatabaseMetaData > xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW );
56 Reference< XResultSet > xRes = (xMetaData.get()->*GetAll)();
57 Reference< XRow > xRow( xRes, UNO_QUERY_THROW );
58 OUString sValue;
59 while ( xRes->next() ) {
60 sValue = xRow->getString( 1 );
61 if ( !xRow->wasNull() )
62 _rList.append_text( sValue );
65 int nPos = _rList.find_text( _rCurrent );
66 if (nPos != -1)
67 _rList.set_active( nPos );
68 else
69 _rList.set_active( 0 );
70 } catch( const Exception& ) {
71 DBG_UNHANDLED_EXCEPTION("dbaccess");
76 OSaveAsDlg::OSaveAsDlg( weld::Window * pParent,
77 sal_Int32 _rType,
78 const Reference< XComponentContext >& _rxContext,
79 const Reference< XConnection>& _xConnection,
80 const OUString& rDefault,
81 const IObjectNameCheck& _rObjectNameCheck,
82 SADFlags _nFlags)
83 : GenericDialogController(pParent, u"dbaccess/ui/savedialog.ui"_ustr, u"SaveDialog"_ustr)
84 , m_xContext( _rxContext )
85 , m_aName(rDefault)
86 , m_rObjectNameCheck( _rObjectNameCheck )
87 , m_nType(_rType)
88 , m_nFlags(_nFlags)
89 , m_aChecker(OUString())
90 , m_xDescription(m_xBuilder->weld_label(u"descriptionft"_ustr))
91 , m_xCatalogLbl(m_xBuilder->weld_label(u"catalogft"_ustr))
92 , m_xCatalog(m_xBuilder->weld_combo_box(u"catalog"_ustr))
93 , m_xSchemaLbl(m_xBuilder->weld_label(u"schemaft"_ustr))
94 , m_xSchema(m_xBuilder->weld_combo_box(u"schema"_ustr))
95 , m_xLabel(m_xBuilder->weld_label(u"titleft"_ustr))
96 , m_xTitle(m_xBuilder->weld_entry(u"title"_ustr))
97 , m_xPB_OK(m_xBuilder->weld_button(u"ok"_ustr))
99 if ( _xConnection.is() )
100 m_xMetaData = _xConnection->getMetaData();
102 if (m_xMetaData.is())
104 OUString sExtraNameChars(m_xMetaData->getExtraNameCharacters());
105 m_aChecker.setAllowedChars(sExtraNameChars);
108 m_xTitle->connect_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
109 m_xSchema->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
110 m_xCatalog->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
112 switch (_rType) {
113 case CommandType::QUERY:
114 implInitOnlyTitle(DBA_RES(STR_QRY_LABEL));
115 break;
117 case CommandType::TABLE:
118 OSL_ENSURE( m_xMetaData.is(), "OSaveAsDlg::OSaveAsDlg: no meta data for entering table names: this will crash!" );
120 m_xLabel->set_label(DBA_RES(STR_TBL_LABEL));
121 if(m_xMetaData.is() && !m_xMetaData->supportsCatalogsInTableDefinitions()) {
122 m_xCatalogLbl->hide();
123 m_xCatalog->hide();
124 } else {
125 // now fill the catalogs
126 lcl_fillComboList( *m_xCatalog, _xConnection,
127 &XDatabaseMetaData::getCatalogs, _xConnection->getCatalog() );
130 if ( !m_xMetaData->supportsSchemasInTableDefinitions()) {
131 m_xSchemaLbl->hide();
132 m_xSchema->hide();
133 } else {
134 lcl_fillComboList( *m_xSchema, _xConnection,
135 &XDatabaseMetaData::getSchemas, m_xMetaData->getUserName() );
138 OSL_ENSURE(m_xMetaData.is(),"The metadata can not be null!");
139 if(m_aName.indexOf('.') != -1) {
140 OUString sCatalog,sSchema,sTable;
141 ::dbtools::qualifiedNameComponents(m_xMetaData,
142 m_aName,
143 sCatalog,
144 sSchema,
145 sTable,
146 ::dbtools::EComposeRule::InDataManipulation);
148 int nPos = m_xCatalog->find_text(sCatalog);
149 if (nPos != -1)
150 m_xCatalog->set_active(nPos);
152 if ( !sSchema.isEmpty() ) {
153 nPos = m_xSchema->find_text(sSchema);
154 if (nPos != -1)
155 m_xSchema->set_active(nPos);
157 m_xTitle->set_text(sTable);
158 } else
159 m_xTitle->set_text(m_aName);
160 m_xTitle->select_region(0, -1);
162 sal_Int32 nLength = m_xMetaData.is() ? m_xMetaData->getMaxTableNameLength() : 0;
163 if (nLength)
165 m_xTitle->set_max_length(nLength);
166 m_xSchema->set_entry_max_length(nLength);
167 m_xCatalog->set_entry_max_length(nLength);
170 bool bCheck = _xConnection.is() && isSQL92CheckEnabled(_xConnection);
171 m_aChecker.setCheck(bCheck); // enable non valid sql chars as well
173 break;
175 default:
176 OSL_FAIL( "OSaveAsDlg::OSaveAsDlg: Type not supported yet!" );
179 implInit();
182 OSaveAsDlg::OSaveAsDlg(weld::Window * pParent,
183 const Reference< XComponentContext >& _rxContext,
184 const OUString& rDefault,
185 const OUString& _sLabel,
186 const IObjectNameCheck& _rObjectNameCheck,
187 SADFlags _nFlags)
188 : GenericDialogController(pParent, u"dbaccess/ui/savedialog.ui"_ustr, u"SaveDialog"_ustr)
189 , m_xContext( _rxContext )
190 , m_aName(rDefault)
191 , m_rObjectNameCheck( _rObjectNameCheck )
192 , m_nType(CommandType::COMMAND)
193 , m_nFlags(_nFlags)
194 , m_aChecker(OUString())
195 , m_xDescription(m_xBuilder->weld_label(u"descriptionft"_ustr))
196 , m_xCatalogLbl(m_xBuilder->weld_label(u"catalogft"_ustr))
197 , m_xCatalog(m_xBuilder->weld_combo_box(u"catalog"_ustr))
198 , m_xSchemaLbl(m_xBuilder->weld_label(u"schemaft"_ustr))
199 , m_xSchema(m_xBuilder->weld_combo_box(u"schema"_ustr))
200 , m_xLabel(m_xBuilder->weld_label(u"titleft"_ustr))
201 , m_xTitle(m_xBuilder->weld_entry(u"title"_ustr))
202 , m_xPB_OK(m_xBuilder->weld_button(u"ok"_ustr))
204 m_xTitle->connect_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
205 m_xSchema->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
206 m_xCatalog->connect_entry_insert_text(LINK(this, OSaveAsDlg, TextFilterHdl));
207 implInitOnlyTitle(_sLabel);
208 implInit();
211 OSaveAsDlg::~OSaveAsDlg()
215 IMPL_LINK_NOARG(OSaveAsDlg, ButtonClickHdl, weld::Button&, void)
217 m_aName = m_xTitle->get_text();
219 OUString sNameToCheck( m_aName );
221 if ( m_nType == CommandType::TABLE ) {
222 sNameToCheck = ::dbtools::composeTableName(
223 m_xMetaData,
224 getCatalog(),
225 getSchema(),
226 sNameToCheck,
227 false, // no quoting
228 ::dbtools::EComposeRule::InDataManipulation
232 SQLExceptionInfo aNameError;
233 if ( m_rObjectNameCheck.isNameValid( sNameToCheck, aNameError ) )
234 m_xDialog->response(RET_OK);
236 showError(aNameError, m_xDialog->GetXWindow(), m_xContext);
237 m_xTitle->grab_focus();
240 IMPL_LINK_NOARG(OSaveAsDlg, EditModifyHdl, weld::Entry&, void)
242 m_xPB_OK->set_sensitive(!m_xTitle->get_text().isEmpty());
245 void OSaveAsDlg::implInitOnlyTitle(const OUString& _rLabel)
247 m_xLabel->set_label(_rLabel);
248 m_xCatalogLbl->hide();
249 m_xCatalog->hide();
250 m_xSchemaLbl->hide();
251 m_xSchema->hide();
253 m_xTitle->set_text(m_aName);
254 m_aChecker.setCheck(false); // enable non valid sql chars as well
257 void OSaveAsDlg::implInit()
259 if ( !( m_nFlags & SADFlags::AdditionalDescription ) ) {
260 // hide the description window
261 m_xDescription->hide();
264 if ( SADFlags::TitlePasteAs == ( m_nFlags & SADFlags::TitlePasteAs ) )
265 m_xDialog->set_title( DBA_RES( STR_TITLE_PASTE_AS ) );
266 else if ( SADFlags::TitleRename == ( m_nFlags & SADFlags::TitleRename ) )
267 m_xDialog->set_title( DBA_RES( STR_TITLE_RENAME ) );
269 m_xPB_OK->connect_clicked(LINK(this,OSaveAsDlg,ButtonClickHdl));
270 m_xTitle->connect_changed(LINK(this,OSaveAsDlg,EditModifyHdl));
271 m_xTitle->grab_focus();
274 const OUString& OSaveAsDlg::getName() const
276 return m_aName;
278 OUString OSaveAsDlg::getCatalog() const
280 return m_xCatalog->get_visible() ? m_xCatalog->get_active_text() : OUString();
282 OUString OSaveAsDlg::getSchema() const
284 return m_xSchema->get_visible() ? m_xSchema->get_active_text() : OUString();
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */