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.

ServerDialog.cxx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <errno.h>
  23. #include <algorithm>
  24. #include <libgen.h>
  25. #include <FL/Fl.H>
  26. #include <FL/Fl_Input.H>
  27. #include <FL/Fl_Input_Choice.H>
  28. #include <FL/Fl_Button.H>
  29. #include <FL/Fl_Return_Button.H>
  30. #include <FL/fl_draw.H>
  31. #include <FL/fl_ask.H>
  32. #include <FL/Fl_Box.H>
  33. #include <FL/Fl_File_Chooser.H>
  34. #include <os/os.h>
  35. #include <rfb/Exception.h>
  36. #include <rfb/LogWriter.h>
  37. #include "fltk/layout.h"
  38. #include "ServerDialog.h"
  39. #include "OptionsDialog.h"
  40. #include "i18n.h"
  41. #include "vncviewer.h"
  42. #include "parameters.h"
  43. using namespace std;
  44. using namespace rfb;
  45. static LogWriter vlog("ServerDialog");
  46. const char* SERVER_HISTORY="tigervnc.history";
  47. ServerDialog::ServerDialog()
  48. : Fl_Window(450, 0, _("VNC Viewer: Connection Details"))
  49. {
  50. int x, y, x2;
  51. Fl_Button *button;
  52. Fl_Box *divider;
  53. x = OUTER_MARGIN;
  54. y = OUTER_MARGIN;
  55. serverName = new Fl_Input_Choice(LBLLEFT(x, y, w() - OUTER_MARGIN*2,
  56. INPUT_HEIGHT, _("VNC server:")));
  57. // Bug fix for wrong background
  58. serverName->color(FL_BACKGROUND2_COLOR);
  59. y += INPUT_HEIGHT + INNER_MARGIN;
  60. x2 = x;
  61. button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Options..."));
  62. button->callback(this->handleOptions, this);
  63. x2 += BUTTON_WIDTH + INNER_MARGIN;
  64. button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Load..."));
  65. button->callback(this->handleLoad, this);
  66. x2 += BUTTON_WIDTH + INNER_MARGIN;
  67. button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Save As..."));
  68. button->callback(this->handleSaveAs, this);
  69. x2 += BUTTON_WIDTH + INNER_MARGIN;
  70. y += BUTTON_HEIGHT + INNER_MARGIN;
  71. divider = new Fl_Box(0, y, w(), 2);
  72. divider->box(FL_THIN_DOWN_FRAME);
  73. y += divider->h() + INNER_MARGIN;
  74. // Symmetric margin around bottom button bar
  75. y += OUTER_MARGIN - INNER_MARGIN;
  76. button = new Fl_Button(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("About..."));
  77. button->callback(this->handleAbout, this);
  78. x2 = w() - OUTER_MARGIN - BUTTON_WIDTH*2 - INNER_MARGIN*1;
  79. button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Cancel"));
  80. button->callback(this->handleCancel, this);
  81. x2 += BUTTON_WIDTH + INNER_MARGIN;
  82. button = new Fl_Return_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Connect"));
  83. button->callback(this->handleConnect, this);
  84. x2 += BUTTON_WIDTH + INNER_MARGIN;
  85. y += BUTTON_HEIGHT + INNER_MARGIN;
  86. /* Needed for resize to work sanely */
  87. resizable(NULL);
  88. h(y-INNER_MARGIN+OUTER_MARGIN);
  89. callback(this->handleCancel, this);
  90. }
  91. ServerDialog::~ServerDialog()
  92. {
  93. }
  94. void ServerDialog::run(const char* servername, char *newservername)
  95. {
  96. ServerDialog dialog;
  97. dialog.serverName->value(servername);
  98. dialog.show();
  99. try {
  100. size_t i;
  101. dialog.loadServerHistory();
  102. dialog.serverName->clear();
  103. for(i = 0; i < dialog.serverHistory.size(); ++i)
  104. dialog.serverName->add(dialog.serverHistory[i].c_str());
  105. } catch (Exception& e) {
  106. vlog.error("%s", e.str());
  107. fl_alert(_("Unable to load the server history:\n\n%s"),
  108. e.str());
  109. }
  110. while (dialog.shown()) Fl::wait();
  111. if (dialog.serverName->value() == NULL) {
  112. newservername[0] = '\0';
  113. return;
  114. }
  115. strncpy(newservername, dialog.serverName->value(), VNCSERVERNAMELEN);
  116. newservername[VNCSERVERNAMELEN - 1] = '\0';
  117. }
  118. void ServerDialog::handleOptions(Fl_Widget* /*widget*/, void* /*data*/)
  119. {
  120. OptionsDialog::showDialog();
  121. }
  122. void ServerDialog::handleLoad(Fl_Widget* /*widget*/, void* data)
  123. {
  124. ServerDialog *dialog = (ServerDialog*)data;
  125. if (dialog->usedDir.empty())
  126. dialog->usedDir = os::getuserhomedir();
  127. Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir.c_str(),
  128. _("TigerVNC configuration (*.tigervnc)"),
  129. 0, _("Select a TigerVNC configuration file"));
  130. file_chooser->preview(0);
  131. file_chooser->previewButton->hide();
  132. file_chooser->show();
  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. const char* filename = file_chooser->value();
  142. dialog->updateUsedDir(filename);
  143. try {
  144. dialog->serverName->value(loadViewerParameters(filename));
  145. } catch (Exception& e) {
  146. vlog.error("%s", e.str());
  147. fl_alert(_("Unable to load the specified configuration file:\n\n%s"),
  148. e.str());
  149. }
  150. delete(file_chooser);
  151. }
  152. void ServerDialog::handleSaveAs(Fl_Widget* /*widget*/, void* data)
  153. {
  154. ServerDialog *dialog = (ServerDialog*)data;
  155. const char* servername = dialog->serverName->value();
  156. const char* filename;
  157. if (dialog->usedDir.empty())
  158. dialog->usedDir = os::getuserhomedir();
  159. Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir.c_str(),
  160. _("TigerVNC configuration (*.tigervnc)"),
  161. 2, _("Save the TigerVNC configuration to file"));
  162. file_chooser->preview(0);
  163. file_chooser->previewButton->hide();
  164. file_chooser->show();
  165. while(1) {
  166. // Block until user picks something.
  167. while(file_chooser->shown())
  168. Fl::wait();
  169. // Did the user hit cancel?
  170. if (file_chooser->value() == NULL) {
  171. delete(file_chooser);
  172. return;
  173. }
  174. filename = file_chooser->value();
  175. dialog->updateUsedDir(filename);
  176. FILE* f = fopen(filename, "r");
  177. if (f) {
  178. // The file already exists.
  179. fclose(f);
  180. int overwrite_choice = fl_choice(_("%s already exists. Do you want to overwrite?"),
  181. _("Overwrite"), _("No"), NULL, filename);
  182. if (overwrite_choice == 1) {
  183. // If the user doesn't want to overwrite:
  184. file_chooser->show();
  185. continue;
  186. }
  187. }
  188. break;
  189. }
  190. try {
  191. saveViewerParameters(filename, servername);
  192. } catch (Exception& e) {
  193. vlog.error("%s", e.str());
  194. fl_alert(_("Unable to save the specified configuration "
  195. "file:\n\n%s"), e.str());
  196. }
  197. delete(file_chooser);
  198. }
  199. void ServerDialog::handleAbout(Fl_Widget* /*widget*/, void* /*data*/)
  200. {
  201. about_vncviewer();
  202. }
  203. void ServerDialog::handleCancel(Fl_Widget* /*widget*/, void* data)
  204. {
  205. ServerDialog *dialog = (ServerDialog*)data;
  206. dialog->serverName->value("");
  207. dialog->hide();
  208. }
  209. void ServerDialog::handleConnect(Fl_Widget* /*widget*/, void *data)
  210. {
  211. ServerDialog *dialog = (ServerDialog*)data;
  212. const char* servername = dialog->serverName->value();
  213. dialog->hide();
  214. try {
  215. saveViewerParameters(NULL, servername);
  216. } catch (Exception& e) {
  217. vlog.error("%s", e.str());
  218. fl_alert(_("Unable to save the default configuration:\n\n%s"),
  219. e.str());
  220. }
  221. try {
  222. vector<string>::iterator elem = std::find(dialog->serverHistory.begin(), dialog->serverHistory.end(), servername);
  223. // avoid duplicates in the history
  224. if(dialog->serverHistory.end() == elem) {
  225. dialog->serverHistory.insert(dialog->serverHistory.begin(), servername);
  226. dialog->saveServerHistory();
  227. }
  228. } catch (Exception& e) {
  229. vlog.error("%s", e.str());
  230. fl_alert(_("Unable to save the server history:\n\n%s"),
  231. e.str());
  232. }
  233. }
  234. void ServerDialog::loadServerHistory()
  235. {
  236. serverHistory.clear();
  237. #ifdef _WIN32
  238. loadHistoryFromRegKey(serverHistory);
  239. return;
  240. #endif
  241. const char* homeDir = os::getvnchomedir();
  242. if (homeDir == NULL)
  243. throw Exception(_("Could not obtain the home directory path"));
  244. char filepath[PATH_MAX];
  245. snprintf(filepath, sizeof(filepath), "%s/%s", homeDir, SERVER_HISTORY);
  246. /* Read server history from file */
  247. FILE* f = fopen(filepath, "r");
  248. if (!f) {
  249. if (errno == ENOENT) {
  250. // no history file
  251. return;
  252. }
  253. throw Exception(_("Could not open \"%s\": %s"),
  254. filepath, strerror(errno));
  255. }
  256. int lineNr = 0;
  257. while (!feof(f)) {
  258. char line[256];
  259. // Read the next line
  260. lineNr++;
  261. if (!fgets(line, sizeof(line), f)) {
  262. if (feof(f))
  263. break;
  264. fclose(f);
  265. throw Exception(_("Failed to read line %d in file %s: %s"),
  266. lineNr, filepath, strerror(errno));
  267. }
  268. int len = strlen(line);
  269. if (len == (sizeof(line) - 1)) {
  270. fclose(f);
  271. throw Exception(_("Failed to read line %d in file %s: %s"),
  272. lineNr, filepath, _("Line too long"));
  273. }
  274. if ((len > 0) && (line[len-1] == '\n')) {
  275. line[len-1] = '\0';
  276. len--;
  277. }
  278. if ((len > 0) && (line[len-1] == '\r')) {
  279. line[len-1] = '\0';
  280. len--;
  281. }
  282. if (len == 0)
  283. continue;
  284. serverHistory.push_back(line);
  285. }
  286. fclose(f);
  287. }
  288. void ServerDialog::saveServerHistory()
  289. {
  290. #ifdef _WIN32
  291. saveHistoryToRegKey(serverHistory);
  292. return;
  293. #endif
  294. const char* homeDir = os::getvnchomedir();
  295. if (homeDir == NULL)
  296. throw Exception(_("Could not obtain the home directory path"));
  297. char filepath[PATH_MAX];
  298. snprintf(filepath, sizeof(filepath), "%s/%s", homeDir, SERVER_HISTORY);
  299. /* Write server history to file */
  300. FILE* f = fopen(filepath, "w+");
  301. if (!f)
  302. throw Exception(_("Could not open \"%s\": %s"),
  303. filepath, strerror(errno));
  304. // Save the last X elements to the config file.
  305. for(size_t i=0; i < serverHistory.size() && i <= SERVER_HISTORY_SIZE; i++)
  306. fprintf(f, "%s\n", serverHistory[i].c_str());
  307. fclose(f);
  308. }
  309. void ServerDialog::updateUsedDir(const char* filename)
  310. {
  311. char * name = strdup(filename);
  312. usedDir = dirname(name);
  313. free(name);
  314. }