Get the style color and number just once
[LibreOffice.git] / extensions / source / abpilot / abpfinalpage.cxx
blob114533edf1b3815ab673e0bb44a00ff4c2c0030d
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
34 static std::shared_ptr<const SfxFilter> lcl_getBaseFilter()
36 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetFilterByName(u"StarOffice XML (Base)"_ustr);
37 OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
38 return pFilter;
41 FinalPage::FinalPage(weld::Container* pPage, OAddressBookSourcePilot* pWizard)
42 : AddressBookSourcePage(pPage, pWizard, u"modules/sabpilot/ui/datasourcepage.ui"_ustr,
43 u"DataSourcePage"_ustr)
44 , m_xLocation(new SvtURLBox(m_xBuilder->weld_combo_box(u"location"_ustr)))
45 , m_xBrowse(m_xBuilder->weld_button(u"browse"_ustr))
46 , m_xRegisterName(m_xBuilder->weld_check_button(u"available"_ustr))
47 , m_xEmbed(m_xBuilder->weld_check_button(u"embed"_ustr))
48 , m_xNameLabel(m_xBuilder->weld_label(u"nameft"_ustr))
49 , m_xLocationLabel(m_xBuilder->weld_label(u"locationft"_ustr))
50 , m_xName(m_xBuilder->weld_entry(u"name"_ustr))
51 , m_xDuplicateNameError(m_xBuilder->weld_label(u"warning"_ustr))
53 m_xLocation->SetSmartProtocol(INetProtocol::File);
54 m_xLocation->DisableHistory();
56 m_xLocationController.reset( new svx::DatabaseLocationInputController(pWizard->getORB(),
57 *m_xLocation, *m_xBrowse, *pWizard->getDialog()) );
59 m_xName->connect_changed( LINK(this, FinalPage, OnEntryNameModified) );
60 m_xLocation->connect_changed( LINK(this, FinalPage, OnComboNameModified) );
61 m_xRegisterName->connect_toggled( LINK( this, FinalPage, OnRegister ) );
62 m_xRegisterName->set_active(true);
63 m_xEmbed->connect_toggled( LINK( this, FinalPage, OnEmbed ) );
64 m_xEmbed->set_active(true);
67 FinalPage::~FinalPage()
69 m_xLocationController.reset();
72 bool FinalPage::isValidName() const
74 OUString sCurrentName(m_xName->get_text());
76 if (sCurrentName.isEmpty())
77 // the name must not be empty
78 return false;
80 if ( m_aInvalidDataSourceNames.find( sCurrentName ) != m_aInvalidDataSourceNames.end() )
81 // there already is a data source with this name
82 return false;
84 return true;
87 void FinalPage::setFields()
89 AddressSettings& rSettings = getSettings();
91 INetURLObject aURL( rSettings.sDataSourceName );
92 if( aURL.GetProtocol() == INetProtocol::NotValid )
94 OUString sPath = SvtPathOptions().GetWorkPath() +
95 "/" + rSettings.sDataSourceName;
97 std::shared_ptr<const SfxFilter> pFilter = lcl_getBaseFilter();
98 if ( pFilter )
100 OUString sExt = pFilter->GetDefaultExtension();
101 sPath += o3tl::getToken(sExt,1,'*');
104 aURL.SetURL(sPath);
106 OSL_ENSURE( aURL.GetProtocol() != INetProtocol::NotValid ,"No valid file name!");
107 rSettings.sDataSourceName = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
108 m_xLocationController->setURL( rSettings.sDataSourceName );
109 OUString sName = aURL.getName( );
110 sal_Int32 nPos = sName.indexOf(aURL.GetFileExtension());
111 if ( nPos != -1 )
113 sName = sName.replaceAt(nPos-1, 4, u"");
115 m_xName->set_text(sName);
117 OnRegister(*m_xRegisterName);
121 void FinalPage::initializePage()
123 AddressBookSourcePage::initializePage();
125 setFields();
128 bool FinalPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason )
130 if (!AddressBookSourcePage::commitPage(_eReason))
131 return false;
133 if ( ( ::vcl::WizardTypes::eTravelBackward != _eReason )
134 && ( !m_xLocationController->prepareCommit() )
136 return false;
138 AddressSettings& rSettings = getSettings();
139 rSettings.sDataSourceName = m_xLocationController->getURL();
140 rSettings.bRegisterDataSource = m_xRegisterName->get_active();
141 if ( rSettings.bRegisterDataSource )
142 rSettings.sRegisteredDataSourceName = m_xName->get_text();
143 rSettings.bEmbedDataSource = m_xEmbed->get_active();
145 return true;
148 void FinalPage::Activate()
150 AddressBookSourcePage::Activate();
152 // get the names of all data sources
153 ODataSourceContext aContext( getORB() );
154 aContext.getDataSourceNames( m_aInvalidDataSourceNames );
156 // give the name edit the focus
157 m_xLocation->grab_focus();
159 // default the finish button
160 getDialog()->defaultButton( WizardButtonFlags::FINISH );
162 OnEmbed(*m_xEmbed);
165 void FinalPage::Deactivate()
167 AddressBookSourcePage::Deactivate();
169 // default the "next" button, again
170 getDialog()->defaultButton( WizardButtonFlags::NEXT );
171 // disable the finish button
172 getDialog()->enableButtons( WizardButtonFlags::FINISH, false );
176 bool FinalPage::canAdvance() const
178 return false;
181 void FinalPage::implCheckName()
183 bool bValidName = isValidName();
184 bool bEmptyName = m_xName->get_text().isEmpty();
185 bool bEmptyLocation = m_xLocation->get_active_text().isEmpty();
187 // enable or disable the finish button
188 getDialog()->enableButtons( WizardButtonFlags::FINISH, !bEmptyLocation && (!m_xRegisterName->get_active() || bValidName) );
190 // show the error message for an invalid name
191 m_xDuplicateNameError->set_visible(!bValidName && !bEmptyName);
194 IMPL_LINK_NOARG( FinalPage, OnEntryNameModified, weld::Entry&, void )
196 implCheckName();
199 IMPL_LINK_NOARG( FinalPage, OnComboNameModified, weld::ComboBox&, void )
201 implCheckName();
204 IMPL_LINK_NOARG(FinalPage, OnRegister, weld::Toggleable&, void)
206 bool bEnable = m_xRegisterName->get_active();
207 m_xNameLabel->set_sensitive(bEnable);
208 m_xName->set_sensitive(bEnable);
209 implCheckName();
212 IMPL_LINK_NOARG(FinalPage, OnEmbed, weld::Toggleable&, void)
214 bool bEmbed = m_xEmbed->get_active();
215 m_xLocationLabel->set_sensitive(!bEmbed);
216 m_xLocation->set_sensitive(!bEmbed);
217 m_xBrowse->set_sensitive(!bEmbed);
220 } // namespace abp
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */