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