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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. * Copyright 2012 Samuel Mannehed <samuel@cendio.se> for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <FL/Fl.H>
  23. #include <FL/Fl_Input.H>
  24. #include <FL/Fl_Button.H>
  25. #include <FL/Fl_Return_Button.H>
  26. #include <FL/fl_draw.H>
  27. #include <FL/fl_ask.H>
  28. #include <FL/Fl_Box.H>
  29. #include <FL/Fl_File_Chooser.H>
  30. #include "ServerDialog.h"
  31. #include "OptionsDialog.h"
  32. #include "fltk_layout.h"
  33. #include "i18n.h"
  34. #include "vncviewer.h"
  35. #include "parameters.h"
  36. #include "rfb/Exception.h"
  37. ServerDialog::ServerDialog()
  38. : Fl_Window(450, 160, _("VNC Viewer: Connection Details"))
  39. {
  40. int x, y;
  41. Fl_Button *button;
  42. Fl_Box *divider;
  43. int margin = 20;
  44. int server_label_width = gui_str_len(_("VNC server:"));
  45. x = margin + server_label_width;
  46. y = margin;
  47. serverName = new Fl_Input(x, y, w() - margin*2 - server_label_width, INPUT_HEIGHT, _("VNC server:"));
  48. int adjust = (w() - 20) / 4;
  49. int button_width = adjust - margin/2;
  50. x = margin;
  51. y = margin + margin/2 + INPUT_HEIGHT;
  52. y += margin/2;
  53. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Options..."));
  54. button->callback(this->handleOptions, this);
  55. x += adjust;
  56. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Load..."));
  57. button->callback(this->handleLoad, this);
  58. x += adjust;
  59. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Save As..."));
  60. button->callback(this->handleSaveAs, this);
  61. x = 0;
  62. y += margin/2 + BUTTON_HEIGHT;
  63. divider = new Fl_Box(x, y, w(), 2);
  64. divider->box(FL_THIN_DOWN_FRAME);
  65. x += margin;
  66. y += margin/2;
  67. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("About..."));
  68. button->callback(this->handleAbout, this);
  69. x = w() - margin - adjust - button_width - 20;
  70. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Cancel"));
  71. button->callback(this->handleCancel, this);
  72. x += adjust;
  73. button = new Fl_Return_Button(x, y, button_width+20, BUTTON_HEIGHT, _("Connect"));
  74. button->callback(this->handleConnect, this);
  75. callback(this->handleCancel, this);
  76. set_modal();
  77. }
  78. ServerDialog::~ServerDialog()
  79. {
  80. }
  81. void ServerDialog::run(const char* servername, char *newservername)
  82. {
  83. ServerDialog dialog;
  84. dialog.serverName->value(servername);
  85. dialog.show();
  86. while (dialog.shown()) Fl::wait();
  87. if (dialog.serverName->value() == NULL) {
  88. newservername[0] = '\0';
  89. return;
  90. }
  91. strncpy(newservername, dialog.serverName->value(), VNCSERVERNAMELEN);
  92. newservername[VNCSERVERNAMELEN - 1] = '\0';
  93. }
  94. void ServerDialog::handleOptions(Fl_Widget *widget, void *data)
  95. {
  96. OptionsDialog::showDialog();
  97. }
  98. void ServerDialog::handleLoad(Fl_Widget *widget, void *data)
  99. {
  100. ServerDialog *dialog = (ServerDialog*)data;
  101. Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", _("TigerVNC configuration (*.tigervnc)"),
  102. 0, _("Select a TigerVNC configuration file"));
  103. file_chooser->preview(0);
  104. file_chooser->previewButton->hide();
  105. file_chooser->show();
  106. // Block until user picks something.
  107. while(file_chooser->shown())
  108. Fl::wait();
  109. // Did the user hit cancel?
  110. if (file_chooser->value() == NULL) {
  111. delete(file_chooser);
  112. return;
  113. }
  114. const char* filename = file_chooser->value();
  115. try {
  116. dialog->serverName->value(loadViewerParameters(filename));
  117. } catch (rfb::Exception& e) {
  118. fl_alert("%s", e.str());
  119. }
  120. delete(file_chooser);
  121. }
  122. void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
  123. {
  124. ServerDialog *dialog = (ServerDialog*)data;
  125. const char* servername = dialog->serverName->value();
  126. const char* filename;
  127. Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", _("TigerVNC configuration (*.tigervnc)"),
  128. 2, _("Save the TigerVNC configuration to file"));
  129. file_chooser->preview(0);
  130. file_chooser->previewButton->hide();
  131. file_chooser->show();
  132. while(1) {
  133. // Block until user picks something.
  134. while(file_chooser->shown())
  135. Fl::wait();
  136. // Did the user hit cancel?
  137. if (file_chooser->value() == NULL) {
  138. delete(file_chooser);
  139. return;
  140. }
  141. filename = file_chooser->value();
  142. FILE* f = fopen(filename, "r");
  143. if (f) {
  144. // The file already exists.
  145. fclose(f);
  146. int overwrite_choice = fl_choice(_("%s already exists. Do you want to overwrite?"),
  147. _("Overwrite"), _("No"), NULL, filename);
  148. if (overwrite_choice == 1) {
  149. // If the user doesn't want to overwrite:
  150. file_chooser->show();
  151. continue;
  152. }
  153. }
  154. break;
  155. }
  156. try {
  157. saveViewerParameters(filename, servername);
  158. } catch (rfb::Exception& e) {
  159. fl_alert("%s", e.str());
  160. }
  161. delete(file_chooser);
  162. }
  163. void ServerDialog::handleAbout(Fl_Widget *widget, void *data)
  164. {
  165. about_vncviewer();
  166. }
  167. void ServerDialog::handleCancel(Fl_Widget *widget, void *data)
  168. {
  169. ServerDialog *dialog = (ServerDialog*)data;
  170. dialog->serverName->value(NULL);
  171. dialog->hide();
  172. }
  173. void ServerDialog::handleConnect(Fl_Widget *widget, void *data)
  174. {
  175. ServerDialog *dialog = (ServerDialog*)data;
  176. const char* servername = dialog->serverName->value();
  177. dialog->hide();
  178. try {
  179. saveViewerParameters(NULL, servername);
  180. } catch (rfb::Exception& e) {
  181. fl_alert("%s", e.str());
  182. }
  183. }