Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / abpilot / abpfinalpage.cxx
blob78756b28b5eb9dadbf94366c1e4a8326b10de45f
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 "abpfinalpage.hxx"
21 #include "addresssettings.hxx"
22 #include "abspilot.hxx"
23 #include <osl/diagnose.h>
24 #include <tools/urlobj.hxx>
25 #include <svtools/inettbc.hxx>
26 #include <unotools/ucbhelper.hxx>
27 #include <unotools/pathoptions.hxx>
28 #include <svl/filenotation.hxx>
29 #include <sfx2/docfilt.hxx>
30 #include <o3tl/string_view.hxx>
32 namespace abp
35 using namespace ::svt;
36 using namespace ::utl;
38 static std::shared_ptr<const SfxFilter> lcl_getBaseFilter()
40 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetFilterByName("StarOffice XML (Base)");
41 OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
42 return pFilter;
45 FinalPage::FinalPage(weld::Container* pPage, OAddressBookSourcePilot* pWizard)
46 : AddressBookSourcePage(pPage, pWizard, "modules/sabpilot/ui/datasourcepage.ui",
47 "DataSourcePage")
48 , m_xLocation(new SvtURLBox(m_xBuilder->weld_combo_box("location")))
49 , m_xBrowse(m_xBuilder->weld_button("browse"))
50 , m_xRegisterName(m_xBuilder->weld_check_button("available"))
51 , m_xEmbed(m_xBuilder->weld_check_button("embed"))
52 , m_xNameLabel(m_xBuilder->weld_label("nameft"))
53 , m_xLocationLabel(m_xBuilder->weld_label("locationft"))
54 , m_xName(m_xBuilder->weld_entry("name"))
55 , m_xDuplicateNameError(m_xBuilder->weld_label("warning"))
57 m_xLocation->SetSmartProtocol(INetProtocol::File);
58 m_xLocation->DisableHistory();
60 m_xLocationController.reset( new svx::DatabaseLocationInputController(pWizard->getORB(),
61 *m_xLocation, *m_xBrowse, *pWizard->getDialog()) );
63 m_xName->connect_changed( LINK(this, FinalPage, OnEntryNameModified) );
64 m_xLocation->connect_changed( LINK(this, FinalPage, OnComboNameModified) );
65 m_xRegisterName->connect_toggled( LINK( this, FinalPage, OnRegister ) );
66 m_xRegisterName->set_active(true);
67 m_xEmbed->connect_toggled( LINK( this, FinalPage, OnEmbed ) );
68 m_xEmbed->set_active(true);
71 FinalPage::~FinalPage()
73 m_xLocationController.reset();
76 bool FinalPage::isValidName() const
78 OUString sCurrentName(m_xName->get_text());
80 if (sCurrentName.isEmpty())
81 // the name must not be empty
82 return false;
84 if ( m_aInvalidDataSourceNames.find( sCurrentName ) != m_aInvalidDataSourceNames.end() )
85 // there already is a data source with this name
86 return false;
88 return true;
91 void FinalPage::setFields()
93 AddressSettings& rSettings = getSettings();
95 INetURLObject aURL( rSettings.sDataSourceName );
96 if( aURL.GetProtocol() == INetProtocol::NotValid )
98 OUString sPath = SvtPathOptions().GetWorkPath() +
99 "/" + rSettings.sDataSourceName;
101 std::shared_ptr<const SfxFilter> pFilter = lcl_getBaseFilter();
102 if ( pFilter )
104 OUString sExt = pFilter->GetDefaultExtension();
105 sPath += o3tl::getToken(sExt,1,'*');
108 aURL.SetURL(sPath);
110 OSL_ENSURE( aURL.GetProtocol() != INetProtocol::NotValid ,"No valid file name!");
111 rSettings.sDataSourceName = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
112 m_xLocationController->setURL( rSettings.sDataSourceName );
113 OUString sName = aURL.getName( );
114 sal_Int32 nPos = sName.indexOf(aURL.GetFileExtension());
115 if ( nPos != -1 )
117 sName = sName.replaceAt(nPos-1, 4, u"");
119 m_xName->set_text(sName);
121 OnRegister(*m_xRegisterName);
125 void FinalPage::initializePage()
127 AddressBookSourcePage::initializePage();
129 setFields();
132 bool FinalPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason )
134 if (!AddressBookSourcePage::commitPage(_eReason))
135 return false;
137 if ( ( ::vcl::WizardTypes::eTravelBackward != _eReason )
138 && ( !m_xLocationController->prepareCommit() )
140 return false;
142 AddressSettings& rSettings = getSettings();
143 rSettings.sDataSourceName = m_xLocationController->getURL();
144 rSettings.bRegisterDataSource = m_xRegisterName->get_active();
145 if ( rSettings.bRegisterDataSource )
146 rSettings.sRegisteredDataSourceName = m_xName->get_text();
147 rSettings.bEmbedDataSource = m_xEmbed->get_active();
149 return true;
152 void FinalPage::Activate()
154 AddressBookSourcePage::Activate();
156 // get the names of all data sources
157 ODataSourceContext aContext( getORB() );
158 aContext.getDataSourceNames( m_aInvalidDataSourceNames );
160 // give the name edit the focus
161 m_xLocation->grab_focus();
163 // default the finish button
164 getDialog()->defaultButton( WizardButtonFlags::FINISH );
166 OnEmbed(*m_xEmbed);
169 void FinalPage::Deactivate()
171 AddressBookSourcePage::Deactivate();
173 // default the "next" button, again
174 getDialog()->defaultButton( WizardButtonFlags::NEXT );
175 // disable the finish button
176 getDialog()->enableButtons( WizardButtonFlags::FINISH, false );
180 bool FinalPage::canAdvance() const
182 return false;
185 void FinalPage::implCheckName()
187 bool bValidName = isValidName();
188 bool bEmptyName = m_xName->get_text().isEmpty();
189 bool bEmptyLocation = m_xLocation->get_active_text().isEmpty();
191 // enable or disable the finish button
192 getDialog()->enableButtons( WizardButtonFlags::FINISH, !bEmptyLocation && (!m_xRegisterName->get_active() || bValidName) );
194 // show the error message for an invalid name
195 m_xDuplicateNameError->set_visible(!bValidName && !bEmptyName);
198 IMPL_LINK_NOARG( FinalPage, OnEntryNameModified, weld::Entry&, void )
200 implCheckName();
203 IMPL_LINK_NOARG( FinalPage, OnComboNameModified, weld::ComboBox&, void )
205 implCheckName();
208 IMPL_LINK_NOARG(FinalPage, OnRegister, weld::Toggleable&, void)
210 bool bEnable = m_xRegisterName->get_active();
211 m_xNameLabel->set_sensitive(bEnable);
212 m_xName->set_sensitive(bEnable);
213 implCheckName();
216 IMPL_LINK_NOARG(FinalPage, OnEmbed, weld::Toggleable&, void)
218 bool bEmbed = m_xEmbed->get_active();
219 m_xLocationLabel->set_sensitive(!bEmbed);
220 m_xLocation->set_sensitive(!bEmbed);
221 m_xBrowse->set_sensitive(!bEmbed);
224 } // namespace abp
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */