]> code.citadel.org Git - citadel.git/blob - daphne/userlogin.cpp
* added message subject to all those tiny messages
[citadel.git] / daphne / userlogin.cpp
1 // ============================================================================
2 // declarations
3 // ============================================================================
4
5 #include "includes.hpp"
6
7 // ----------------------------------------------------------------------------
8 // private classes
9 // ----------------------------------------------------------------------------
10
11 // userlogin is the frame for logging in.
12
13 // ----------------------------------------------------------------------------
14 // constants
15 // ----------------------------------------------------------------------------
16
17 // IDs for the controls and the menu commands
18 enum
19 {
20         BUTTON_LOGIN,
21         BUTTON_NEWUSER,
22         BUTTON_EXIT
23 };
24
25 // ----------------------------------------------------------------------------
26 // event tables and other macros for wxWindows
27 // ----------------------------------------------------------------------------
28
29 // the event tables connect the wxWindows events with the functions (event
30 // handlers) which process them. It can be also done at run-time, but for the
31 // simple menu events like this the static method is much simpler.
32 BEGIN_EVENT_TABLE(      UserLogin, wxMDIChildFrame)
33         EVT_BUTTON(     BUTTON_LOGIN,           UserLogin::OnButtonPressed)
34         EVT_BUTTON(     BUTTON_NEWUSER,         UserLogin::OnButtonPressed)
35         EVT_BUTTON(     BUTTON_EXIT,            UserLogin::OnButtonPressed)
36 END_EVENT_TABLE()
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42
43 // ----------------------------------------------------------------------------
44 // the application class
45 // ----------------------------------------------------------------------------
46
47 // frame constructor
48 UserLogin::UserLogin(CitClient *sock, wxMDIParentFrame *MyMDI)
49        : wxMDIChildFrame(MyMDI, //parent
50                         -1,     //window id
51                         " Please log in ",
52                         wxDefaultPosition,
53                         wxDefaultSize,
54                         wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL,
55                         "UserLogin"
56                         ) {
57
58         wxString sendcmd, recvcmd, xferbuf, buf;
59         wxPanel *banner;
60
61         citsock = sock;
62         citMyMDI = MyMDI;
63
64         // set the frame icon
65         /* SetIcon(wxICON(mondrian)); */
66
67         wxStaticText *username_label = new wxStaticText(
68                 this, -1, "User name:",
69                 wxDefaultPosition, wxDefaultSize, 0, ""
70                 );
71
72         username = new wxTextCtrl(
73                 this,
74                 -1,
75                 "",
76                 wxPoint(10,10),
77                 wxSize(300,30),
78                 0, // no style
79                 wxDefaultValidator,
80                 "sendcmd"
81                 );
82
83         wxStaticText *password_label = new wxStaticText(
84                 this, -1, "Password:",
85                 wxDefaultPosition, wxDefaultSize, 0, ""
86                 );
87
88         password = new wxTextCtrl(
89                 this,
90                 -1,
91                 "",
92                 wxPoint(10,100),
93                 wxSize(300,30),
94                 wxTE_PASSWORD,
95                 wxDefaultValidator,
96                 "sendcmd"
97                 );
98
99         login_button = new wxButton(
100                 this,
101                 BUTTON_LOGIN,
102                 "Login",
103                 wxPoint(100,100),
104                 wxSize(100,30),
105                 0L,
106                 wxDefaultValidator,
107                 "login_button"
108                 );
109         login_button->SetDefault();
110
111         newuser_button = new wxButton(
112                 this,
113                 BUTTON_NEWUSER,
114                 "New user",
115                 wxPoint(200,200),
116                 wxSize(100,30),
117                 0L,
118                 wxDefaultValidator,
119                 "newuser_button"
120                 );
121
122         exit_button = new wxButton(
123                 this,
124                 BUTTON_EXIT,
125                 "Close",
126                 wxPoint(300,300),
127                 wxSize(100,30),
128                 0L,
129                 wxDefaultValidator,
130                 "exit_button"
131                 ); 
132
133         wxHtmlWindow *hello = new wxHtmlWindow(this);
134
135         // Set up a panel for the title...
136         banner = new wxPanel(this, -1);
137         banner->SetBackgroundColour(wxColour(0x00, 0x00, 0x77));
138         banner->SetForegroundColour(wxColour(0xFF, 0xFF, 0x00));
139
140         wxLayoutConstraints *b1 = new wxLayoutConstraints;
141         b1->top.SameAs(this, wxTop, 2);
142         b1->left.SameAs(this, wxLeft, 2);
143         b1->right.SameAs(this, wxRight, 2);
144         b1->height.PercentOf(this, wxHeight, 10);
145         banner->SetConstraints(b1);
146
147         wxStaticText *rname = new wxStaticText(banner, -1, citsock->HumanNode);
148         rname->SetFont(wxFont(16, wxDEFAULT, wxNORMAL, wxNORMAL));
149         rname->SetForegroundColour(wxColour(0xFF, 0xFF, 0x00));
150         wxLayoutConstraints *t2 = new wxLayoutConstraints;
151         t2->top.SameAs(banner, wxTop, 1);
152         t2->centreX.SameAs(banner, wxCentreX);
153         t2->width.SameAs(banner, wxWidth);
154         t2->height.AsIs();
155         rname->SetConstraints(t2);
156
157         wxLayoutConstraints *h0 = new wxLayoutConstraints;
158         h0->top.Below(banner, 10);
159         h0->bottom.Above(username, -10);
160         h0->left.SameAs(this, wxLeft, 10);
161         h0->right.SameAs(this, wxRight, 10);
162         hello->SetConstraints(h0);
163
164         wxLayoutConstraints *c1 = new wxLayoutConstraints;
165         c1->bottom.SameAs(this, wxBottom, 10);          // 10 from the bottom
166         c1->centreX.SameAs(this, wxCentreX);            // in the middle
167         c1->height.AsIs(); c1->width.AsIs();
168         newuser_button->SetConstraints(c1);
169
170         wxLayoutConstraints *c2 = new wxLayoutConstraints;
171         c2->bottom.SameAs(newuser_button, wxBottom);
172         c2->right.LeftOf(newuser_button, 10);           // 10 from middle btn
173         c2->height.AsIs(); c2->width.AsIs();
174         login_button->SetConstraints(c2);
175
176         wxLayoutConstraints *c3 = new wxLayoutConstraints;
177         c3->bottom.SameAs(newuser_button, wxBottom);
178         c3->left.RightOf(newuser_button, 10);           // 10 from middle btn
179         c3->height.AsIs(); c3->width.AsIs();
180         exit_button->SetConstraints(c3);
181
182         wxLayoutConstraints *c6 = new wxLayoutConstraints;
183         c6->left.SameAs(this, wxLeft, 10);
184         c6->bottom.SameAs(password, wxBottom);
185         c6->width.AsIs(); c6->height.AsIs();
186         password_label->SetConstraints(c6);
187
188         wxLayoutConstraints *c7 = new wxLayoutConstraints;
189         c7->left.SameAs(this, wxLeft, 10);
190         c7->bottom.SameAs(username, wxBottom);
191         c7->width.AsIs(); c7->height.AsIs();
192         username_label->SetConstraints(c7);
193
194         wxLayoutConstraints *c4 = new wxLayoutConstraints;
195         c4->bottom.Above(newuser_button, -10);
196         c4->left.RightOf(username_label, 10);
197         c4->height.AsIs();
198         c4->right.SameAs(this, wxRight, 10);
199         password->SetConstraints(c4);
200
201         wxLayoutConstraints *c5 = new wxLayoutConstraints;
202         c5->bottom.Above(password, -10);
203         c5->left.SameAs(password, wxLeft);
204         c5->height.AsIs();
205         c5->width.SameAs(password, wxWidth);
206         username->SetConstraints(c5);
207
208         SetAutoLayout(TRUE);
209         Maximize(TRUE);
210         Show(TRUE);
211
212         sendcmd = "MESG hello";
213         if (citsock->serv_trans(sendcmd, recvcmd, xferbuf)==1) {
214                 variformat_to_html(buf, xferbuf, FALSE); 
215                 buf = "<HTML><font size=-1><BODY>"
216                         + buf + "</BODY></HTML></font>\n"; 
217                 hello->SetPage(buf); 
218         }
219
220         username->SetFocus();
221 }
222
223
224
225 void UserLogin::OnButtonPressed(wxCommandEvent& whichbutton) {
226         wxString sendbuf;
227         wxString recvbuf;
228         int r;
229
230         if (whichbutton.GetId() == BUTTON_EXIT) {
231                 if (citadel->IsConnected()) {
232                 sendbuf = "QUIT";
233                 citsock->serv_trans(sendbuf);
234                 BigMDI->SetStatusText("Not connected");
235                 delete this; 
236               } else {
237                 BigMDI->SetStatusText("Not connected");
238                 delete this; }
239
240         }
241
242         if (whichbutton.GetId() == BUTTON_LOGIN) {
243                 sendbuf = "USER " + username->GetValue();
244                 r = citsock->serv_trans(sendbuf, recvbuf);
245                 if (r != 3) {
246                         wxMessageDialog nouser(this,
247                                 recvbuf.Mid(4),
248                                 "Error",
249                                 wxOK | wxCENTRE | wxICON_INFORMATION,
250                                 wxDefaultPosition);
251                         nouser.ShowModal();
252                 } else {
253                         sendbuf = "PASS ";
254                         sendbuf += password->GetValue();
255                         r = citsock->serv_trans(sendbuf, recvbuf);
256                         if (r != 2) {
257                                 wxMessageDialog nopass(this,
258                                         recvbuf.Mid(4),
259                                         "Error",
260                                         wxOK | wxCENTRE | wxICON_INFORMATION,
261                                         wxDefaultPosition);
262                                 nopass.ShowModal();
263                         } else {
264                                 BeginSession(recvbuf);
265                                 citsock->curr_pass = password->GetValue();
266                                 delete this;    // dismiss the login box
267                         }
268                 }
269         }
270
271         if (whichbutton.GetId() == BUTTON_NEWUSER) {
272                 sendbuf = "NEWU " + username->GetValue();
273                 r = citsock->serv_trans(sendbuf, recvbuf);
274                 if (r != 2) {
275                         wxMessageDialog nouser(this,
276                                 recvbuf.Mid(4),
277                                 "Error",
278                                 wxOK | wxCENTRE | wxICON_INFORMATION,
279                                 wxDefaultPosition);
280                         nouser.ShowModal();
281                 } else {
282                         sendbuf = "SETP " + password->GetValue();
283                         citsock->serv_trans(sendbuf);
284                         BeginSession(recvbuf);
285                         citsock->curr_pass = password->GetValue();
286                         delete this;            // dismiss the login box
287                 }
288         }
289 }
290
291
292
293 void UserLogin::BeginSession(wxString serv_response) {
294         wxString junk;
295
296         extract(citsock->curr_user, serv_response.Mid(4), 0);
297         BigMDI->SetStatusText(citsock->curr_user, 1);
298         citsock->GotoRoom("_BASEROOM_", "", junk);
299         citsock->axlevel = extract_int(serv_response.Mid(4), 1);
300
301         // FIX ... add code here to perform registration if necessary
302
303         RoomList->LoadRoomList();
304         
305 }