1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "abpfinalpage.hxx"
31 #include "addresssettings.hxx"
32 #include "abspilot.hxx"
33 #include <tools/debug.hxx>
34 #include <tools/urlobj.hxx>
35 #include <unotools/ucbhelper.hxx>
36 #include <sfx2/filedlghelper.hxx>
37 #include <unotools/pathoptions.hxx>
38 #ifndef SVTOOLS_FILENOTATION_HXX_
39 #include <svl/filenotation.hxx>
40 #endif
41 #include <sfx2/docfilt.hxx>
42 #include <vcl/msgbox.hxx>
43 #include <comphelper/componentcontext.hxx>
44 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
45 
46 //.........................................................................
47 namespace abp
48 {
49 //.........................................................................
50 	using namespace ::svt;
51 	using namespace ::utl;
52 
53 	const SfxFilter* lcl_getBaseFilter()
54 	{
55 		static const String s_sDatabaseType = String::CreateFromAscii("StarOffice XML (Base)");
56 		const SfxFilter* pFilter = SfxFilter::GetFilterByName( s_sDatabaseType);
57 		OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
58 		return pFilter;
59 	}
60 	//=====================================================================
61 	//= FinalPage
62 	//=====================================================================
63 	//---------------------------------------------------------------------
64 	FinalPage::FinalPage( OAddessBookSourcePilot* _pParent )
65 		:AddressBookSourcePage(_pParent, ModuleRes(RID_PAGE_FINAL))
66 		,m_aExplanation			( this, ModuleRes( FT_FINISH_EXPL ) )
67 		,m_aLocationLabel		( this, ModuleRes( FT_LOCATION ) )
68 		,m_aLocation			( this, ModuleRes( CBB_LOCATION ) )
69 		,m_aBrowse				( this, ModuleRes( PB_BROWSE ) )
70 		,m_aRegisterName		( this, ModuleRes( CB_REGISTER_DS ) )
71 		,m_aNameLabel			( this, ModuleRes( FT_NAME_EXPL ) )
72 		,m_aName				( this, ModuleRes( ET_DATASOURCENAME ) )
73 		,m_aDuplicateNameError	( this, ModuleRes( FT_DUPLICATENAME ) )
74         ,m_aLocationController( ::comphelper::ComponentContext( _pParent->getORB() ), m_aLocation, m_aBrowse )
75 	{
76 		FreeResource();
77 
78 		m_aName.SetModifyHdl( LINK(this, FinalPage, OnNameModified) );
79 		m_aLocation.SetModifyHdl( LINK(this, FinalPage, OnNameModified) );
80 		m_aRegisterName.SetClickHdl( LINK( this, FinalPage, OnRegister ) );
81 		m_aRegisterName.Check(sal_True);
82 	}
83 
84 	//---------------------------------------------------------------------
85 	sal_Bool FinalPage::isValidName() const
86 	{
87 		::rtl::OUString sCurrentName(m_aName.GetText());
88 
89 		if (0 == sCurrentName.getLength())
90 			// the name must not be empty
91 			return sal_False;
92 
93 		if ( m_aInvalidDataSourceNames.find( sCurrentName ) != m_aInvalidDataSourceNames.end() )
94 			// there already is a data source with this name
95 			return sal_False;
96 
97 		return sal_True;
98 	}
99 
100 	//---------------------------------------------------------------------
101 	void FinalPage::setFields()
102 	{
103 		AddressSettings& rSettings = getSettings();
104 
105 		INetURLObject aURL( rSettings.sDataSourceName );
106 		if( aURL.GetProtocol() == INET_PROT_NOT_VALID )
107 		{
108 			String sPath = SvtPathOptions().GetWorkPath();
109 			sPath += '/';
110 			sPath += String(rSettings.sDataSourceName);
111 
112 			const SfxFilter* pFilter = lcl_getBaseFilter();
113 			if ( pFilter )
114 			{
115 				String sExt = pFilter->GetDefaultExtension();
116 				sPath += sExt.GetToken(1,'*');
117 			}
118 
119 			aURL.SetURL(sPath);
120 		}
121 		OSL_ENSURE( aURL.GetProtocol() != INET_PROT_NOT_VALID ,"No valid file name!");
122 		rSettings.sDataSourceName = aURL.GetMainURL( INetURLObject::NO_DECODE );
123         m_aLocationController.setURL( rSettings.sDataSourceName );
124 		String sName = aURL.getName( );
125 		xub_StrLen nPos = sName.Search(String(aURL.GetExtension()));
126 		if ( nPos != STRING_NOTFOUND )
127 		{
128 			sName.Erase(nPos-1,4);
129 		}
130 		m_aName.SetText(sName);
131 
132 		OnRegister(&m_aRegisterName);
133 	}
134 
135 	//---------------------------------------------------------------------
136 	void FinalPage::initializePage()
137 	{
138 		AddressBookSourcePage::initializePage();
139 
140 		setFields();
141 	}
142 
143 	//---------------------------------------------------------------------
144     sal_Bool FinalPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
145 	{
146 		if (!AddressBookSourcePage::commitPage(_eReason))
147 			return sal_False;
148 
149         if  (   ( ::svt::WizardTypes::eTravelBackward != _eReason )
150             &&  ( !m_aLocationController.prepareCommit() )
151             )
152             return sal_False;
153 
154 		AddressSettings& rSettings = getSettings();
155 		rSettings.sDataSourceName = m_aLocationController.getURL();
156 		rSettings.bRegisterDataSource = m_aRegisterName.IsChecked();
157 		if ( rSettings.bRegisterDataSource )
158 			rSettings.sRegisteredDataSourceName = m_aName.GetText();
159 
160 		return sal_True;
161 	}
162 
163 	//---------------------------------------------------------------------
164 	void FinalPage::ActivatePage()
165 	{
166 		AddressBookSourcePage::ActivatePage();
167 
168 		// get the names of all data sources
169 		ODataSourceContext aContext( getORB() );
170 		aContext.getDataSourceNames( m_aInvalidDataSourceNames );
171 
172 		// give the name edit the focus
173 		m_aLocation.GrabFocus();
174 
175 		// default the finish button
176 		getDialog()->defaultButton( WZB_FINISH );
177 	}
178 
179 	//---------------------------------------------------------------------
180 	void FinalPage::DeactivatePage()
181 	{
182 		AddressBookSourcePage::DeactivatePage();
183 
184 		// default the "next" button, again
185 		getDialog()->defaultButton( WZB_NEXT );
186 		// disable the finish button
187 		getDialog()->enableButtons( WZB_FINISH, sal_False );
188 	}
189 
190 	//---------------------------------------------------------------------
191 	bool FinalPage::canAdvance() const
192 	{
193 		return false;
194 	}
195 
196 	//---------------------------------------------------------------------
197 	void FinalPage::implCheckName()
198 	{
199 		sal_Bool bValidName = isValidName();
200 		sal_Bool bEmptyName = 0 == m_aName.GetText().Len();
201 		sal_Bool bEmptyLocation = 0 == m_aLocation.GetText().Len();
202 
203 		// enable or disable the finish button
204 		getDialog()->enableButtons( WZB_FINISH, !bEmptyLocation && (!m_aRegisterName.IsChecked() || bValidName) );
205 
206 		// show the error message for an invalid name
207 		m_aDuplicateNameError.Show( !bValidName && !bEmptyName );
208 	}
209 
210 	//---------------------------------------------------------------------
211 	IMPL_LINK( FinalPage, OnNameModified, Edit*, /**/ )
212 	{
213 		implCheckName();
214 		return 0L;
215 	}
216 
217     // -----------------------------------------------------------------------------
218 	IMPL_LINK( FinalPage, OnRegister, CheckBox*, EMPTYARG )
219 	{
220 		sal_Bool bEnable = m_aRegisterName.IsChecked();
221 		m_aNameLabel.Enable(bEnable);
222 		m_aName.Enable(bEnable);
223 		implCheckName();
224 		return 0L;
225 	}
226 //.........................................................................
227 }	// namespace abp
228 //.........................................................................
229 
230