blob: 0ac5009aa962e64c191d83d4266365066a0cd360 [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"
Pierre Ossmanc628ba42011-05-23 12:21:21 +000037#include "fltk_layout.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000038#include "parameters.h"
39#include "UserDialog.h"
40
41using namespace rfb;
42
Pierre Ossman34771ae2011-05-17 12:42:51 +000043static int ret_val = 0;
44
45static void button_cb(Fl_Widget *widget, void *val) {
46 ret_val = (fl_intptr_t)val;
47 widget->window()->hide();
48}
49
Pierre Ossman5156d5e2011-03-09 09:42:34 +000050UserDialog::UserDialog()
51{
52}
53
54UserDialog::~UserDialog()
55{
56}
57
58void UserDialog::getUserPasswd(char** user, char** password)
59{
60 CharArray passwordFileStr(passwordFile.getData());
61
62 assert(password);
63
64 if (!user && passwordFileStr.buf[0]) {
65 ObfuscatedPasswd obfPwd(256);
66 FILE* fp;
67
68 fp = fopen(passwordFileStr.buf, "r");
69 if (!fp)
70 throw rfb::Exception(_("Opening password file failed"));
71
72 obfPwd.length = fread(obfPwd.buf, 1, obfPwd.length, fp);
73 fclose(fp);
74
75 PlainPasswd passwd(obfPwd);
76 *password = passwd.takeBuf();
77
78 return;
79 }
80
81 if (!user) {
Pierre Ossman34771ae2011-05-17 12:42:51 +000082 fl_message_title(_("VNC authentication"));
83 *password = strDup(fl_password(_("Password:"), ""));
Pierre Ossman5156d5e2011-03-09 09:42:34 +000084 if (!*password)
85 throw rfb::Exception(_("Authentication cancelled"));
86
87 return;
88 }
89
Pierre Ossman34771ae2011-05-17 12:42:51 +000090 // Largely copied from FLTK so that we get the same look and feel
91 // as the simpler password input.
92 Fl_Window *win = new Fl_Window(410, 145, _("VNC authentication"));
93 win->callback(button_cb,(void *)0);
Pierre Ossman5156d5e2011-03-09 09:42:34 +000094
Pierre Ossman34771ae2011-05-17 12:42:51 +000095 Fl_Input *username = new Fl_Input(70, 25, 300, 25, _("Username:"));
96 username->align(FL_ALIGN_TOP_LEFT);
97
98 Fl_Secret_Input *passwd = new Fl_Secret_Input(70, 70, 300, 25, _("Password:"));
99 passwd->align(FL_ALIGN_TOP_LEFT);
100
101 Fl_Box *icon = new Fl_Box(10, 10, 50, 50, "?");
102 icon->box(FL_UP_BOX);
103 icon->labelfont(FL_TIMES_BOLD);
104 icon->labelsize(34);
105 icon->color(FL_WHITE);
106 icon->labelcolor(FL_BLUE);
107
108 Fl_Button *button;
109
110 button = new Fl_Return_Button(310, 110, 90, 25, fl_ok);
111 button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
112 button->callback(button_cb, (void*)0);
113
114 button = new Fl_Button(210, 110, 90, 25, fl_cancel);
115 button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
116 button->callback(button_cb, (void*)1);
117 button->shortcut(FL_Escape);
118
119 win->end();
120
121 win->set_modal();
122
123 ret_val = -1;
124
125 win->show();
126 while (win->shown()) Fl::wait();
127
128 if (ret_val == 0) {
129 *user = strDup(username->value());
130 *password = strDup(passwd->value());
131 } else {
132 *user = strDup("");
133 *password = strDup("");
134 }
135
136 delete win;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000137}
138
139bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
140{
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000141 char buffer[1024];
142
143 if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
144 return 0;
145
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000146 // FLTK doesn't give us a flexible choice of the icon, so we ignore those
147 // bits for now.
148
149 // FIXME: Filter out % from input text
150
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000151 fl_message_title(title);
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000152
153 switch (flags & 0xf) {
154 case M_OKCANCEL:
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000155 return fl_choice(buffer, NULL, fl_ok, fl_cancel) == 1;
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000156 case M_YESNO:
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000157 return fl_choice(buffer, NULL, fl_yes, fl_no) == 1;
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000158 case M_OK:
159 default:
160 if (((flags & 0xf0) == M_ICONERROR) ||
161 ((flags & 0xf0) == M_ICONWARNING))
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000162 fl_alert(buffer);
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000163 else
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000164 fl_message(buffer);
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000165 return true;
166 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000167
168 return false;
169}