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_dbaccess.hxx"
26 
27 #include "admincontrols.hxx"
28 #include "admincontrols.hrc"
29 #include "dbu_dlg.hrc"
30 #include "dsitems.hxx"
31 #include "moduledbu.hxx"
32 
33 #include <svl/eitem.hxx>
34 #include <svl/stritem.hxx>
35 #include <svl/intitem.hxx>
36 
37 //........................................................................
38 namespace dbaui
39 {
40 //........................................................................
41 
42 	//====================================================================
43 	//= TextResetOperatorEventFilter
44 	//====================================================================
45     class TextResetOperatorEventFilter : public ::svt::IWindowEventFilter
46     {
47     public:
48         TextResetOperatorEventFilter()
49         {
50         }
51 
52         // IWindowEventFilter
53         virtual bool payAttentionTo( const VclWindowEvent& _rEvent ) const
54         {
55             return  ( _rEvent.GetId() == VCLEVENT_WINDOW_ENABLED )
56                 ||  ( _rEvent.GetId() == VCLEVENT_WINDOW_DISABLED )
57                 ||  ( _rEvent.GetId() == VCLEVENT_EDIT_MODIFY );
58         }
59     };
60 
61 	//====================================================================
62 	//= TextResetOperator
63 	//====================================================================
64     class TextResetOperator :public ::svt::IWindowOperator
65     {
66     public:
67         TextResetOperator( const String& _rDisabledText )
68             :m_sDisabledText( _rDisabledText )
69         {
70         }
71 
72         // IWindowOperator
73         virtual void operateOn( const VclWindowEvent& _rTrigger, Window& _rOperateOn ) const;
74 
75     private:
76         const String    m_sDisabledText;
77               String    m_sUserText;
78               sal_Bool      m_bLastKnownEnabledState;
79     };
80 
81 	//--------------------------------------------------------------------
82     void TextResetOperator::operateOn( const VclWindowEvent& _rTrigger, Window& _rOperateOn ) const
83     {
84         OSL_ENSURE( _rTrigger.GetWindow() == &_rOperateOn, "TextResetOperator::operateOn: you're misusing this implementation!" );
85 
86         switch ( _rTrigger.GetId() )
87         {
88         case 0:
89             // initial call
90             const_cast< TextResetOperator* >( this )->m_sUserText = _rTrigger.GetWindow()->GetText();
91             break;
92 
93         case VCLEVENT_EDIT_MODIFY:
94             if ( _rTrigger.GetWindow()->IsEnabled() )
95                 const_cast< TextResetOperator* >( this )->m_sUserText = _rTrigger.GetWindow()->GetText();
96             break;
97 
98         case VCLEVENT_WINDOW_ENABLED:
99             _rOperateOn.SetText( m_sUserText );
100             break;
101 
102         case VCLEVENT_WINDOW_DISABLED:
103             _rOperateOn.SetText( m_sDisabledText );
104             break;
105 
106         default:
107             OSL_ENSURE( false, "TextResetOperator::operateOn: unexpected event ID!" );
108             // all those IDs should have been filtered out by payAttentionTo
109             break;
110         }
111     }
112 
113 	//====================================================================
114 	//= TextResetOperatorController
115 	//====================================================================
116     class TextResetOperatorController_Base
117     {
118     protected:
119         TextResetOperatorController_Base( const String& _rDisabledText )
120             :m_pEventFilter( new TextResetOperatorEventFilter )
121             ,m_pOperator( new TextResetOperator( _rDisabledText ) )
122         {
123         }
124 
125         inline ::svt::PWindowEventFilter getEventFilter() const   { return m_pEventFilter; }
126         inline ::svt::PWindowOperator    getOperator() const      { return m_pOperator; }
127 
128     private:
129         ::svt::PWindowEventFilter   m_pEventFilter;
130         ::svt::PWindowOperator      m_pOperator;
131     };
132 
133     class TextResetOperatorController   :public TextResetOperatorController_Base
134                                 ,public ::svt::DialogController
135     {
136     public:
137         TextResetOperatorController( Window& _rObservee, const String& _rDisabledText )
138             :TextResetOperatorController_Base( _rDisabledText )
139             ,::svt::DialogController( _rObservee, getEventFilter(), getOperator() )
140         {
141             addDependentWindow( _rObservee );
142         }
143     };
144 
145 	//====================================================================
146 	//= MySQLNativeSettings
147 	//====================================================================
148 	//--------------------------------------------------------------------
149     MySQLNativeSettings::MySQLNativeSettings( Window& _rParent, const Link& _rControlModificationLink )
150         :Control( &_rParent, ModuleRes( RID_MYSQL_NATIVE_SETTINGS ).SetAutoRelease( sal_False ) )
151         ,m_aDatabaseNameLabel	( this, ModuleRes( FT_MYSQL_DATABASE_NAME ) )
152 		,m_aDatabaseName	    ( this, ModuleRes( ED_MYSQL_DATABASE_NAME ) )
153         ,m_aHostPortRadio       ( this, ModuleRes( RB_MYSQL_HOST_PORT ) )
154         ,m_aSocketRadio		    ( this, ModuleRes( RB_MYSQL_SOCKET ) )
155         ,m_aNamedPipeRadio		( this, ModuleRes( RB_MYSQL_NAMED_PIPE ) )
156         ,m_aHostNameLabel		( this, ModuleRes( FT_COMMON_HOST_NAME ) )
157 		,m_aHostName		    ( this, ModuleRes( ED_COMMON_HOST_NAME ) )
158 		,m_aPortLabel	        ( this, ModuleRes( FT_COMMON_PORT ) )
159 		,m_aPort	            ( this, ModuleRes( NF_COMMON_PORT ) )
160         ,m_aDefaultPort         ( this, ModuleRes( FT_COMMON_PORT_DEFAULT ) )
161 		,m_aSocket		        ( this, ModuleRes( ED_MYSQL_SOCKET ) )
162 		,m_aNamedPipe		    ( this, ModuleRes( ED_MYSQL_NAMED_PIPE ) )
163     {
164         FreeResource();
165 
166         m_aDatabaseName.SetModifyHdl( _rControlModificationLink );
167         m_aHostName.SetModifyHdl( _rControlModificationLink );
168         m_aPort.SetModifyHdl( _rControlModificationLink );
169         m_aNamedPipe.SetModifyHdl( _rControlModificationLink );
170         m_aSocketRadio.SetToggleHdl( _rControlModificationLink );
171         m_aNamedPipeRadio.SetToggleHdl( _rControlModificationLink );
172 
173         m_aControlDependencies.enableOnRadioCheck( m_aHostPortRadio, m_aHostNameLabel, m_aHostName, m_aPortLabel, m_aPort, m_aDefaultPort );
174         m_aControlDependencies.enableOnRadioCheck( m_aSocketRadio, m_aSocket );
175         m_aControlDependencies.enableOnRadioCheck( m_aNamedPipeRadio, m_aNamedPipe );
176 
177         m_aControlDependencies.addController( ::svt::PDialogController(
178             new TextResetOperatorController( m_aHostName, String::CreateFromAscii( "localhost" ) )
179         ) );
180 
181         // sockets are available on Unix systems only, named pipes only on Windows
182 #ifdef UNX
183         m_aNamedPipeRadio.Hide();
184         m_aNamedPipe.Hide();
185 #else
186         m_aSocketRadio.Hide();
187         m_aSocket.Hide();
188 #endif
189     }
190 
191 	//--------------------------------------------------------------------
192     MySQLNativeSettings::~MySQLNativeSettings()
193     {
194     }
195 
196 	//--------------------------------------------------------------------
197 	void MySQLNativeSettings::fillControls( ::std::vector< ISaveValueWrapper* >& _rControlList )
198     {
199         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aDatabaseName ) );
200         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aHostName ) );
201         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aPort ) );
202         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aSocket ) );
203         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aNamedPipe ) );
204     }
205 
206 	//--------------------------------------------------------------------
207 	void MySQLNativeSettings::fillWindows( ::std::vector< ISaveValueWrapper* >& _rControlList )
208     {
209         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aDatabaseNameLabel ) );
210         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aHostNameLabel ) );
211         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aPortLabel ) );
212         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aDefaultPort ) );
213         _rControlList.push_back( new ODisableWrapper< RadioButton >( &m_aSocketRadio ) );
214         _rControlList.push_back( new ODisableWrapper< RadioButton >( &m_aNamedPipeRadio ) );
215     }
216 
217 	//--------------------------------------------------------------------
218     sal_Bool MySQLNativeSettings::FillItemSet( SfxItemSet& _rSet )
219     {
220 		sal_Bool bChangedSomething = sal_False;
221 
222         OGenericAdministrationPage::fillString( _rSet, &m_aHostName,     DSID_CONN_HOSTNAME,    bChangedSomething );
223         OGenericAdministrationPage::fillString( _rSet, &m_aDatabaseName, DSID_DATABASENAME,     bChangedSomething );
224         OGenericAdministrationPage::fillInt32 ( _rSet, &m_aPort,         DSID_MYSQL_PORTNUMBER, bChangedSomething );
225 #ifdef UNX
226         OGenericAdministrationPage::fillString( _rSet, &m_aSocket,       DSID_CONN_SOCKET,      bChangedSomething );
227 #else
228         OGenericAdministrationPage::fillString( _rSet, &m_aNamedPipe,    DSID_NAMED_PIPE,       bChangedSomething );
229 #endif
230 
231         return bChangedSomething;
232     }
233 
234 	//--------------------------------------------------------------------
235 	void MySQLNativeSettings::implInitControls(const SfxItemSet& _rSet )
236     {
237 		SFX_ITEMSET_GET( _rSet, pInvalid, SfxBoolItem, DSID_INVALID_SELECTION, sal_True );
238 		bool bValid = !pInvalid || !pInvalid->GetValue();
239         if ( !bValid )
240             return;
241 
242         SFX_ITEMSET_GET( _rSet, pDatabaseName,  SfxStringItem,  DSID_DATABASENAME,      sal_True );
243         SFX_ITEMSET_GET( _rSet, pHostName,      SfxStringItem,  DSID_CONN_HOSTNAME,     sal_True );
244         SFX_ITEMSET_GET( _rSet, pPortNumber,    SfxInt32Item,   DSID_MYSQL_PORTNUMBER,  sal_True );
245         SFX_ITEMSET_GET( _rSet, pSocket,        SfxStringItem,  DSID_CONN_SOCKET,       sal_True );
246         SFX_ITEMSET_GET( _rSet, pNamedPipe,     SfxStringItem,  DSID_NAMED_PIPE,       sal_True );
247 
248         m_aDatabaseName.SetText( pDatabaseName->GetValue() );
249         m_aDatabaseName.ClearModifyFlag();
250 
251         m_aHostName.SetText( pHostName->GetValue() );
252         m_aHostName.ClearModifyFlag();
253 
254         m_aPort.SetValue( pPortNumber->GetValue() );
255         m_aPort.ClearModifyFlag();
256 
257         m_aSocket.SetText( pSocket->GetValue() );
258         m_aSocket.ClearModifyFlag();
259 
260         m_aNamedPipe.SetText( pNamedPipe->GetValue() );
261         m_aNamedPipe.ClearModifyFlag();
262 
263         // if a socket (on Unix) or a pipe name (on Windows) is given, this is preferred over
264         // the port
265 #ifdef UNX
266         RadioButton& rSocketPipeRadio = m_aSocketRadio;
267         const SfxStringItem* pSocketPipeItem = pSocket;
268 #else
269         RadioButton& rSocketPipeRadio = m_aNamedPipeRadio;
270         const SfxStringItem* pSocketPipeItem = pNamedPipe;
271 #endif
272         String sSocketPipe( pSocketPipeItem->GetValue() );
273         if ( sSocketPipe.Len() > 0 )
274             rSocketPipeRadio.Check();
275         else
276             m_aHostPortRadio.Check();
277     }
278 
279     //--------------------------------------------------------------------
280 	bool MySQLNativeSettings::canAdvance() const
281     {
282         if ( m_aDatabaseName.GetText().Len() == 0 )
283             return false;
284 
285         if  (   m_aHostPortRadio.IsChecked()
286             &&  (   ( m_aHostName.GetText().Len() == 0 )
287                 ||  ( m_aPort.GetText().Len() == 0 )
288                 )
289             )
290             return false;
291 
292 #ifdef UNX
293         if  (   ( m_aSocketRadio.IsChecked() )
294             &&  ( m_aSocket.GetText().Len() == 0 )
295             )
296 #else
297         if  (   ( m_aNamedPipeRadio.IsChecked() )
298             &&  ( m_aNamedPipe.GetText().Len() == 0 )
299             )
300 #endif
301             return false;
302 
303         return true;
304     }
305 
306 //........................................................................
307 } // namespace dbaui
308 //........................................................................
309