1#include "Mime.h"
2#include "TypeConstants.h"
3#include "Application.h"
4#include "InterfaceDefs.h"
5#include "TranslationUtils.h"
6#include "Button.h"
7#include "Errors.h"
8#include "Window.h"
9#include "TextControl.h"
10#include "Roster.h"
11#include "Bitmap.h"
12#include "Screen.h"
13#include "OS.h"
14
15// POSIX includes
16#include "errno.h"
17#include "malloc.h"
18
19#include "LoginPanel.h"
20#include "md5.h"
21
22// To create:
23//		BRect frame2(100, 150, 350, 290);
24//		LoginPanel *login = new LoginPanel(frame2);
25
26
27// ----- LoginView -----------------------------------------------------
28
29LoginView::LoginView(BRect rect, char *server, char *share) :
30  BView(rect, "LoginView", B_FOLLOW_ALL, B_WILL_DRAW)
31{
32	strcpy(resource, share);
33	strcat(resource, " on ");
34	strcat(resource, server);
35
36	rgb_color gray = ui_color(B_PANEL_BACKGROUND_COLOR);
37	SetViewColor(gray);
38
39	BRect bmpRect(0.0, 0.0, 31.0, 31.0);
40	icon = new BBitmap(bmpRect, B_CMAP8);
41	BMimeType mime("application/x-vnd.Teldar-FileSharing");
42	mime.GetIcon(icon, B_LARGE_ICON);
43
44	BRect r(10, 72, 240, 92);
45	user = new BTextControl(r, "User", "User:", "", NULL);
46	user->SetDivider(55);
47	AddChild(user);
48
49	r.top = 97;
50	r.bottom = r.top + 20;
51	password = new BTextControl(r, "Password", "Password:", "", NULL);
52	password->SetDivider(55);
53	password->TextView()->HideTyping(true);
54	AddChild(password);
55
56	r.Set(LOGIN_PANEL_WIDTH - 160, LOGIN_PANEL_HEIGHT - 33, LOGIN_PANEL_WIDTH - 90, LOGIN_PANEL_HEIGHT - 13);
57	BButton *okBtn = new BButton(r, "OkayBtn", "Login", new BMessage(MSG_LOGIN_OK), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
58	okBtn->MakeDefault(true);
59	AddChild(okBtn);
60
61	r.Set(LOGIN_PANEL_WIDTH - 80, LOGIN_PANEL_HEIGHT - 33, LOGIN_PANEL_WIDTH - 10, LOGIN_PANEL_HEIGHT - 13);
62	AddChild(new BButton(r, "CancelBtn", "Cancel", new BMessage(MSG_LOGIN_CANCEL), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM));
63}
64
65LoginView::~LoginView()
66{
67}
68
69void LoginView::Draw(BRect rect)
70{
71	BRect r = Bounds();
72	BRect iconRect(13.0, 5.0, 45.0, 37.0);
73	rgb_color black = { 0, 0, 0, 255 };
74	rgb_color gray = ui_color(B_PANEL_BACKGROUND_COLOR);
75
76	SetViewColor(gray);
77	SetLowColor(gray);
78	FillRect(r, B_SOLID_LOW);
79
80	SetHighColor(black);
81	SetFont(be_bold_font);
82	SetFontSize(11);
83	MovePenTo(55, 15);
84	DrawString("Login Required");
85
86	SetFont(be_plain_font);
87	SetFontSize(10);
88	MovePenTo(55, 28);
89	DrawString("The print server has rejected your user name and");
90	MovePenTo(55, 40);
91	DrawString("password.  Please re-enter your user credentials.");
92
93	MovePenTo(13, 61);
94	DrawString("Printer:");
95	MovePenTo(70, 61);
96	DrawString(resource);
97
98	SetDrawingMode(B_OP_ALPHA);
99	SetHighColor(0, 0, 0, 180);
100	SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_COMPOSITE);
101	DrawBitmap(icon, iconRect);
102}
103
104
105// ----- LoginPanel ----------------------------------------------------------------------
106
107LoginPanel::LoginPanel(BRect frame, char *server, char *share, bool show) :
108  BWindow(frame, "Login", B_MODAL_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, B_NOT_ZOOMABLE | B_NOT_RESIZABLE)
109{
110	user[0] = password[0] = 0;
111	cancelled = false;
112
113	BRect r = Bounds();
114	loginView = new LoginView(r, server, share);
115	AddChild(loginView);
116
117	if (show)
118		Show();
119}
120
121LoginPanel::~LoginPanel()
122{
123}
124
125// Center()
126//
127void LoginPanel::Center()
128{
129	BScreen screen(this);
130	BRect screenFrame = screen.Frame();
131	BRect winFrame;
132	winFrame.left = (screenFrame.Width() - 300) / 2;
133	winFrame.top = (screenFrame.Height() - 158) / 2;
134	winFrame.right = winFrame.left + 300;
135	winFrame.bottom = winFrame.top + 158;
136	MoveTo(winFrame.left, winFrame.top);
137	ResizeTo(300, 158);
138
139	Show();
140}
141
142// MessageReceived()
143//
144void LoginPanel::MessageReceived(BMessage *msg)
145{
146	switch (msg->what)
147	{
148		case MSG_LOGIN_OK:
149			safeStringCopy(user, loginView->GetUser(), sizeof(user));
150			safeStringCopy(password, loginView->GetPassword(), sizeof(password));
151			md5EncodeString(password, md5passwd);
152			BWindow::Quit();
153			break;
154
155		case MSG_LOGIN_CANCEL:
156			cancelled = true;
157			BWindow::Quit();
158			break;
159
160		default:
161			BWindow::MessageReceived(msg);
162			break;
163	}
164}
165
166void safeStringCopy(char *dest, const char *source, int destSize)
167{
168	int length = strlen(source);
169	if (length >= destSize)
170		length = destSize - 1;
171
172	strncpy(dest, source, length);
173	dest[length] = 0;
174}
175