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_dbui.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:
TextResetOperatorEventFilter()48         TextResetOperatorEventFilter()
49         {
50         }
51 
52         // IWindowEventFilter
payAttentionTo(const VclWindowEvent & _rEvent) const53         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:
TextResetOperator(const String & _rDisabledText)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 	//--------------------------------------------------------------------
operateOn(const VclWindowEvent & _rTrigger,Window & _rOperateOn) const82     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:
TextResetOperatorController_Base(const String & _rDisabledText)119         TextResetOperatorController_Base( const String& _rDisabledText )
120             :m_pEventFilter( new TextResetOperatorEventFilter )
121             ,m_pOperator( new TextResetOperator( _rDisabledText ) )
122         {
123         }
124 
getEventFilter() const125         inline ::svt::PWindowEventFilter getEventFilter() const   { return m_pEventFilter; }
getOperator() const126         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:
TextResetOperatorController(Window & _rObservee,const String & _rDisabledText)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 	//--------------------------------------------------------------------
MySQLNativeSettings(Window & _rParent,const Link & _rControlModificationLink)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_aSocket.SetModifyHdl( _rControlModificationLink );
170         m_aNamedPipe.SetModifyHdl( _rControlModificationLink );
171         m_aSocketRadio.SetToggleHdl( _rControlModificationLink );
172         m_aNamedPipeRadio.SetToggleHdl( _rControlModificationLink );
173 
174         m_aControlDependencies.enableOnRadioCheck( m_aHostPortRadio, m_aHostNameLabel, m_aHostName, m_aPortLabel, m_aPort, m_aDefaultPort );
175         m_aControlDependencies.enableOnRadioCheck( m_aSocketRadio, m_aSocket );
176         m_aControlDependencies.enableOnRadioCheck( m_aNamedPipeRadio, m_aNamedPipe );
177 
178         m_aControlDependencies.addController( ::svt::PDialogController(
179             new TextResetOperatorController( m_aHostName, String::CreateFromAscii( "localhost" ) )
180         ) );
181 
182         // sockets are available on Unix systems only, named pipes only on Windows
183 #ifdef UNX
184         m_aNamedPipeRadio.Hide();
185         m_aNamedPipe.Hide();
186 #else
187         m_aSocketRadio.Hide();
188         m_aSocket.Hide();
189 #endif
190     }
191 
192 	//--------------------------------------------------------------------
~MySQLNativeSettings()193     MySQLNativeSettings::~MySQLNativeSettings()
194     {
195     }
196 
197 	//--------------------------------------------------------------------
fillControls(::std::vector<ISaveValueWrapper * > & _rControlList)198 	void MySQLNativeSettings::fillControls( ::std::vector< ISaveValueWrapper* >& _rControlList )
199     {
200         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aDatabaseName ) );
201         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aHostName ) );
202         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aPort ) );
203         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aSocket ) );
204         _rControlList.push_back( new OSaveValueWrapper< Edit >( &m_aNamedPipe ) );
205     }
206 
207 	//--------------------------------------------------------------------
fillWindows(::std::vector<ISaveValueWrapper * > & _rControlList)208 	void MySQLNativeSettings::fillWindows( ::std::vector< ISaveValueWrapper* >& _rControlList )
209     {
210         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aDatabaseNameLabel ) );
211         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aHostNameLabel ) );
212         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aPortLabel ) );
213         _rControlList.push_back( new ODisableWrapper< FixedText >( &m_aDefaultPort ) );
214         _rControlList.push_back( new ODisableWrapper< RadioButton >( &m_aSocketRadio ) );
215         _rControlList.push_back( new ODisableWrapper< RadioButton >( &m_aNamedPipeRadio ) );
216     }
217 
218 	//--------------------------------------------------------------------
FillItemSet(SfxItemSet & _rSet)219     sal_Bool MySQLNativeSettings::FillItemSet( SfxItemSet& _rSet )
220     {
221 		sal_Bool bChangedSomething = sal_False;
222 
223         OGenericAdministrationPage::fillString( _rSet, &m_aHostName,     DSID_CONN_HOSTNAME,    bChangedSomething );
224         OGenericAdministrationPage::fillString( _rSet, &m_aDatabaseName, DSID_DATABASENAME,     bChangedSomething );
225         OGenericAdministrationPage::fillInt32 ( _rSet, &m_aPort,         DSID_MYSQL_PORTNUMBER, bChangedSomething );
226 #ifdef UNX
227         OGenericAdministrationPage::fillString( _rSet, &m_aSocket,       DSID_CONN_SOCKET,      bChangedSomething );
228 #else
229         OGenericAdministrationPage::fillString( _rSet, &m_aNamedPipe,    DSID_NAMED_PIPE,       bChangedSomething );
230 #endif
231 
232         return bChangedSomething;
233     }
234 
235 	//--------------------------------------------------------------------
implInitControls(const SfxItemSet & _rSet)236 	void MySQLNativeSettings::implInitControls(const SfxItemSet& _rSet )
237     {
238 		SFX_ITEMSET_GET( _rSet, pInvalid, SfxBoolItem, DSID_INVALID_SELECTION, sal_True );
239 		bool bValid = !pInvalid || !pInvalid->GetValue();
240         if ( !bValid )
241             return;
242 
243         SFX_ITEMSET_GET( _rSet, pDatabaseName,  SfxStringItem,  DSID_DATABASENAME,      sal_True );
244         SFX_ITEMSET_GET( _rSet, pHostName,      SfxStringItem,  DSID_CONN_HOSTNAME,     sal_True );
245         SFX_ITEMSET_GET( _rSet, pPortNumber,    SfxInt32Item,   DSID_MYSQL_PORTNUMBER,  sal_True );
246         SFX_ITEMSET_GET( _rSet, pSocket,        SfxStringItem,  DSID_CONN_SOCKET,       sal_True );
247         SFX_ITEMSET_GET( _rSet, pNamedPipe,     SfxStringItem,  DSID_NAMED_PIPE,       sal_True );
248 
249         m_aDatabaseName.SetText( pDatabaseName->GetValue() );
250         m_aDatabaseName.ClearModifyFlag();
251 
252         m_aHostName.SetText( pHostName->GetValue() );
253         m_aHostName.ClearModifyFlag();
254 
255         m_aPort.SetValue( pPortNumber->GetValue() );
256         m_aPort.ClearModifyFlag();
257 
258         m_aSocket.SetText( pSocket->GetValue() );
259         m_aSocket.ClearModifyFlag();
260 
261         m_aNamedPipe.SetText( pNamedPipe->GetValue() );
262         m_aNamedPipe.ClearModifyFlag();
263 
264         // if a socket (on Unix) or a pipe name (on Windows) is given, this is preferred over
265         // the port
266 #ifdef UNX
267         RadioButton& rSocketPipeRadio = m_aSocketRadio;
268         const SfxStringItem* pSocketPipeItem = pSocket;
269 #else
270         RadioButton& rSocketPipeRadio = m_aNamedPipeRadio;
271         const SfxStringItem* pSocketPipeItem = pNamedPipe;
272 #endif
273         String sSocketPipe( pSocketPipeItem->GetValue() );
274         if ( sSocketPipe.Len() > 0 )
275             rSocketPipeRadio.Check();
276         else
277             m_aHostPortRadio.Check();
278     }
279 
280     //--------------------------------------------------------------------
canAdvance() const281 	bool MySQLNativeSettings::canAdvance() const
282     {
283         if ( m_aDatabaseName.GetText().Len() == 0 )
284             return false;
285 
286         if  (   m_aHostPortRadio.IsChecked()
287             &&  (   ( m_aHostName.GetText().Len() == 0 )
288                 ||  ( m_aPort.GetText().Len() == 0 )
289                 )
290             )
291             return false;
292 
293 #ifdef UNX
294         if  (   ( m_aSocketRadio.IsChecked() )
295             &&  ( m_aSocket.GetText().Len() == 0 )
296             )
297 #else
298         if  (   ( m_aNamedPipeRadio.IsChecked() )
299             &&  ( m_aNamedPipe.GetText().Len() == 0 )
300             )
301 #endif
302             return false;
303 
304         return true;
305     }
306 
307 //........................................................................
308 } // namespace dbaui
309 //........................................................................
310