tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / dbaccess / source / ui / misc / WCPage.cxx
blob123db5efc54817bd80249237c04a7fea62a9e1b7
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 <WCPage.hxx>
21 #include <WCopyTable.hxx>
23 #include <defaultobjectnamecheck.hxx>
24 #include <strings.hrc>
25 #include <core_resource.hxx>
26 #include <com/sun/star/sdb/CommandType.hpp>
27 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
28 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
29 #include <connectivity/dbexception.hxx>
30 #include <connectivity/dbtools.hxx>
32 using namespace ::dbaui;
33 using namespace ::dbtools;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::container;
37 using namespace ::com::sun::star::sdb;
38 using namespace ::com::sun::star::sdbc;
39 using namespace ::com::sun::star::sdbcx;
41 namespace CopyTableOperation = css::sdb::application::CopyTableOperation;
43 OCopyTable::OCopyTable(weld::Container* pPage, OCopyTableWizard* pWizard)
44 : OWizardPage(pPage, pWizard, u"dbaccess/ui/copytablepage.ui"_ustr, u"CopyTablePage"_ustr)
45 , m_bPKeyAllowed(false)
46 , m_bUseHeaderAllowed(true)
47 , m_nOldOperation(0)
48 , m_xEdTableName(m_xBuilder->weld_entry(u"name"_ustr))
49 , m_xRB_DefData(m_xBuilder->weld_radio_button(u"defdata"_ustr))
50 , m_xRB_Def(m_xBuilder->weld_radio_button(u"def"_ustr))
51 , m_xRB_View(m_xBuilder->weld_radio_button(u"view"_ustr))
52 , m_xRB_AppendData(m_xBuilder->weld_radio_button(u"data"_ustr))
53 , m_xCB_UseHeaderLine(m_xBuilder->weld_check_button(u"firstline"_ustr))
54 , m_xCB_PrimaryColumn(m_xBuilder->weld_check_button(u"primarykey"_ustr))
55 , m_xFT_KeyName(m_xBuilder->weld_label(u"keynamelabel"_ustr))
56 , m_xEdKeyName(m_xBuilder->weld_entry(u"keyname"_ustr))
58 if ( m_pParent->m_xDestConnection.is() )
60 if (!m_pParent->supportsViews())
61 m_xRB_View->set_sensitive(false);
63 m_xCB_UseHeaderLine->set_active(true);
64 m_bPKeyAllowed = m_pParent->supportsPrimaryKey();
66 m_xCB_PrimaryColumn->set_sensitive(m_bPKeyAllowed);
68 m_xRB_AppendData->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
69 m_xRB_DefData->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
70 m_xRB_Def->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
71 m_xRB_View->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
73 m_xCB_PrimaryColumn->connect_toggled(LINK( this, OCopyTable, KeyClickHdl ) );
75 m_xFT_KeyName->set_sensitive(false);
76 m_xEdKeyName->set_sensitive(false);
77 m_xEdKeyName->set_text(m_pParent->createUniqueName(u"ID"_ustr));
79 const sal_Int32 nMaxLen = m_pParent->getMaxColumnNameLength();
80 m_xEdKeyName->set_max_length(nMaxLen);
83 SetPageTitle(DBA_RES(STR_COPYTABLE_TITLE_COPY));
86 OCopyTable::~OCopyTable()
90 void OCopyTable::SetAppendDataRadio()
92 m_pParent->EnableNextButton(true);
93 m_xFT_KeyName->set_sensitive(false);
94 m_xCB_PrimaryColumn->set_sensitive(false);
95 m_xEdKeyName->set_sensitive(false);
96 m_pParent->setOperation(CopyTableOperation::AppendData);
99 IMPL_LINK(OCopyTable, RadioChangeHdl, weld::Toggleable&, rButton, void)
101 if (!rButton.get_active())
102 return;
103 if (m_xRB_AppendData->get_active())
105 SetAppendDataRadio();
106 return;
108 m_pParent->EnableNextButton(!m_xRB_View->get_active());
109 bool bKey = m_bPKeyAllowed && !m_xRB_View->get_active();
110 m_xFT_KeyName->set_sensitive(bKey && m_xCB_PrimaryColumn->get_active());
111 m_xEdKeyName->set_sensitive(bKey && m_xCB_PrimaryColumn->get_active());
112 m_xCB_PrimaryColumn->set_sensitive(bKey);
113 m_xCB_UseHeaderLine->set_sensitive(m_bUseHeaderAllowed && IsOptionDefData());
115 // set type what to do
116 if( IsOptionDefData() )
117 m_pParent->setOperation( CopyTableOperation::CopyDefinitionAndData );
118 else if( IsOptionDef() )
119 m_pParent->setOperation( CopyTableOperation::CopyDefinitionOnly );
120 else if( IsOptionView() )
121 m_pParent->setOperation( CopyTableOperation::CreateAsView );
124 IMPL_LINK_NOARG( OCopyTable, KeyClickHdl, weld::Toggleable&, void )
126 m_xEdKeyName->set_sensitive(m_xCB_PrimaryColumn->get_active());
127 m_xFT_KeyName->set_sensitive(m_xCB_PrimaryColumn->get_active());
130 bool OCopyTable::LeavePage()
132 m_pParent->m_bCreatePrimaryKeyColumn = m_bPKeyAllowed && m_xCB_PrimaryColumn->get_sensitive() && m_xCB_PrimaryColumn->get_active();
133 m_pParent->m_aKeyName = m_pParent->m_bCreatePrimaryKeyColumn ? m_xEdKeyName->get_text() : OUString();
134 m_pParent->setUseHeaderLine( m_xCB_UseHeaderLine->get_active() );
136 // first check if the table already exists in the database
137 if( m_pParent->getOperation() != CopyTableOperation::AppendData )
139 m_pParent->clearDestColumns();
140 DynamicTableOrQueryNameCheck aNameCheck( m_pParent->m_xDestConnection, CommandType::TABLE );
141 SQLExceptionInfo aErrorInfo;
142 if ( !aNameCheck.isNameValid( m_xEdTableName->get_text(), aErrorInfo ) )
144 aErrorInfo.append( SQLExceptionInfo::TYPE::SQLContext, DBA_RES( STR_SUGGEST_APPEND_TABLE_DATA ) );
145 m_pParent->showError(aErrorInfo.get());
147 return false;
150 // have to check the length of the table name
151 Reference< XDatabaseMetaData > xMeta = m_pParent->m_xDestConnection->getMetaData();
152 OUString sCatalog;
153 OUString sSchema;
154 OUString sTable;
155 ::dbtools::qualifiedNameComponents( xMeta,
156 m_xEdTableName->get_text(),
157 sCatalog,
158 sSchema,
159 sTable,
160 ::dbtools::EComposeRule::InDataManipulation);
161 sal_Int32 nMaxLength = xMeta->getMaxTableNameLength();
162 if ( nMaxLength && sTable.getLength() > nMaxLength )
164 m_pParent->showError(DBA_RES(STR_INVALID_TABLE_NAME_LENGTH));
165 return false;
168 // now we have to check if the name of the primary key already exists
169 if ( m_pParent->m_bCreatePrimaryKeyColumn
170 && m_pParent->m_aKeyName != m_pParent->createUniqueName(m_pParent->m_aKeyName) )
172 m_pParent->showError(DBA_RES(STR_WIZ_NAME_ALREADY_DEFINED) + " " + m_pParent->m_aKeyName);
173 return false;
177 if (m_xEdTableName->get_value_changed_from_saved())
178 { // table exists and name has changed
179 if ( m_pParent->getOperation() == CopyTableOperation::AppendData )
181 if(!checkAppendData())
182 return false;
184 else if ( m_nOldOperation == CopyTableOperation::AppendData )
186 m_xEdTableName->save_value();
187 return LeavePage();
190 else
191 { // table exist and is not new or doesn't exist and so on
192 if ( CopyTableOperation::AppendData == m_pParent->getOperation() )
194 if( !checkAppendData() )
195 return false;
198 m_pParent->m_sName = m_xEdTableName->get_text();
199 m_xEdTableName->save_value();
201 if(m_pParent->m_sName.isEmpty())
203 m_pParent->showError(DBA_RES(STR_INVALID_TABLE_NAME));
204 return false;
207 return true;
210 void OCopyTable::Activate()
212 m_pParent->GetOKButton().set_sensitive(true);
213 m_nOldOperation = m_pParent->getOperation();
214 m_xEdTableName->grab_focus();
215 m_xCB_UseHeaderLine->set_active(m_pParent->UseHeaderLine());
218 OUString OCopyTable::GetTitle() const
220 return DBA_RES(STR_WIZ_TABLE_COPY);
223 void OCopyTable::Reset()
225 m_bFirstTime = false;
227 m_xEdTableName->set_text( m_pParent->m_sName );
228 m_xEdTableName->save_value();
231 bool OCopyTable::checkAppendData()
233 m_pParent->clearDestColumns();
234 Reference< XPropertySet > xTable;
235 Reference< XTablesSupplier > xSup( m_pParent->m_xDestConnection, UNO_QUERY );
236 Reference<XNameAccess> xTables;
237 if (xSup.is())
238 xTables = xSup->getTables();
239 if (xTables.is() && xTables->hasByName(m_xEdTableName->get_text()))
241 const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
242 const sal_uInt32 nSrcSize = rSrcColumns.size();
243 m_pParent->m_vColumnPositions.resize( nSrcSize, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
244 m_pParent->m_vColumnTypes.resize( nSrcSize , COLUMN_POSITION_NOT_FOUND );
246 // set new destination
247 xTables->getByName( m_xEdTableName->get_text() ) >>= xTable;
248 ObjectCopySource aTableCopySource( m_pParent->m_xDestConnection, xTable );
249 m_pParent->loadData( aTableCopySource, m_pParent->m_vDestColumns, m_pParent->m_aDestVec );
250 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
251 const sal_uInt32 nMinSrcDestSize = std::min<sal_uInt32>(nSrcSize, rDestColumns.size());
252 sal_uInt32 i = 0;
253 for (auto const& column : rDestColumns)
255 if (i >= nMinSrcDestSize)
256 break;
257 bool bNotConvert = true;
258 m_pParent->m_vColumnPositions[i] = ODatabaseExport::TPositions::value_type(i+1,i+1);
259 TOTypeInfoSP pTypeInfo = m_pParent->convertType(column->second->getSpecialTypeInfo(),bNotConvert);
260 if ( !bNotConvert )
262 m_pParent->showColumnTypeNotSupported(column->first);
263 return false;
266 if ( pTypeInfo )
267 m_pParent->m_vColumnTypes[i] = pTypeInfo->nType;
268 else
269 m_pParent->m_vColumnTypes[i] = DataType::VARCHAR;
270 ++i;
275 if ( !xTable.is() )
277 m_pParent->showError(DBA_RES(STR_INVALID_TABLE_NAME));
278 return false;
280 return true;
283 void OCopyTable::setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName )
285 bool bCreatePK = m_bPKeyAllowed && _bDoCreate;
286 m_xCB_PrimaryColumn->set_active( bCreatePK );
287 m_xEdKeyName->set_text( _rSuggestedName );
289 m_xFT_KeyName->set_sensitive( bCreatePK );
290 m_xEdKeyName->set_sensitive( bCreatePK );
293 void OCopyTable::setCreateStyleAction()
295 // reselect the last action before
296 switch (m_pParent->getOperation())
298 case CopyTableOperation::CopyDefinitionAndData:
299 m_xRB_DefData->set_active(true);
300 RadioChangeHdl(*m_xRB_DefData);
301 break;
302 case CopyTableOperation::CopyDefinitionOnly:
303 m_xRB_Def->set_active(true);
304 RadioChangeHdl(*m_xRB_Def);
305 break;
306 case CopyTableOperation::AppendData:
307 m_xRB_AppendData->set_active(true);
308 SetAppendDataRadio();
309 break;
310 case CopyTableOperation::CreateAsView:
311 if (m_xRB_View->get_sensitive())
313 m_xRB_View->set_active(true);
314 RadioChangeHdl(*m_xRB_View);
316 else
318 m_xRB_DefData->set_active(true);
319 RadioChangeHdl(*m_xRB_DefData);
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */