1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25 ************************************************************************/
26 
27 // MARKER(update_precomp.py): autogen include statement, do not remove
28 #include "precompiled_dbaccess.hxx"
29 
30 #include "admincontrols.hxx"
31 #include "admincontrols.hrc"
32 #include "dbu_dlg.hrc"
33 #include "dsitems.hxx"
34 #include "moduledbu.hxx"
35 
36 #include <svl/eitem.hxx>
37 #include <svl/stritem.hxx>
38 #include <svl/intitem.hxx>
39 
40 //........................................................................
41 namespace dbaui
42 {
43 //........................................................................
44 
45 	//====================================================================
46 	//= TextResetOperatorEventFilter
47 	//====================================================================
48     class TextResetOperatorEventFilter : public ::svt::IWindowEventFilter
49     {
50     public:
51         TextResetOperatorEventFilter()
52         {
53         }
54 
55         // IWindowEventFilter
56         virtual bool payAttentionTo( const VclWindowEvent& _rEvent ) const
57         {
58             return  ( _rEvent.GetId() == VCLEVENT_WINDOW_ENABLED )
59                 ||  ( _rEvent.GetId() == VCLEVENT_WINDOW_DISABLED )
60                 ||  ( _rEvent.GetId() == VCLEVENT_EDIT_MODIFY );
61         }
62     };
63 
64 	//====================================================================
65 	//= TextResetOperator
66 	//====================================================================
67     class TextResetOperator :public ::svt::IWindowOperator
68     {
69     public:
70         TextResetOperator( const String& _rDisabledText )
71             :m_sDisabledText( _rDisabledText )
72         {
73         }
74 
75         // IWindowOperator
76         virtual void operateOn( const VclWindowEvent& _rTrigger, Window& _rOperateOn ) const;
77 
78     private:
79         const String    m_sDisabledText;
80               String    m_sUserText;
81               sal_Bool      m_bLastKnownEnabledState;
82     };
83 
84 	//--------------------------------------------------------------------
85     void TextResetOperator::operateOn( const VclWindowEvent& _rTrigger, Window& _rOperateOn ) const
86     {
87         OSL_ENSURE( _rTrigger.GetWindow() == &_rOperateOn, "TextResetOperator::operateOn: you're misusing this implementation!" );
88 
89         switch ( _rTrigger.GetId() )
90         {
91         case 0:
92             // initial call
93             const_cast< TextResetOperator* >( this )->m_sUserText = _rTrigger.GetWindow()->GetText();
94             break;
95 
96         case VCLEVENT_EDIT_MODIFY:
97             if ( _rTrigger.GetWindow()->IsEnabled() )
98                 const_cast< TextResetOperator* >( this )->m_sUserText = _rTrigger.GetWindow()->GetText();
99             break;
100 
101         case VCLEVENT_WINDOW_ENABLED:
102             _rOperateOn.SetText( m_sUserText );
103             break;
104 
105         case VCLEVENT_WINDOW_DISABLED:
106             _rOperateOn.SetText( m_sDisabledText );
107             break;
108 
109         default:
110             OSL_ENSURE( false, "TextResetOperator::operateOn: unexpected event ID!" );
111             // all those IDs should have been filtered out by payAttentionTo
112             break;
113         }
114     }
115 
116 	//====================================================================
117 	//= TextResetOperatorController
118 	//====================================================================
119     class TextResetOperatorController_Base
120     {
121     protected:
122         TextResetOperatorController_Base( const String& _rDisabledText )
123             :m_pEventFilter( new TextResetOperatorEventFilter )
124             ,m_pOperator( new TextResetOperator( _rDisabledText ) )
125         {
126         }
127 
128         inline ::svt::PWindowEventFilter getEventFilter() const   { return m_pEventFilter; }
129         inline ::svt::PWindowOperator    getOperator() const      { return m_pOperator; }
130 
131     private:
132         ::svt::PWindowEventFilter   m_pEventFilter;
133         ::svt::PWindowOperator      m_pOperator;
134     };
135 
136     class TextResetOperatorController   :public TextResetOperatorController_Base
137                                 ,public ::svt::DialogController
138     {
139     public:
140         TextResetOperatorController( Window& _rObservee, const String& _rDisabledText )
141             :TextResetOperatorController_Base( _rDisabledText )
142             ,::svt::DialogController( _rObservee, getEventFilter(), getOperator() )
143         {
144             addDependentWindow( _rObservee );
145         }
146     };
147 
148 	//====================================================================
149 	//= MySQLNativeSettings
150 	//====================================================================
151 	//--------------------------------------------------------------------
152     MySQLNativeSettings::MySQLNativeSettings( Window& _rParent, const Link& _rControlModificationLink )
153         :Control( &_rParent, ModuleRes( RID_MYSQL_NATIVE_SETTINGS ).SetAutoRelease( sal_False ) )
154         ,m_aDatabaseNameLabel	( this, ModuleRes( FT_MYSQL_DATABASE_NAME ) )
155 		,m_aDatabaseName	    ( this, ModuleRes( ED_MYSQL_DATABASE_NAME ) )
156         ,m_aHostPortRadio       ( this, ModuleRes( RB_MYSQL_HOST_PORT ) )
157         ,m_aSocketRadio		    ( this, ModuleRes( RB_MYSQL_SOCKET ) )
158         ,m_aNamedPipeRadio		( this, ModuleRes( RB_MYSQL_NAMED_PIPE ) )
159         ,m_aHostNameLabel		( this, ModuleRes( FT_COMMON_HOST_NAME ) )
160 		,m_aHostName		    ( this, ModuleRes( ED_COMMON_HOST_NAME ) )
161 		,m_aPortLabel	        ( this, ModuleRes( FT_COMMON_PORT ) )
162 		,m_aPort	            ( this, ModuleRes( NF_COMMON_PORT ) )
163         ,m_aDefaultPort         ( this, ModuleRes( FT_COMMON_PORT_DEFAULT ) )
164 		,m_aSocket		        ( this, ModuleRes( ED_MYSQL_SOCKET ) )
165 		,m_aNamedPipe		    ( this, ModuleRes( ED_MYSQL_NAMED_PIPE ) )
166     {
167         FreeResource();
168 
169         m_aDatabaseName.SetModifyHdl( _rControlModificationLink );
170         m_aHostName.SetModifyHdl( _rControlModificationLink );
171         m_aPort.SetModifyHdl( _rControlModificationLink );
172         m_aNamedPipe.SetModifyHdl( _rControlModificationLink );
173         m_aSocketRadio.SetToggleHdl( _rControlModificationLink );
174         m_aNamedPipeRadio.SetToggleHdl( _rControlModificationLink );
175 
176         m_aControlDependencies.enableOnRadioCheck( m_aHostPortRadio, m_aHostNameLabel, m_aHostName, m_aPortLabel, m_aPort, m_aDefaultPort );
177         m_aControlDependencies.enableOnRadioCheck( m_aSocketRadio, m_aSocket );
178         m_aControlDependencies.enableOnRadioCheck( m_aNamedPipeRadio, m_aNamedPipe );
179 
180         m_aControlDependencies.addController( ::svt::PDialogController(
181             new TextResetOperatorController( m_aHostName, String::CreateFromAscii( "localhost" ) )
182         ) );
183 
184         // sockets are available on Unix systems only, named pipes only on Windows
185 #ifdef UNX
186         m_aNamedPipeRadio.Hide();
187         m_aNamedPipe.Hide();
188 #else
189         m_aSocketRadio.Hide();
190         m_aSocket.Hide();
191 #endif
192     }
193 
194 	//--------------------------------------------------------------------
195     MySQLNativeSettings::~MySQLNativeSettings()
196     {
197     }
198 
199 	//--------------------------------------------------------------------
200 	void MySQLNativeSettings::fillControls( ::std::vector< ISaveValueWrapper* >& _rControlList )
201     {
202         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aDatabaseName ) );
203         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aHostName ) );
204         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aPort ) );
205         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aSocket ) );
206         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aNamedPipe ) );
207     }
208 
209 	//--------------------------------------------------------------------
210 	void MySQLNativeSettings::fillWindows( ::std::vector< ISaveValueWrapper* >& _rControlList )
211     {
212         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aDatabaseNameLabel ) );
213         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aHostNameLabel ) );
214         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aPortLabel ) );
215         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aDefaultPort ) );
216         _rControlList.push_back( new ODisableWrapper< RadioButton >( &m_aSocketRadio ) );
217         _rControlList.push_back( new ODisableWrapper< RadioButton >( &m_aNamedPipeRadio ) );
218     }
219 
220 	//--------------------------------------------------------------------
221     sal_Bool MySQLNativeSettings::FillItemSet( SfxItemSet& _rSet )
222     {
223 		sal_Bool bChangedSomething = sal_False;
224 
225         OGenericAdministrationPage::fillString( _rSet, &m_aHostName,     DSID_CONN_HOSTNAME,    bChangedSomething );
226         OGenericAdministrationPage::fillString( _rSet, &m_aDatabaseName, DSID_DATABASENAME,     bChangedSomething );
227         OGenericAdministrationPage::fillInt32 ( _rSet, &m_aPort,         DSID_MYSQL_PORTNUMBER, bChangedSomething );
228 #ifdef UNX
229         OGenericAdministrationPage::fillString( _rSet, &m_aSocket,       DSID_CONN_SOCKET,      bChangedSomething );
230 #else
231         OGenericAdministrationPage::fillString( _rSet, &m_aNamedPipe,    DSID_NAMED_PIPE,       bChangedSomething );
232 #endif
233 
234         return bChangedSomething;
235     }
236 
237 	//--------------------------------------------------------------------
238 	void MySQLNativeSettings::implInitControls(const SfxItemSet& _rSet )
239     {
240 		SFX_ITEMSET_GET( _rSet, pInvalid, SfxBoolItem, DSID_INVALID_SELECTION, sal_True );
241 		bool bValid = !pInvalid || !pInvalid->GetValue();
242         if ( !bValid )
243             return;
244 
245         SFX_ITEMSET_GET( _rSet, pDatabaseName,  SfxStringItem,  DSID_DATABASENAME,      sal_True );
246         SFX_ITEMSET_GET( _rSet, pHostName,      SfxStringItem,  DSID_CONN_HOSTNAME,     sal_True );
247         SFX_ITEMSET_GET( _rSet, pPortNumber,    SfxInt32Item,   DSID_MYSQL_PORTNUMBER,  sal_True );
248         SFX_ITEMSET_GET( _rSet, pSocket,        SfxStringItem,  DSID_CONN_SOCKET,       sal_True );
249         SFX_ITEMSET_GET( _rSet, pNamedPipe,     SfxStringItem,  DSID_NAMED_PIPE,       sal_True );
250 
251         m_aDatabaseName.SetText( pDatabaseName->GetValue() );
252         m_aDatabaseName.ClearModifyFlag();
253 
254         m_aHostName.SetText( pHostName->GetValue() );
255         m_aHostName.ClearModifyFlag();
256 
257         m_aPort.SetValue( pPortNumber->GetValue() );
258         m_aPort.ClearModifyFlag();
259 
260         m_aSocket.SetText( pSocket->GetValue() );
261         m_aSocket.ClearModifyFlag();
262 
263         m_aNamedPipe.SetText( pNamedPipe->GetValue() );
264         m_aNamedPipe.ClearModifyFlag();
265 
266         // if a socket (on Unix) or a pipe name (on Windows) is given, this is preferred over
267         // the port
268 #ifdef UNX
269         RadioButton& rSocketPipeRadio = m_aSocketRadio;
270         const SfxStringItem* pSocketPipeItem = pSocket;
271 #else
272         RadioButton& rSocketPipeRadio = m_aNamedPipeRadio;
273         const SfxStringItem* pSocketPipeItem = pNamedPipe;
274 #endif
275         String sSocketPipe( pSocketPipeItem->GetValue() );
276         if ( sSocketPipe.Len() > 0 )
277             rSocketPipeRadio.Check();
278         else
279             m_aHostPortRadio.Check();
280     }
281 
282     //--------------------------------------------------------------------
283 	bool MySQLNativeSettings::canAdvance() const
284     {
285         if ( m_aDatabaseName.GetText().Len() == 0 )
286             return false;
287 
288         if  (   m_aHostPortRadio.IsChecked()
289             &&  (   ( m_aHostName.GetText().Len() == 0 )
290                 ||  ( m_aPort.GetText().Len() == 0 )
291                 )
292             )
293             return false;
294 
295 #ifdef UNX
296         if  (   ( m_aSocketRadio.IsChecked() )
297             &&  ( m_aSocket.GetText().Len() == 0 )
298             )
299 #else
300         if  (   ( m_aNamedPipeRadio.IsChecked() )
301             &&  ( m_aNamedPipe.GetText().Len() == 0 )
302             )
303 #endif
304             return false;
305 
306         return true;
307     }
308 
309 //........................................................................
310 } // namespace dbaui
311 //........................................................................
312