Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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                      warning ( "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 ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 300, 180), "Enter Password"),
00104                      password ( crc ), buttonNum(0), success(false), firstTime ( _firstTime ), cancelAllowed ( _cancelAllowed ), defaultAllowed ( _defaultAllowed )
00105                {
00106                   line1 = new PG_LineEdit( this, PG_Rect( border, 40, Width() - 2 * border, 20));
00107                   line1->SetPassHidden('*');
00108                   line1->sigEditReturn.connect( SigC::slot( *this, &PasswordDialog::line1completed ));
00109 
00110                   if ( firstTime ) {
00111                      line2 = new PG_LineEdit( this, PG_Rect( border, 70, Width() - 2 * border, 20));
00112                      line2->SetPassHidden('*');
00113                      line2->sigEditReturn.connect( SigC::slot( *this, &PasswordDialog::ok ));
00114                   } else {
00115                      line2 = NULL;
00116                   }
00117 
00118                   int bnum;
00119                   if ( firstTime && defaultAllowed && cancelAllowed )
00120                      bnum = 3;
00121                   else
00122                      bnum = 2;
00123 
00124                   
00125                   addButton ( "~O~k", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::ok ));
00126                   
00127                   if ( firstTime && defaultAllowed ) {
00128                      addButton ( "~D~efault", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::def ));
00129                      if ( cancelAllowed ) {
00130                         addButton ( "~C~ancel", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
00131                      }
00132                   } else {
00133                      if ( cancelAllowed ) {
00134                         addButton ( "~C~ancel", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
00135                      } else {
00136                         addButton ( "~A~bort", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
00137                      }
00138                   }
00139                };
00140 
00141                bool getSuccess()
00142                {
00143                   return success;
00144                }
00145 
00146                int RunModal()
00147                {
00148                   line1->EditBegin();
00149                   return ASC_PG_Dialog::RunModal();
00150                }
00151            };
00152 
00153 
00154 bool enterpassword ( Password& pwd, bool firstTime, bool cancelAllowed, bool defaultAllowed )
00155 {
00156    Password def = CGameOptions::Instance()->getDefaultPassword();
00157 
00158    if ( !pwd.empty() && !def.empty() && pwd==def && !firstTime )
00159       return true;
00160 
00161    PasswordDialog pwod ( pwd, firstTime, cancelAllowed, defaultAllowed );
00162    pwod.Show();
00163    pwod.RunModal();
00164    return pwod.getSuccess();
00165 }

Generated on Tue Jun 24 01:27:50 2008 for Advanced Strategic Command by  doxygen 1.4.2