blob: e94ab71fca177ef5190766ec1ad355bd620b2d32 [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <assert.h>
20#include <stdio.h>
21#include <string.h>
22
Pierre Ossman34771ae2011-05-17 12:42:51 +000023#include <FL/Fl.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000024#include <FL/fl_ask.H>
Pierre Ossman34771ae2011-05-17 12:42:51 +000025#include <FL/Fl_Window.H>
26#include <FL/Fl_Box.H>
27#include <FL/Fl_Input.H>
28#include <FL/Fl_Secret_Input.H>
29#include <FL/Fl_Button.H>
30#include <FL/Fl_Return_Button.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000031
32#include <rfb/util.h>
33#include <rfb/Password.h>
34#include <rfb/Exception.h>
35
36#include "i18n.h"
37#include "parameters.h"
38#include "UserDialog.h"
39
40using namespace rfb;
41
Pierre Ossman34771ae2011-05-17 12:42:51 +000042static int ret_val = 0;
43
44static void button_cb(Fl_Widget *widget, void *val) {
45 ret_val = (fl_intptr_t)val;
46 widget->window()->hide();
47}
48
Pierre Ossman5156d5e2011-03-09 09:42:34 +000049UserDialog::UserDialog()
50{
51}
52
53UserDialog::~UserDialog()
54{
55}
56
57void UserDialog::getUserPasswd(char** user, char** password)
58{
59 CharArray passwordFileStr(passwordFile.getData());
60
61 assert(password);
62
63 if (!user && passwordFileStr.buf[0]) {
64 ObfuscatedPasswd obfPwd(256);
65 FILE* fp;
66
67 fp = fopen(passwordFileStr.buf, "r");
68 if (!fp)
69 throw rfb::Exception(_("Opening password file failed"));
70
71 obfPwd.length = fread(obfPwd.buf, 1, obfPwd.length, fp);
72 fclose(fp);
73
74 PlainPasswd passwd(obfPwd);
75 *password = passwd.takeBuf();
76
77 return;
78 }
79
80 if (!user) {
Pierre Ossman34771ae2011-05-17 12:42:51 +000081 fl_message_title(_("VNC authentication"));
82 *password = strDup(fl_password(_("Password:"), ""));
Pierre Ossman5156d5e2011-03-09 09:42:34 +000083 if (!*password)
84 throw rfb::Exception(_("Authentication cancelled"));
85
86 return;
87 }
88
Pierre Ossman34771ae2011-05-17 12:42:51 +000089 // Largely copied from FLTK so that we get the same look and feel
90 // as the simpler password input.
91 Fl_Window *win = new Fl_Window(410, 145, _("VNC authentication"));
92 win->callback(button_cb,(void *)0);
Pierre Ossman5156d5e2011-03-09 09:42:34 +000093
Pierre Ossman34771ae2011-05-17 12:42:51 +000094 Fl_Input *username = new Fl_Input(70, 25, 300, 25, _("Username:"));
95 username->align(FL_ALIGN_TOP_LEFT);
96
97 Fl_Secret_Input *passwd = new Fl_Secret_Input(70, 70, 300, 25, _("Password:"));
98 passwd->align(FL_ALIGN_TOP_LEFT);
99
100 Fl_Box *icon = new Fl_Box(10, 10, 50, 50, "?");
101 icon->box(FL_UP_BOX);
102 icon->labelfont(FL_TIMES_BOLD);
103 icon->labelsize(34);
104 icon->color(FL_WHITE);
105 icon->labelcolor(FL_BLUE);
106
107 Fl_Button *button;
108
109 button = new Fl_Return_Button(310, 110, 90, 25, fl_ok);
110 button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
111 button->callback(button_cb, (void*)0);
112
113 button = new Fl_Button(210, 110, 90, 25, fl_cancel);
114 button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
115 button->callback(button_cb, (void*)1);
116 button->shortcut(FL_Escape);
117
118 win->end();
119
120 win->set_modal();
121
122 ret_val = -1;
123
124 win->show();
125 while (win->shown()) Fl::wait();
126
127 if (ret_val == 0) {
128 *user = strDup(username->value());
129 *password = strDup(passwd->value());
130 } else {
131 *user = strDup("");
132 *password = strDup("");
133 }
134
135 delete win;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000136}
137
138bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
139{
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000140 // FLTK doesn't give us a flexible choice of the icon, so we ignore those
141 // bits for now.
142
143 // FIXME: Filter out % from input text
144
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000145 fl_message_title(title);
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000146
147 switch (flags & 0xf) {
148 case M_OKCANCEL:
149 return fl_choice(text, NULL, fl_ok, fl_cancel) == 1;
150 case M_YESNO:
151 return fl_choice(text, NULL, fl_yes, fl_no) == 1;
152 case M_OK:
153 default:
154 if (((flags & 0xf0) == M_ICONERROR) ||
155 ((flags & 0xf0) == M_ICONWARNING))
156 fl_alert(text);
157 else
158 fl_message(text);
159 return true;
160 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000161
162 return false;
163}