File: pwd_dlg.cpp

package info (click to toggle)
asc 2.6.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 82,860 kB
  • ctags: 26,395
  • sloc: cpp: 158,660; sh: 11,274; ansic: 6,878; makefile: 604; perl: 138
file content (169 lines) | stat: -rw-r--r-- 6,695 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*! \file pwd_dlg.cpp
    \brief Dialog for entering and asking for passwords
*/

/***************************************************************************
                          password_dialog.cpp  -  description
                             -------------------
    begin                : Mon Nov 27 2000
    copyright            : (C) 2000 by Martin Bickel
    email                : bickel@asc-hq.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <string>

#include "../paradialog.h"
#include "../gameoptions.h"
#include "../password.h"

 class PasswordDialog : public ASC_PG_Dialog {
               Password& password;
               PG_LineEdit* line1;
               PG_LineEdit* line2;
               
               bool ok()
               {
                  if ( firstTime ) {
                     assert( line2 );
                     if ( line1->GetText() != line2->GetText() )
                        return false;
                     else {
                        password.setUnencoded( line1->GetText() );
                        success = true;
                        QuitModal();
                        return true;
                     }
                  } else {
                     Password p2;
                     p2.setUnencoded( line1->GetText() );
                     static bool dbg = true;
                     if ( p2 != password && dbg )
                        return false;
                     else {
                        success = true;
                        QuitModal();
                        return true;
                     }
                  }
               }
               
               bool cancel()
               {
                  QuitModal();
                  return true;
               }

               bool def()
               {
                  if ( CGameOptions::Instance()->getDefaultPassword().empty() ) {
                     warningMessage ( "no default password setup!" );
                     return false;
                  }

                  password = CGameOptions::Instance()->getDefaultPassword();
                  success = true;
                  QuitModal();
                  return true;
               }

               static const int border  = 20;

               int buttonNum;
               PG_Button* addButton( const ASCString& label, int totalNum )
               {
                  int width = (Width() - (totalNum+1)*border) / totalNum;
                  return new PG_Button( this, PG_Rect( border + buttonNum++ * (width + border), Height()-40, width , 30 ), label );
               }

               bool success;
               
               bool line1completed()
               {
                  if ( firstTime && line2 )
                     line2->EditBegin();
                  else
                     ok();
                  return true;
               }
               
            protected:
               bool firstTime;
               bool cancelAllowed;
               bool defaultAllowed;
            public:
               PasswordDialog ( Password& crc, bool _firstTime, bool _cancelAllowed, bool _defaultAllowed, const ASCString& username ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 300, 190), "Enter Password"),
                     password ( crc ), buttonNum(0), success(false), firstTime ( _firstTime ), cancelAllowed ( _cancelAllowed ), defaultAllowed ( _defaultAllowed )
               {
                  
                  if ( username.length() )
                     new PG_Label( this, PG_Rect( border, 25, Width() - 2 * border, 20 ), "Player: " + username );
                  
                  line1 = new PG_LineEdit( this, PG_Rect( border, 50, Width() - 2 * border, 20));
                  line1->SetPassHidden('*');
                  line1->sigEditReturn.connect( SigC::slot( *this, &PasswordDialog::line1completed ));

                  if ( firstTime ) {
                     line2 = new PG_LineEdit( this, PG_Rect( border, 80, Width() - 2 * border, 20));
                     line2->SetPassHidden('*');
                     line2->sigEditReturn.connect( SigC::slot( *this, &PasswordDialog::ok ));
                  } else {
                     line2 = NULL;
                  }

                  int bnum;
                  if ( firstTime && defaultAllowed && cancelAllowed )
                     bnum = 3;
                  else
                     bnum = 2;

                  
                  addButton ( "~O~k", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::ok ));
                  
                  if ( firstTime && defaultAllowed ) {
                     addButton ( "~D~efault", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::def ));
                     if ( cancelAllowed ) {
                        addButton ( "~C~ancel", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
                     }
                  } else {
                     if ( cancelAllowed ) {
                        addButton ( "~C~ancel", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
                     } else {
                        addButton ( "~A~bort", bnum ) -> sigClick.connect( SigC::slot( *this, &PasswordDialog::cancel ));
                     }
                  }
               };

               bool getSuccess()
               {
                  return success;
               }

               int RunModal()
               {
                  line1->EditBegin();
                  return ASC_PG_Dialog::RunModal();
               }
           };


bool enterpassword ( Password& pwd, bool firstTime, bool cancelAllowed, bool defaultAllowed, const ASCString& username )
{
   Password def = CGameOptions::Instance()->getDefaultPassword();

   if ( !pwd.empty() && !def.empty() && pwd==def && !firstTime )
      return true;

   PasswordDialog pwod ( pwd, firstTime, cancelAllowed, defaultAllowed, username );
   pwod.Show();
   pwod.RunModal();
   return pwod.getSuccess();
}