bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / abpilot / abpfinalpage.cxx
blob0be3156f43291464cdfac8bcad022cc89fd730a2
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 <tools/urlobj.hxx>
24 #include <unotools/ucbhelper.hxx>
25 #include <unotools/pathoptions.hxx>
26 #include <svl/filenotation.hxx>
27 #include <sfx2/docfilt.hxx>
28 #include <vcl/msgbox.hxx>
29 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
32 namespace abp
35 using namespace ::svt;
36 using namespace ::utl;
38 const SfxFilter* lcl_getBaseFilter()
40 const SfxFilter* pFilter = SfxFilter::GetFilterByName(OUString("StarOffice XML (Base)"));
41 OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
42 return pFilter;
45 FinalPage::FinalPage( OAddessBookSourcePilot* _pParent )
46 : AddressBookSourcePage(_pParent, "DataSourcePage",
47 "modules/sabpilot/ui/datasourcepage.ui")
49 get(m_pLocation, "location");
50 get(m_pBrowse, "browse");
51 get(m_pRegisterName, "available");
52 get(m_pNameLabel, "nameft");
53 get(m_pName, "name");
54 get(m_pDuplicateNameError, "warning");
55 m_pLocationController = new svx::DatabaseLocationInputController(_pParent->getORB(),
56 *m_pLocation, *m_pBrowse);
58 m_pName->SetModifyHdl( LINK(this, FinalPage, OnNameModified) );
59 m_pLocation->SetModifyHdl( LINK(this, FinalPage, OnNameModified) );
60 m_pRegisterName->SetClickHdl( LINK( this, FinalPage, OnRegister ) );
61 m_pRegisterName->Check(true);
64 FinalPage::~FinalPage()
66 disposeOnce();
69 void FinalPage::dispose()
71 delete m_pLocationController;
72 m_pLocation.clear();
73 m_pBrowse.clear();
74 m_pRegisterName.clear();
75 m_pNameLabel.clear();
76 m_pName.clear();
77 m_pDuplicateNameError.clear();
78 AddressBookSourcePage::dispose();
81 bool FinalPage::isValidName() const
83 OUString sCurrentName(m_pName->GetText());
85 if (sCurrentName.isEmpty())
86 // the name must not be empty
87 return false;
89 if ( m_aInvalidDataSourceNames.find( sCurrentName ) != m_aInvalidDataSourceNames.end() )
90 // there already is a data source with this name
91 return false;
93 return true;
96 void FinalPage::setFields()
98 AddressSettings& rSettings = getSettings();
100 INetURLObject aURL( rSettings.sDataSourceName );
101 if( aURL.GetProtocol() == INetProtocol::NotValid )
103 OUString sPath = SvtPathOptions().GetWorkPath();
104 sPath += "/";
105 sPath += rSettings.sDataSourceName;
107 const SfxFilter* pFilter = lcl_getBaseFilter();
108 if ( pFilter )
110 OUString sExt = pFilter->GetDefaultExtension();
111 sPath += sExt.getToken(1,'*');
114 aURL.SetURL(sPath);
116 OSL_ENSURE( aURL.GetProtocol() != INetProtocol::NotValid ,"No valid file name!");
117 rSettings.sDataSourceName = aURL.GetMainURL( INetURLObject::NO_DECODE );
118 m_pLocationController->setURL( rSettings.sDataSourceName );
119 OUString sName = aURL.getName( );
120 sal_Int32 nPos = sName.indexOf(aURL.GetExtension());
121 if ( nPos != -1 )
123 sName = sName.replaceAt(nPos-1, 4, "");
125 m_pName->SetText(sName);
127 OnRegister(m_pRegisterName);
131 void FinalPage::initializePage()
133 AddressBookSourcePage::initializePage();
135 setFields();
139 bool FinalPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
141 if (!AddressBookSourcePage::commitPage(_eReason))
142 return false;
144 if ( ( ::svt::WizardTypes::eTravelBackward != _eReason )
145 && ( !m_pLocationController->prepareCommit() )
147 return false;
149 AddressSettings& rSettings = getSettings();
150 rSettings.sDataSourceName = m_pLocationController->getURL();
151 rSettings.bRegisterDataSource = m_pRegisterName->IsChecked();
152 if ( rSettings.bRegisterDataSource )
153 rSettings.sRegisteredDataSourceName = m_pName->GetText();
155 return true;
159 void FinalPage::ActivatePage()
161 AddressBookSourcePage::ActivatePage();
163 // get the names of all data sources
164 ODataSourceContext aContext( getORB() );
165 aContext.getDataSourceNames( m_aInvalidDataSourceNames );
167 // give the name edit the focus
168 m_pLocation->GrabFocus();
170 // default the finish button
171 getDialog()->defaultButton( WizardButtonFlags::FINISH );
175 void FinalPage::DeactivatePage()
177 AddressBookSourcePage::DeactivatePage();
179 // default the "next" button, again
180 getDialog()->defaultButton( WizardButtonFlags::NEXT );
181 // disable the finish button
182 getDialog()->enableButtons( WizardButtonFlags::FINISH, false );
186 bool FinalPage::canAdvance() const
188 return false;
192 void FinalPage::implCheckName()
194 bool bValidName = isValidName();
195 bool bEmptyName = m_pName->GetText().isEmpty();
196 bool bEmptyLocation = m_pLocation->GetText().isEmpty();
198 // enable or disable the finish button
199 getDialog()->enableButtons( WizardButtonFlags::FINISH, !bEmptyLocation && (!m_pRegisterName->IsChecked() || bValidName) );
201 // show the error message for an invalid name
202 m_pDuplicateNameError->Show( !bValidName && !bEmptyName );
206 IMPL_LINK( FinalPage, OnNameModified, Edit*, /**/ )
208 implCheckName();
209 return 0L;
213 IMPL_LINK_NOARG(FinalPage, OnRegister)
215 bool bEnable = m_pRegisterName->IsChecked();
216 m_pNameLabel->Enable(bEnable);
217 m_pName->Enable(bEnable);
218 implCheckName();
219 return 0L;
222 } // namespace abp
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */