Advanced Strategic Command
pwd_dlg.cpp
Go to the documentation of this file.
1 
5 /***************************************************************************
6  password_dialog.cpp - description
7  -------------------
8  begin : Mon Nov 27 2000
9  copyright : (C) 2000 by Martin Bickel
10  email : bickel@asc-hq.org
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 
22 #include <string>
23 
24 #include "../paradialog.h"
25 #include "../gameoptions.h"
26 #include "../password.h"
27 
28  class PasswordDialog : public ASC_PG_Dialog {
29  Password& password;
30  PG_LineEdit* line1;
31  PG_LineEdit* line2;
32 
33  bool ok()
34  {
35  if ( firstTime ) {
36  assert( line2 );
37  if ( line1->GetText() != line2->GetText() )
38  return false;
39  else {
40  password.setUnencoded( line1->GetText() );
41  success = true;
42  QuitModal();
43  return true;
44  }
45  } else {
46  Password p2;
47  p2.setUnencoded( line1->GetText() );
48  static bool dbg = true;
49  if ( p2 != password && dbg )
50  return false;
51  else {
52  success = true;
53  QuitModal();
54  return true;
55  }
56  }
57  }
58 
59  bool cancel()
60  {
61  QuitModal();
62  return true;
63  }
64 
65  bool def()
66  {
67  if ( CGameOptions::Instance()->getDefaultPassword().empty() ) {
68  warningMessage ( "no default password setup!" );
69  return false;
70  }
71 
73  success = true;
74  QuitModal();
75  return true;
76  }
77 
78  static const int border = 20;
79 
80  int buttonNum;
81  PG_Button* addButton( const ASCString& label, int totalNum )
82  {
83  int width = (Width() - (totalNum+1)*border) / totalNum;
84  return new PG_Button( this, PG_Rect( border + buttonNum++ * (width + border), Height()-40, width , 30 ), label );
85  }
86 
87  bool success;
88 
89  bool line1completed()
90  {
91  if ( firstTime && line2 )
92  line2->EditBegin();
93  else
94  ok();
95  return true;
96  }
97 
98  protected:
99  bool firstTime;
102  public:
103  PasswordDialog ( Password& crc, bool _firstTime, bool _cancelAllowed, bool _defaultAllowed, const ASCString& username ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 300, 190), "Enter Password"),
104  password ( crc ), buttonNum(0), success(false), firstTime ( _firstTime ), cancelAllowed ( _cancelAllowed ), defaultAllowed ( _defaultAllowed )
105  {
106 
107  if ( username.length() )
108  new PG_Label( this, PG_Rect( border, 25, Width() - 2 * border, 20 ), "Player: " + username );
109 
110  line1 = new PG_LineEdit( this, PG_Rect( border, 50, Width() - 2 * border, 20));
111  line1->SetPassHidden('*');
112  line1->sigEditReturn.connect( sigc::mem_fun( *this, &PasswordDialog::line1completed ));
113 
114  if ( firstTime ) {
115  line2 = new PG_LineEdit( this, PG_Rect( border, 80, Width() - 2 * border, 20));
116  line2->SetPassHidden('*');
117  line2->sigEditReturn.connect( sigc::mem_fun( *this, &PasswordDialog::ok ));
118  } else {
119  line2 = NULL;
120  }
121 
122  int bnum;
124  bnum = 3;
125  else
126  bnum = 2;
127 
128 
129  addButton ( "~O~k", bnum ) -> sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PasswordDialog::ok )));
130 
131  if ( firstTime && defaultAllowed ) {
132  addButton ( "~D~efault", bnum ) -> sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PasswordDialog::def )));
133  if ( cancelAllowed ) {
134  addButton ( "~C~ancel", bnum ) -> sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PasswordDialog::cancel )));
135  }
136  } else {
137  if ( cancelAllowed ) {
138  addButton ( "~C~ancel", bnum ) -> sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PasswordDialog::cancel )));
139  } else {
140  addButton ( "~A~bort", bnum ) -> sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PasswordDialog::cancel )));
141  }
142  }
143  };
144 
145  bool getSuccess()
146  {
147  return success;
148  }
149 
150  int RunModal()
151  {
152  line1->EditBegin();
153  return ASC_PG_Dialog::RunModal();
154  }
155  };
156 
157 
158 bool enterpassword ( Password& pwd, bool firstTime, bool cancelAllowed, bool defaultAllowed, const ASCString& username )
159 {
161 
162  if ( !pwd.empty() && !def.empty() && pwd==def && !firstTime )
163  return true;
164 
165  PasswordDialog pwod ( pwd, firstTime, cancelAllowed, defaultAllowed, username );
166  pwod.Show();
167  pwod.RunModal();
168  return pwod.getSuccess();
169 }
PasswordDialog(Password &crc, bool _firstTime, bool _cancelAllowed, bool _defaultAllowed, const ASCString &username)
Definition: pwd_dlg.cpp:103
bool firstTime
Definition: pwd_dlg.cpp:99
void warningMessage(const ASCString &str)
bool cancelAllowed
Definition: pwd_dlg.cpp:100
bool enterpassword(Password &pwd, bool firstTime, bool cancelAllowed, bool defaultAllowed, const ASCString &username)
Definition: pwd_dlg.cpp:158
void setUnencoded(const string &s)
Definition: password.cpp:27
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
bool empty() const
Definition: password.cpp:89
Password getDefaultPassword()
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
static CGameOptions * Instance()
returns the only Instance
Definition: gameoptions.cpp:38
bool defaultAllowed
Definition: pwd_dlg.cpp:101
int RunModal()
Definition: pwd_dlg.cpp:150
bool getSuccess()
Definition: pwd_dlg.cpp:145