pwd_dlg.cpp

Go to the documentation of this file.
00001 
00005 /***************************************************************************
00006                           password_dialog.cpp  -  description
00007                              -------------------
00008     begin                : Mon Nov 27 2000
00009     copyright            : (C) 2000 by Martin Bickel
00010     email                : bickel@asc-hq.org
00011  ***************************************************************************/
00012 
00013 /***************************************************************************
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  ***************************************************************************/
00021 
00022 #include <string>
00023 
00024 #include "../paradialog.h"
00025 #include "../gameoptions.h"
00026 #include "../password.h"
00027 
00028  class PasswordDialog : public ASC_PG_Dialog {
00029                Password& password;
00030                PG_LineEdit* line1;
00031                PG_LineEdit* line2;
00032                
00033                bool ok()
00034                {
00035                   if ( firstTime ) {
00036                      assert( line2 );
00037                      if ( line1->GetText() != line2->GetText() )
00038                         return false;
00039                      else {
00040                         password.setUnencoded( line1->GetText() );
00041                         success = true;
00042                         QuitModal();
00043                         return true;
00044                      }
00045                   } else {
00046                      Password p2;
00047                      p2.setUnencoded( line1->GetText() );
00048                      static bool dbg = true;
00049                      if ( p2 != password && dbg )
00050                         return false;
00051                      else {
00052                         success = true;
00053                         QuitModal();
00054                         return true;
00055                      }
00056                   }
00057                }
00058                
00059                bool cancel()
00060                {
00061                   QuitModal();
00062                   return true;
00063                }
00064 
00065                bool def()
00066                {
00067                   if ( CGameOptions::Instance()->getDefaultPassword().empty() ) {
00068                      warningMessage ( "no default password setup!" );
00069                      return false;
00070                   }
00071 
00072                   password = CGameOptions::Instance()->getDefaultPassword();
00073                   success = true;
00074                   QuitModal();
00075                   return true;
00076                }
00077 
00078                static const int border  = 20;
00079 
00080                int buttonNum;
00081                PG_Button* addButton( const ASCString& label, int totalNum )
00082                {
00083                   int width = (Width() - (totalNum+1)*border) / totalNum;
00084                   return new PG_Button( this, PG_Rect( border + buttonNum++ * (width + border), Height()-40, width , 30 ), label );
00085                }
00086 
00087                bool success;
00088                
00089                bool line1completed()
00090                {
00091                   if ( firstTime && line2 )
00092                      line2->EditBegin();
00093                   else
00094                      ok();
00095                   return true;
00096                }
00097                
00098             protected:
00099                bool firstTime;
00100                bool cancelAllowed;
00101                bool defaultAllowed;
00102             public:
00103                PasswordDialog ( Password& crc, bool _firstTime, bool _cancelAllowed, bool _defaultAllowed, const ASCString& username ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 300, 190), "Enter Password"),
00104                      password ( crc ), buttonNum(0), success(false), firstTime ( _firstTime ), cancelAllowed ( _cancelAllowed ), defaultAllowed ( _defaultAllowed )
00105                {
00106                   
00107                   if ( username.length() )
00108                      new PG_Label( this, PG_Rect( border, 25, Width() - 2 * border, 20 ), "Player: " + username );
00109                   
00110                   line1 = new PG_LineEdit( this, PG_Rect( border, 50, Width() - 2 * border, 20));
00111                   line1->SetPassHidden('*');
00112                   line1->sigEditReturn.connect( SigC::slot( *this, &PasswordDialog::line1completed ));
00113 
00114                   if ( firstTime ) {
00115                      line2 = new PG_LineEdit( this, PG_Rect( border, 80, Width() - 2 * border, 20));
00116                      line2->SetPassHidden('*');
00117                      line2->sigEditReturn.connect( SigC::slot( *this, &PasswordDialog::ok ));
00118                   } else {
00119                      line2 = NULL;
00120                   }
00121 
00122                   int bnum;
00123                   if ( firstTime && defaultAllowed && cancelAllowed )
00124                      bnum = 3;
00125                   else
00126                      bnum = 2;
00127 
00128                   
00129                   addButton ( "~O~k", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::ok ));
00130                   
00131                   if ( firstTime && defaultAllowed ) {
00132                      addButton ( "~D~efault", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::def ));
00133                      if ( cancelAllowed ) {
00134                         addButton ( "~C~ancel", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
00135                      }
00136                   } else {
00137                      if ( cancelAllowed ) {
00138                         addButton ( "~C~ancel", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
00139                      } else {
00140                         addButton ( "~A~bort", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
00141                      }
00142                   }
00143                };
00144 
00145                bool getSuccess()
00146                {
00147                   return success;
00148                }
00149 
00150                int RunModal()
00151                {
00152                   line1->EditBegin();
00153                   return ASC_PG_Dialog::RunModal();
00154                }
00155            };
00156 
00157 
00158 bool enterpassword ( Password& pwd, bool firstTime, bool cancelAllowed, bool defaultAllowed, const ASCString& username )
00159 {
00160    Password def = CGameOptions::Instance()->getDefaultPassword();
00161 
00162    if ( !pwd.empty() && !def.empty() && pwd==def && !firstTime )
00163       return true;
00164 
00165    PasswordDialog pwod ( pwd, firstTime, cancelAllowed, defaultAllowed, username );
00166    pwod.Show();
00167    pwod.RunModal();
00168    return pwod.getSuccess();
00169 }

Generated on Mon May 21 01:26:36 2012 for Advanced Strategic Command by  doxygen 1.5.1