Fix header inclusions
[amule.git] / src / utils / wxCas / src / wxcasprefs.cpp
blobcd7a9ddb1b9b03fa965137f810c18ffff4bbb7c4
1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 /// Name: wxCasPrefs Class
3 ///
4 /// Purpose: Display user preferences dialog and manage configuration storage system
5 ///
6 /// Author: ThePolish <thepolish@vipmail.ru>
7 ///
8 /// Copyright (c) 2004-2011 ThePolish ( thepolish@vipmail.ru )
9 ///
10 /// Derived from CAS by Pedro de Oliveira <falso@rdk.homeip.net>
11 ///
12 /// Pixmaps from aMule http://www.amule.org
13 ///
14 /// This program is free software; you can redistribute it and/or modify
15 /// it under the terms of the GNU General Public License as published by
16 /// the Free Software Foundation; either version 2 of the License, or
17 /// (at your option) any later version.
18 ///
19 /// This program is distributed in the hope that it will be useful,
20 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
21 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 /// GNU General Public License for more details.
23 ///
24 /// You should have received a copy of the GNU General Public License
25 /// along with this program; if not, write to the
26 /// Free Software Foundation, Inc.,
27 /// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
31 #include <wx/config.h>
32 #include <wx/dirdlg.h>
34 #include "wxcasprefs.h"
35 #include "wxcascte.h"
36 #include "wxcasframe.h"
38 // Constructor
39 WxCasPrefs::WxCasPrefs ( wxWindow * parent ) : wxDialog ( parent, -1,
40 wxString ( _
41 ( "Preferences" ) ) )
43 // Prefs
44 wxConfigBase * prefs = wxConfigBase::Get();
46 // Main vertical Sizer
47 m_mainVBox = new wxBoxSizer ( wxVERTICAL );
49 // OS Path
50 m_osPathSBox =
51 new wxStaticBox ( this, -1, _( "Directory containing amulesig.dat file" ) );
52 m_osPathSBoxSizer = new wxStaticBoxSizer ( m_osPathSBox, wxHORIZONTAL );
54 m_osPathTextCtrl = new wxTextCtrl ( this, -1, wxEmptyString );
55 m_osPathBrowseButton =
56 new wxButton ( this, ID_OSPATH_BROWSE_BUTTON, wxString ( _( "Browse" ) ) );
58 wxString str;
60 prefs->Read ( WxCasCte::AMULESIG_PATH_KEY, &str,
61 WxCasCte::DEFAULT_AMULESIG_PATH );
63 // Text extent
64 wxInt32 charExtent, y;
65 m_osPathTextCtrl->GetTextExtent ( wxT( "8" ), &charExtent, &y );
66 m_osPathTextCtrl->SetSize ( wxSize ( charExtent * ( str.Length () + 1 ), -1 ) );
68 m_osPathTextCtrl->SetValue ( str );
69 m_osPathTextCtrl->
70 SetToolTip ( _
71 ( "Enter here the directory where your amulesig.dat file is" ) );
73 m_osPathSBoxSizer->Add ( m_osPathTextCtrl, 1, wxALL | wxALIGN_CENTER, 5 );
74 m_osPathSBoxSizer->Add ( m_osPathBrowseButton, 0, wxALL | wxALIGN_CENTER, 5 );
76 m_mainVBox->Add ( m_osPathSBoxSizer, 0, wxGROW | wxALIGN_CENTER | wxALL, 10 );
78 // Refresh rate
79 m_refreshSBox = new wxStaticBox ( this, -1, wxEmptyString );
80 m_refreshSBoxSizer = new wxStaticBoxSizer ( m_refreshSBox, wxHORIZONTAL );
83 m_refreshSpinButton = new wxSpinCtrl ( this, -1 );
84 m_refreshSpinButton->SetRange ( WxCasCte::MIN_REFRESH_RATE,
85 WxCasCte::MAX_REFRESH_RATE );
86 m_refreshSpinButton->SetValue ( prefs->
87 Read ( WxCasCte::REFRESH_RATE_KEY,
88 WxCasCte::DEFAULT_REFRESH_RATE ) );
89 m_refreshStaticText =
90 new wxStaticText ( this, -1, _( "Refresh rate interval in seconds" ), wxDefaultPosition,
91 wxDefaultSize, wxALIGN_CENTRE );
92 m_refreshSBoxSizer->Add ( m_refreshSpinButton, 0, wxALL | wxALIGN_CENTER, 5 );
93 m_refreshSBoxSizer->Add ( m_refreshStaticText, 1, wxALL | wxALIGN_CENTER, 5 );
95 m_mainVBox->Add ( m_refreshSBoxSizer, 0, wxGROW | wxALIGN_CENTER | wxALL,
96 10 );
98 // Auto generate stat image
99 m_autoStatImgSBox = new wxStaticBox ( this, -1, wxEmptyString );
100 m_autoStatImgSBoxSizer =
101 new wxStaticBoxSizer ( m_autoStatImgSBox, wxVERTICAL );
103 m_autoStatImgCheck =
104 new wxCheckBox ( this, ID_AUTOSTATIMG_CHECK,
106 ( "Generate a stat image at every refresh event" ) );
107 m_autoStatImgSBoxSizer->Add ( m_autoStatImgCheck, 0,
108 wxGROW | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
110 m_autoStatImgHBoxSizer = new wxBoxSizer ( wxHORIZONTAL );
112 wxString strs[] =
114 wxT ( "PNG" ), wxT ( "JPG" ), wxT ( "BMP" ) };
116 m_autoStatImgCombo =
117 new wxComboBox ( this, ID_AUTOSTATIMG_COMBO,
118 prefs->
119 Read ( WxCasCte::AUTOSTATIMG_TYPE_KEY,
120 WxCasCte::DEFAULT_AUTOSTATIMG_TYPE ),
121 wxDefaultPosition, wxDefaultSize, 3, strs,
122 wxCB_DROPDOWN | wxCB_READONLY );
124 m_autoStatImgTextCtrl = new wxTextCtrl ( this, -1, wxEmptyString );
125 m_autoStatImgTextCtrl->SetValue ( prefs->
126 Read ( WxCasCte::AUTOSTATIMG_DIR_KEY,
127 WxCasCte::DEFAULT_AUTOSTATIMG_PATH ) );
128 m_autoStatImgTextCtrl->
129 SetToolTip ( _
130 ( "Enter here the directory where you want to generate the statistic image" ) );
132 m_autoStatImgButton =
133 new wxButton ( this, ID_AUTOSTATIMG_BROWSE_BUTTON, wxString ( _( "Browse" ) ) );
135 m_autoStatImgHBoxSizer->Add ( m_autoStatImgCombo, 0, wxALIGN_CENTER | wxALL,
136 5 );
137 m_autoStatImgHBoxSizer->Add ( m_autoStatImgTextCtrl, 1,
138 wxALIGN_CENTER | wxALL, 5 );
139 m_autoStatImgHBoxSizer->Add ( m_autoStatImgButton, 0, wxALIGN_CENTER | wxALL,
140 5 );
142 m_autoStatImgSBoxSizer->Add ( m_autoStatImgHBoxSizer, 0,
143 wxGROW | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
145 m_mainVBox->Add ( m_autoStatImgSBoxSizer, 0, wxGROW | wxALIGN_CENTER | wxALL,
146 5 );
148 // Auto FTP update stat image
149 m_ftpUpdateSBox = new wxStaticBox ( this, -1, wxEmptyString );
150 m_ftpUpdateSBoxSizer =
151 new wxStaticBoxSizer ( m_ftpUpdateSBox, wxVERTICAL );
153 // Check
154 m_ftpUpdateCheck =
155 new wxCheckBox ( this, ID_FTP_UPDATE_CHECK,
157 ( "Upload periodicaly your stat image to FTP server" ) );
158 m_ftpUpdateSBoxSizer->Add ( m_ftpUpdateCheck, 0,
159 wxGROW | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
161 // Grid size
162 m_ftpUpdateGridSizer = new wxGridSizer( 2 );
164 // FTP Static text
165 m_ftpUrlStaticText = new wxStaticText ( this, -1, _( "FTP Url" ) );
166 m_ftpUpdateGridSizer->Add ( m_ftpUrlStaticText, 1,
167 wxALIGN_LEFT | wxALIGN_BOTTOM | wxALL, 5 );
169 m_ftpPathStaticText = new wxStaticText ( this, -1, _( "FTP Path" ) );
170 m_ftpUpdateGridSizer->Add ( m_ftpPathStaticText, 1,
171 wxALIGN_LEFT | wxALIGN_BOTTOM | wxALL, 5 );
172 // Url
173 m_ftpUrlTextCtrl = new wxTextCtrl ( this, -1, wxEmptyString );
174 m_ftpUrlTextCtrl->SetValue ( prefs->
175 Read ( WxCasCte::FTP_URL_KEY,
176 WxCasCte::DEFAULT_FTP_URL ) );
177 m_ftpUrlTextCtrl->
178 SetToolTip ( _
179 ( "Enter here the URL of your FTP server" ) );
181 m_ftpUpdateGridSizer->Add ( m_ftpUrlTextCtrl, 1,
182 wxGROW | wxALIGN_LEFT | wxALIGN_TOP | wxALL, 5 );
184 // Path
185 m_ftpPathTextCtrl = new wxTextCtrl ( this, -1, wxEmptyString );
186 m_ftpPathTextCtrl->SetValue ( prefs->
187 Read ( WxCasCte::FTP_PATH_KEY,
188 WxCasCte::DEFAULT_FTP_PATH ) );
189 m_ftpPathTextCtrl->
190 SetToolTip ( _
191 ( "Enter here the directory where putting your stat image on FTP server" ) );
193 m_ftpUpdateGridSizer->Add ( m_ftpPathTextCtrl, 1,
194 wxGROW | wxALIGN_LEFT | wxALIGN_TOP | wxALL, 5 );
196 // Login Static text
197 m_ftpUserStaticText = new wxStaticText ( this, -1, _( "User" ) );
198 m_ftpUpdateGridSizer->Add ( m_ftpUserStaticText, 1,
199 wxALIGN_LEFT | wxALIGN_BOTTOM | wxALL, 5 );
201 m_ftpPasswdStaticText = new wxStaticText ( this, -1, _( "Password" ) );
202 m_ftpUpdateGridSizer->Add ( m_ftpPasswdStaticText, 1,
203 wxALIGN_LEFT | wxALIGN_BOTTOM | wxALL, 5 );
205 // User
206 m_ftpUserTextCtrl = new wxTextCtrl ( this, -1, wxEmptyString );
207 m_ftpUserTextCtrl->SetValue ( prefs->
208 Read ( WxCasCte::FTP_USER_KEY,
209 WxCasCte::DEFAULT_FTP_USER ) );
210 m_ftpUserTextCtrl->
211 SetToolTip ( _
212 ( "Enter here the User name to log into your FTP server" ) );
214 m_ftpUpdateGridSizer->Add ( m_ftpUserTextCtrl, 1,
215 wxGROW | wxALIGN_LEFT | wxALIGN_TOP | wxALL, 5 );
217 // Passwd
218 m_ftpPasswdTextCtrl = new wxTextCtrl ( this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD );
219 m_ftpPasswdTextCtrl->SetValue ( prefs->
220 Read ( WxCasCte::FTP_PASSWD_KEY,
221 WxCasCte::DEFAULT_FTP_PASSWD ) );
222 m_ftpPasswdTextCtrl->
223 SetToolTip ( _
224 ( "Enter here the User password to log into your FTP server" ) );
226 m_ftpUpdateGridSizer->Add ( m_ftpPasswdTextCtrl, 1,
227 wxGROW | wxALIGN_LEFT | wxALIGN_TOP | wxALL, 5 );
230 // Add to static sizer
231 m_ftpUpdateSBoxSizer->Add ( m_ftpUpdateGridSizer, 1,
232 wxGROW | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
234 // Upload rate
235 m_ftpRateHBoxSizer = new wxBoxSizer( wxHORIZONTAL );
236 m_ftpUpdateSpinButton = new wxSpinCtrl ( this, -1 );
237 m_ftpUpdateSpinButton->SetRange ( WxCasCte::MIN_FTP_RATE,
238 WxCasCte::MAX_FTP_RATE );
239 m_ftpUpdateSpinButton->SetValue ( prefs->
240 Read ( WxCasCte::FTP_UPDATE_RATE_KEY,
241 WxCasCte::DEFAULT_FTP_UPDATE_RATE ) );
242 m_ftpUpdateStaticText =
243 new wxStaticText ( this, -1, _( "FTP update rate interval in minutes" ), wxDefaultPosition,
244 wxDefaultSize, wxALIGN_CENTRE );
245 m_ftpRateHBoxSizer->Add ( m_ftpUpdateSpinButton, 0, wxALL | wxALIGN_CENTER, 5 );
246 m_ftpRateHBoxSizer->Add ( m_ftpUpdateStaticText, 1, wxALL | wxALIGN_CENTER, 5 );
248 m_ftpUpdateSBoxSizer->Add ( m_ftpRateHBoxSizer, 0,
249 wxGROW | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
251 // Add to main sizer
252 m_mainVBox->Add ( m_ftpUpdateSBoxSizer, 0, wxGROW | wxALIGN_CENTER | wxALL,
253 5 );
255 // Mask auto stat img disabled controls
256 if (prefs->Read( WxCasCte::ENABLE_AUTOSTATIMG_KEY,
257 WxCasCte::DEFAULT_AUTOSTATIMG_ISENABLED ) ) {
258 m_autoStatImgCheck->SetValue( TRUE );
259 } else {
260 m_autoStatImgCheck->SetValue ( FALSE );
261 EnableAutoStatImgCtrls( FALSE );
263 m_ftpUpdateCheck->Enable ( FALSE );
264 EnableFtpUpdateCtrls( FALSE );
267 // Mask Ftp update disabled controls
268 if (prefs->Read( WxCasCte::ENABLE_FTP_UPDATE_KEY,
269 WxCasCte::DEFAULT_FTP_UPDATE_ISENABLED ) ) {
270 m_ftpUpdateCheck->SetValue( TRUE );
271 } else {
272 m_ftpUpdateCheck->SetValue ( FALSE );
273 EnableFtpUpdateCtrls( FALSE );
276 // Separator line
277 m_staticLine = new wxStaticLine ( this, -1 );
278 m_mainVBox->Add ( m_staticLine, 0, wxGROW | wxALIGN_CENTER | wxALL );
280 // Button bar
281 m_buttonHBox = new wxBoxSizer ( wxHORIZONTAL );
282 m_validateButton =
283 new wxButton ( this, ID_VALIDATE_BUTTON, wxString ( _( "Validate" ) ) );
284 m_cancelButton =
285 new wxButton ( this, wxID_CANCEL, wxString ( _( "Cancel" ) ) );
287 m_buttonHBox->Add ( m_validateButton, 0, wxALIGN_CENTER | wxALL, 5 );
288 m_buttonHBox->Add ( m_cancelButton, 0, wxALIGN_CENTER | wxALL, 5 );
290 m_mainVBox->Add ( m_buttonHBox, 0, wxALIGN_CENTER | wxALL, 10 );
292 // Layout
293 SetAutoLayout ( TRUE );
294 SetSizerAndFit ( m_mainVBox );
296 m_validateButton->SetFocus ();
297 m_validateButton->SetDefault ();
300 // Destructor
301 WxCasPrefs::~WxCasPrefs ()
304 // Events table
305 BEGIN_EVENT_TABLE ( WxCasPrefs, wxDialog )
306 EVT_BUTTON ( ID_OSPATH_BROWSE_BUTTON, WxCasPrefs::OnOSPathBrowseButton )
307 EVT_BUTTON ( ID_AUTOSTATIMG_BROWSE_BUTTON, WxCasPrefs::OnAutoStatImgBrowseButton )
308 EVT_BUTTON ( ID_VALIDATE_BUTTON, WxCasPrefs::OnValidateButton )
309 EVT_CHECKBOX ( ID_AUTOSTATIMG_CHECK, WxCasPrefs::OnAutoStatImgCheck )
310 EVT_CHECKBOX ( ID_FTP_UPDATE_CHECK, WxCasPrefs::OnFtpUpdateCheck )
311 END_EVENT_TABLE ()
313 // Browse for OS Path
314 void WxCasPrefs::OnOSPathBrowseButton ( wxCommandEvent& WXUNUSED( event ) )
316 const wxString &dir = wxDirSelector(_
317 ("Folder containing your signature file"),
318 WxCasCte::DEFAULT_AMULESIG_PATH,
319 wxDD_DEFAULT_STYLE,
320 wxDefaultPosition, this);
321 if ( !dir.empty () ) {
322 m_osPathTextCtrl->SetValue ( dir );
326 // Browse for stat image Path
327 void
328 WxCasPrefs::OnAutoStatImgBrowseButton ( wxCommandEvent& WXUNUSED( event ) )
330 const wxString & dir = wxDirSelector(
331 _("Folder where generating the statistic image"),
332 WxCasCte::DEFAULT_AUTOSTATIMG_PATH,
333 wxDD_DEFAULT_STYLE,
334 wxDefaultPosition, this);
336 if ( !dir.empty () ) {
337 m_autoStatImgTextCtrl->SetValue ( dir );
341 // Auto Generate Stat Image Check Button
342 void
343 WxCasPrefs::OnAutoStatImgCheck ( wxCommandEvent& WXUNUSED( event ) )
345 if ( m_autoStatImgCheck->GetValue () ) {
346 EnableAutoStatImgCtrls( TRUE );
348 m_ftpUpdateCheck->Enable ( TRUE );
349 if ( m_ftpUpdateCheck->GetValue () ) {
350 EnableFtpUpdateCtrls( TRUE );
352 } else {
353 EnableAutoStatImgCtrls( FALSE );
355 m_ftpUpdateCheck->Enable ( FALSE );
356 EnableFtpUpdateCtrls( FALSE );
360 // Ftp update Check Button
361 void
362 WxCasPrefs::OnFtpUpdateCheck ( wxCommandEvent& WXUNUSED( event ) )
364 if ( m_ftpUpdateCheck->GetValue () ) {
365 EnableFtpUpdateCtrls( TRUE );
366 } else {
367 EnableFtpUpdateCtrls( FALSE );
370 // Validate Prefs
371 void
372 WxCasPrefs::OnValidateButton ( wxCommandEvent& WXUNUSED( event ) )
374 // Prefs
375 wxConfigBase * prefs = wxConfigBase::Get();
377 // Write amulesig dir
378 if ( prefs->Read ( WxCasCte::AMULESIG_PATH_KEY,
379 WxCasCte::DEFAULT_AMULESIG_PATH ) !=
380 m_osPathTextCtrl->GetValue () ) {
381 // Reload amulesig.dat
382 wxFileName amulesig( m_osPathTextCtrl->GetValue (),
383 WxCasCte::AMULESIG_FILENAME );
384 ( ( WxCasFrame* ) GetParent() ) ->SetAmuleSigFile( amulesig );
386 prefs->Write ( WxCasCte::AMULESIG_PATH_KEY,
387 m_osPathTextCtrl->GetValue () );
390 // Restart timer if refresh interval has changed
391 if ( prefs->Read ( WxCasCte::REFRESH_RATE_KEY, WxCasCte::DEFAULT_REFRESH_RATE ) !=
392 m_refreshSpinButton->GetValue () ) {
393 ( ( WxCasFrame* ) GetParent() ) ->ChangeRefreshPeriod( 1000 * m_refreshSpinButton->GetValue () );
395 // Write refresh interval
396 prefs->Write ( WxCasCte::REFRESH_RATE_KEY,
397 m_refreshSpinButton->GetValue () );
400 // Write auto stat img state
401 prefs->Write ( WxCasCte::ENABLE_AUTOSTATIMG_KEY,
402 m_autoStatImgCheck->GetValue () );
404 // If auto stat img is enabled
405 if ( m_autoStatImgCheck->GetValue () ) {
406 prefs->Write ( WxCasCte::AUTOSTATIMG_DIR_KEY,
407 m_autoStatImgTextCtrl->GetValue () );
409 prefs->Write ( WxCasCte::AUTOSTATIMG_TYPE_KEY,
410 m_autoStatImgCombo->GetValue () );
412 // Write Ftp update state
413 prefs->Write ( WxCasCte::ENABLE_FTP_UPDATE_KEY,
414 m_ftpUpdateCheck->GetValue () );
416 // If Ftp update is enabled
417 if ( m_ftpUpdateCheck->GetValue () ) {
418 // Restart timer if update interval has changed
419 if ( prefs->Read ( WxCasCte::FTP_UPDATE_RATE_KEY, WxCasCte::DEFAULT_FTP_UPDATE_RATE ) !=
420 m_ftpUpdateSpinButton->GetValue () ) {
421 ( ( WxCasFrame* ) GetParent() ) ->ChangeFtpUpdatePeriod( 60000 * m_refreshSpinButton->GetValue () );
422 prefs->Write ( WxCasCte::FTP_UPDATE_RATE_KEY,
423 m_ftpUpdateSpinButton->GetValue () );
425 // Write Ftp parameters
426 prefs->Write ( WxCasCte::FTP_URL_KEY,
427 m_ftpUrlTextCtrl->GetValue () );
429 prefs->Write ( WxCasCte::FTP_PATH_KEY,
430 m_ftpPathTextCtrl->GetValue () );
432 prefs->Write ( WxCasCte::FTP_USER_KEY,
433 m_ftpUserTextCtrl->GetValue () );
435 prefs->Write ( WxCasCte::FTP_PASSWD_KEY,
436 m_ftpPasswdTextCtrl->GetValue () );
440 // Force config writing
441 prefs->Flush();
443 // Close window
444 this->EndModal ( this->GetReturnCode () );
447 // Enable/Disable auto img ctrls
448 void
449 WxCasPrefs::EnableAutoStatImgCtrls( bool state )
451 m_autoStatImgTextCtrl->Enable ( state );
452 m_autoStatImgButton->Enable ( state );
453 m_autoStatImgCombo->Enable ( state );
456 // Enable/Disable Ftp update ctrls
457 void
458 WxCasPrefs::EnableFtpUpdateCtrls( bool state )
460 m_ftpUpdateSpinButton->Enable ( state );
461 m_ftpUpdateStaticText->Enable ( state );
462 m_ftpUrlTextCtrl->Enable ( state );
463 m_ftpUrlStaticText->Enable ( state );
464 m_ftpPathTextCtrl->Enable ( state );
465 m_ftpPathStaticText->Enable ( state );
466 m_ftpUserTextCtrl->Enable ( state );
467 m_ftpUserStaticText->Enable ( state );
468 m_ftpPasswdTextCtrl->Enable ( state );
469 m_ftpPasswdStaticText->Enable ( state );
471 // File_checked_for_headers