You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDialog.cxx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* 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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <FL/Fl.H>
  25. #include <FL/fl_ask.H>
  26. #include <FL/Fl_Window.H>
  27. #include <FL/Fl_Box.H>
  28. #include <FL/Fl_Input.H>
  29. #include <FL/Fl_Secret_Input.H>
  30. #include <FL/Fl_Button.H>
  31. #include <FL/Fl_Return_Button.H>
  32. #include <rfb/util.h>
  33. #include <rfb/Password.h>
  34. #include <rfb/Exception.h>
  35. #include "i18n.h"
  36. #include "fltk_layout.h"
  37. #include "parameters.h"
  38. #include "UserDialog.h"
  39. using namespace rfb;
  40. static int ret_val = 0;
  41. static void button_cb(Fl_Widget *widget, void *val) {
  42. ret_val = (fl_intptr_t)val;
  43. widget->window()->hide();
  44. }
  45. UserDialog::UserDialog()
  46. {
  47. }
  48. UserDialog::~UserDialog()
  49. {
  50. }
  51. void UserDialog::getUserPasswd(char** user, char** password)
  52. {
  53. CharArray passwordFileStr(passwordFile.getData());
  54. assert(password);
  55. if (!user && passwordFileStr.buf[0]) {
  56. ObfuscatedPasswd obfPwd(256);
  57. FILE* fp;
  58. fp = fopen(passwordFileStr.buf, "rb");
  59. if (!fp)
  60. throw rfb::Exception(_("Opening password file failed"));
  61. obfPwd.length = fread(obfPwd.buf, 1, obfPwd.length, fp);
  62. fclose(fp);
  63. PlainPasswd passwd(obfPwd);
  64. *password = passwd.takeBuf();
  65. return;
  66. }
  67. if (!user) {
  68. fl_message_title(_("VNC authentication"));
  69. *password = strDup(fl_password(_("Password:"), ""));
  70. if (!*password)
  71. throw rfb::Exception(_("Authentication cancelled"));
  72. return;
  73. }
  74. // Largely copied from FLTK so that we get the same look and feel
  75. // as the simpler password input.
  76. Fl_Window *win = new Fl_Window(410, 145, _("VNC authentication"));
  77. win->callback(button_cb,(void *)0);
  78. Fl_Input *username = new Fl_Input(70, 25, 300, 25, _("Username:"));
  79. username->align(FL_ALIGN_TOP_LEFT);
  80. Fl_Secret_Input *passwd = new Fl_Secret_Input(70, 70, 300, 25, _("Password:"));
  81. passwd->align(FL_ALIGN_TOP_LEFT);
  82. Fl_Box *icon = new Fl_Box(10, 10, 50, 50, "?");
  83. icon->box(FL_UP_BOX);
  84. icon->labelfont(FL_TIMES_BOLD);
  85. icon->labelsize(34);
  86. icon->color(FL_WHITE);
  87. icon->labelcolor(FL_BLUE);
  88. Fl_Button *button;
  89. button = new Fl_Return_Button(310, 110, 90, 25, fl_ok);
  90. button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
  91. button->callback(button_cb, (void*)0);
  92. button = new Fl_Button(210, 110, 90, 25, fl_cancel);
  93. button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
  94. button->callback(button_cb, (void*)1);
  95. button->shortcut(FL_Escape);
  96. win->end();
  97. win->set_modal();
  98. ret_val = -1;
  99. win->show();
  100. while (win->shown()) Fl::wait();
  101. if (ret_val == 0) {
  102. *user = strDup(username->value());
  103. *password = strDup(passwd->value());
  104. } else {
  105. *user = strDup("");
  106. *password = strDup("");
  107. }
  108. delete win;
  109. }
  110. bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
  111. {
  112. char buffer[1024];
  113. if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
  114. return 0;
  115. // FLTK doesn't give us a flexible choice of the icon, so we ignore those
  116. // bits for now.
  117. fl_message_title(title);
  118. switch (flags & 0xf) {
  119. case M_OKCANCEL:
  120. return fl_choice("%s", NULL, fl_ok, fl_cancel, buffer) == 1;
  121. case M_YESNO:
  122. return fl_choice("%s", NULL, fl_yes, fl_no, buffer) == 1;
  123. case M_OK:
  124. default:
  125. if (((flags & 0xf0) == M_ICONERROR) ||
  126. ((flags & 0xf0) == M_ICONWARNING))
  127. fl_alert("%s", buffer);
  128. else
  129. fl_message("%s", buffer);
  130. return true;
  131. }
  132. return false;
  133. }