1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir #include "abpfinalpage.hxx"
27cdf0e10cSrcweir #include "addresssettings.hxx"
28cdf0e10cSrcweir #include "abspilot.hxx"
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir #include <tools/urlobj.hxx>
31cdf0e10cSrcweir #include <unotools/ucbhelper.hxx>
32cdf0e10cSrcweir #include <sfx2/filedlghelper.hxx>
33cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
34cdf0e10cSrcweir #ifndef SVTOOLS_FILENOTATION_HXX_
35cdf0e10cSrcweir #include <svl/filenotation.hxx>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
38cdf0e10cSrcweir #include <vcl/msgbox.hxx>
39cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
40cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //.........................................................................
43cdf0e10cSrcweir namespace abp
44cdf0e10cSrcweir {
45cdf0e10cSrcweir //.........................................................................
46cdf0e10cSrcweir 	using namespace ::svt;
47cdf0e10cSrcweir 	using namespace ::utl;
48cdf0e10cSrcweir 
lcl_getBaseFilter()49cdf0e10cSrcweir 	const SfxFilter* lcl_getBaseFilter()
50cdf0e10cSrcweir 	{
51cdf0e10cSrcweir 		static const String s_sDatabaseType = String::CreateFromAscii("StarOffice XML (Base)");
52cdf0e10cSrcweir 		const SfxFilter* pFilter = SfxFilter::GetFilterByName( s_sDatabaseType);
53cdf0e10cSrcweir 		OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
54cdf0e10cSrcweir 		return pFilter;
55cdf0e10cSrcweir 	}
56cdf0e10cSrcweir 	//=====================================================================
57cdf0e10cSrcweir 	//= FinalPage
58cdf0e10cSrcweir 	//=====================================================================
59cdf0e10cSrcweir 	//---------------------------------------------------------------------
FinalPage(OAddessBookSourcePilot * _pParent)60cdf0e10cSrcweir 	FinalPage::FinalPage( OAddessBookSourcePilot* _pParent )
61cdf0e10cSrcweir 		:AddressBookSourcePage(_pParent, ModuleRes(RID_PAGE_FINAL))
62cdf0e10cSrcweir 		,m_aExplanation			( this, ModuleRes( FT_FINISH_EXPL ) )
63cdf0e10cSrcweir 		,m_aLocationLabel		( this, ModuleRes( FT_LOCATION ) )
64cdf0e10cSrcweir 		,m_aLocation			( this, ModuleRes( CBB_LOCATION ) )
65cdf0e10cSrcweir 		,m_aBrowse				( this, ModuleRes( PB_BROWSE ) )
66cdf0e10cSrcweir 		,m_aRegisterName		( this, ModuleRes( CB_REGISTER_DS ) )
67cdf0e10cSrcweir 		,m_aNameLabel			( this, ModuleRes( FT_NAME_EXPL ) )
68cdf0e10cSrcweir 		,m_aName				( this, ModuleRes( ET_DATASOURCENAME ) )
69cdf0e10cSrcweir 		,m_aDuplicateNameError	( this, ModuleRes( FT_DUPLICATENAME ) )
70cdf0e10cSrcweir         ,m_aLocationController( ::comphelper::ComponentContext( _pParent->getORB() ), m_aLocation, m_aBrowse )
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir 		FreeResource();
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 		m_aName.SetModifyHdl( LINK(this, FinalPage, OnNameModified) );
75cdf0e10cSrcweir 		m_aLocation.SetModifyHdl( LINK(this, FinalPage, OnNameModified) );
76cdf0e10cSrcweir 		m_aRegisterName.SetClickHdl( LINK( this, FinalPage, OnRegister ) );
77cdf0e10cSrcweir 		m_aRegisterName.Check(sal_True);
78cdf0e10cSrcweir 	}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	//---------------------------------------------------------------------
isValidName() const81cdf0e10cSrcweir 	sal_Bool FinalPage::isValidName() const
82cdf0e10cSrcweir 	{
83cdf0e10cSrcweir 		::rtl::OUString sCurrentName(m_aName.GetText());
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 		if (0 == sCurrentName.getLength())
86cdf0e10cSrcweir 			// the name must not be empty
87cdf0e10cSrcweir 			return sal_False;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 		if ( m_aInvalidDataSourceNames.find( sCurrentName ) != m_aInvalidDataSourceNames.end() )
90cdf0e10cSrcweir 			// there already is a data source with this name
91cdf0e10cSrcweir 			return sal_False;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 		return sal_True;
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	//---------------------------------------------------------------------
setFields()97cdf0e10cSrcweir 	void FinalPage::setFields()
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir 		AddressSettings& rSettings = getSettings();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 		INetURLObject aURL( rSettings.sDataSourceName );
102cdf0e10cSrcweir 		if( aURL.GetProtocol() == INET_PROT_NOT_VALID )
103cdf0e10cSrcweir 		{
104cdf0e10cSrcweir 			String sPath = SvtPathOptions().GetWorkPath();
105cdf0e10cSrcweir 			sPath += '/';
106cdf0e10cSrcweir 			sPath += String(rSettings.sDataSourceName);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 			const SfxFilter* pFilter = lcl_getBaseFilter();
109cdf0e10cSrcweir 			if ( pFilter )
110cdf0e10cSrcweir 			{
111cdf0e10cSrcweir 				String sExt = pFilter->GetDefaultExtension();
112cdf0e10cSrcweir 				sPath += sExt.GetToken(1,'*');
113cdf0e10cSrcweir 			}
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 			aURL.SetURL(sPath);
116cdf0e10cSrcweir 		}
117cdf0e10cSrcweir 		OSL_ENSURE( aURL.GetProtocol() != INET_PROT_NOT_VALID ,"No valid file name!");
118cdf0e10cSrcweir 		rSettings.sDataSourceName = aURL.GetMainURL( INetURLObject::NO_DECODE );
119cdf0e10cSrcweir         m_aLocationController.setURL( rSettings.sDataSourceName );
120cdf0e10cSrcweir 		String sName = aURL.getName( );
121cdf0e10cSrcweir 		xub_StrLen nPos = sName.Search(String(aURL.GetExtension()));
122cdf0e10cSrcweir 		if ( nPos != STRING_NOTFOUND )
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			sName.Erase(nPos-1,4);
125cdf0e10cSrcweir 		}
126cdf0e10cSrcweir 		m_aName.SetText(sName);
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 		OnRegister(&m_aRegisterName);
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	//---------------------------------------------------------------------
initializePage()132cdf0e10cSrcweir 	void FinalPage::initializePage()
133cdf0e10cSrcweir 	{
134cdf0e10cSrcweir 		AddressBookSourcePage::initializePage();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		setFields();
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	//---------------------------------------------------------------------
commitPage(::svt::WizardTypes::CommitPageReason _eReason)140cdf0e10cSrcweir     sal_Bool FinalPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
141cdf0e10cSrcweir 	{
142cdf0e10cSrcweir 		if (!AddressBookSourcePage::commitPage(_eReason))
143cdf0e10cSrcweir 			return sal_False;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         if  (   ( ::svt::WizardTypes::eTravelBackward != _eReason )
146cdf0e10cSrcweir             &&  ( !m_aLocationController.prepareCommit() )
147cdf0e10cSrcweir             )
148cdf0e10cSrcweir             return sal_False;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 		AddressSettings& rSettings = getSettings();
151cdf0e10cSrcweir 		rSettings.sDataSourceName = m_aLocationController.getURL();
152cdf0e10cSrcweir 		rSettings.bRegisterDataSource = m_aRegisterName.IsChecked();
153cdf0e10cSrcweir 		if ( rSettings.bRegisterDataSource )
154cdf0e10cSrcweir 			rSettings.sRegisteredDataSourceName = m_aName.GetText();
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 		return sal_True;
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	//---------------------------------------------------------------------
ActivatePage()160cdf0e10cSrcweir 	void FinalPage::ActivatePage()
161cdf0e10cSrcweir 	{
162cdf0e10cSrcweir 		AddressBookSourcePage::ActivatePage();
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 		// get the names of all data sources
165cdf0e10cSrcweir 		ODataSourceContext aContext( getORB() );
166cdf0e10cSrcweir 		aContext.getDataSourceNames( m_aInvalidDataSourceNames );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 		// give the name edit the focus
169cdf0e10cSrcweir 		m_aLocation.GrabFocus();
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 		// default the finish button
172cdf0e10cSrcweir 		getDialog()->defaultButton( WZB_FINISH );
173cdf0e10cSrcweir 	}
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	//---------------------------------------------------------------------
DeactivatePage()176cdf0e10cSrcweir 	void FinalPage::DeactivatePage()
177cdf0e10cSrcweir 	{
178cdf0e10cSrcweir 		AddressBookSourcePage::DeactivatePage();
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 		// default the "next" button, again
181cdf0e10cSrcweir 		getDialog()->defaultButton( WZB_NEXT );
182cdf0e10cSrcweir 		// disable the finish button
183cdf0e10cSrcweir 		getDialog()->enableButtons( WZB_FINISH, sal_False );
184cdf0e10cSrcweir 	}
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	//---------------------------------------------------------------------
canAdvance() const187cdf0e10cSrcweir 	bool FinalPage::canAdvance() const
188cdf0e10cSrcweir 	{
189cdf0e10cSrcweir 		return false;
190cdf0e10cSrcweir 	}
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 	//---------------------------------------------------------------------
implCheckName()193cdf0e10cSrcweir 	void FinalPage::implCheckName()
194cdf0e10cSrcweir 	{
195cdf0e10cSrcweir 		sal_Bool bValidName = isValidName();
196cdf0e10cSrcweir 		sal_Bool bEmptyName = 0 == m_aName.GetText().Len();
197cdf0e10cSrcweir 		sal_Bool bEmptyLocation = 0 == m_aLocation.GetText().Len();
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		// enable or disable the finish button
200cdf0e10cSrcweir 		getDialog()->enableButtons( WZB_FINISH, !bEmptyLocation && (!m_aRegisterName.IsChecked() || bValidName) );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 		// show the error message for an invalid name
203cdf0e10cSrcweir 		m_aDuplicateNameError.Show( !bValidName && !bEmptyName );
204cdf0e10cSrcweir 	}
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 	//---------------------------------------------------------------------
207cdf0e10cSrcweir 	IMPL_LINK( FinalPage, OnNameModified, Edit*, /**/ )
208cdf0e10cSrcweir 	{
209cdf0e10cSrcweir 		implCheckName();
210cdf0e10cSrcweir 		return 0L;
211cdf0e10cSrcweir 	}
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     // -----------------------------------------------------------------------------
IMPL_LINK(FinalPage,OnRegister,CheckBox *,EMPTYARG)214cdf0e10cSrcweir 	IMPL_LINK( FinalPage, OnRegister, CheckBox*, EMPTYARG )
215cdf0e10cSrcweir 	{
216cdf0e10cSrcweir 		sal_Bool bEnable = m_aRegisterName.IsChecked();
217cdf0e10cSrcweir 		m_aNameLabel.Enable(bEnable);
218cdf0e10cSrcweir 		m_aName.Enable(bEnable);
219cdf0e10cSrcweir 		implCheckName();
220cdf0e10cSrcweir 		return 0L;
221cdf0e10cSrcweir 	}
222cdf0e10cSrcweir //.........................................................................
223cdf0e10cSrcweir }	// namespace abp
224cdf0e10cSrcweir //.........................................................................
225cdf0e10cSrcweir 
226