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 9.9KB

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 "ServerDialog.h"
  38. #include "OptionsDialog.h"
  39. #include "fltk_layout.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, 160, _("VNC Viewer: Connection Details"))
  49. {
  50. int x, y;
  51. Fl_Button *button;
  52. Fl_Box *divider;
  53. int margin = 20;
  54. int server_label_width = gui_str_len(_("VNC server:"));
  55. x = margin + server_label_width;
  56. y = margin;
  57. serverName = new Fl_Input_Choice(x, y, w() - margin*2 - server_label_width, INPUT_HEIGHT, _("VNC server:"));
  58. usedDir = NULL;
  59. int adjust = (w() - 20) / 4;
  60. int button_width = adjust - margin/2;
  61. x = margin;
  62. y = margin + margin/2 + INPUT_HEIGHT;
  63. y += margin/2;
  64. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Options..."));
  65. button->callback(this->handleOptions, this);
  66. x += adjust;
  67. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Load..."));
  68. button->callback(this->handleLoad, this);
  69. x += adjust;
  70. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Save As..."));
  71. button->callback(this->handleSaveAs, this);
  72. x = 0;
  73. y += margin/2 + BUTTON_HEIGHT;
  74. divider = new Fl_Box(x, y, w(), 2);
  75. divider->box(FL_THIN_DOWN_FRAME);
  76. x += margin;
  77. y += margin/2;
  78. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("About..."));
  79. button->callback(this->handleAbout, this);
  80. x = w() - margin - adjust - button_width - 20;
  81. button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Cancel"));
  82. button->callback(this->handleCancel, this);
  83. x += adjust;
  84. button = new Fl_Return_Button(x, y, button_width+20, BUTTON_HEIGHT, _("Connect"));
  85. button->callback(this->handleConnect, this);
  86. callback(this->handleCancel, this);
  87. set_modal();
  88. }
  89. ServerDialog::~ServerDialog()
  90. {
  91. if (usedDir)
  92. free(usedDir);
  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)
  126. getuserhomedir(&(dialog->usedDir));
  127. Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"),
  128. 0, _("Select a TigerVNC configuration file"));
  129. file_chooser->preview(0);
  130. file_chooser->previewButton->hide();
  131. file_chooser->show();
  132. // Block until user picks something.
  133. while(file_chooser->shown())
  134. Fl::wait();
  135. // Did the user hit cancel?
  136. if (file_chooser->value() == NULL) {
  137. delete(file_chooser);
  138. return;
  139. }
  140. const char* filename = file_chooser->value();
  141. dialog->updateUsedDir(filename);
  142. try {
  143. dialog->serverName->value(loadViewerParameters(filename));
  144. } catch (Exception& e) {
  145. vlog.error("%s", e.str());
  146. fl_alert(_("Unable to load the specified configuration file:\n\n%s"),
  147. e.str());
  148. }
  149. delete(file_chooser);
  150. }
  151. void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
  152. {
  153. ServerDialog *dialog = (ServerDialog*)data;
  154. const char* servername = dialog->serverName->value();
  155. const char* filename;
  156. if (!dialog->usedDir)
  157. getuserhomedir(&dialog->usedDir);
  158. Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"),
  159. 2, _("Save the TigerVNC configuration to file"));
  160. file_chooser->preview(0);
  161. file_chooser->previewButton->hide();
  162. file_chooser->show();
  163. while(1) {
  164. // Block until user picks something.
  165. while(file_chooser->shown())
  166. Fl::wait();
  167. // Did the user hit cancel?
  168. if (file_chooser->value() == NULL) {
  169. delete(file_chooser);
  170. return;
  171. }
  172. filename = file_chooser->value();
  173. dialog->updateUsedDir(filename);
  174. FILE* f = fopen(filename, "r");
  175. if (f) {
  176. // The file already exists.
  177. fclose(f);
  178. int overwrite_choice = fl_choice(_("%s already exists. Do you want to overwrite?"),
  179. _("Overwrite"), _("No"), NULL, filename);
  180. if (overwrite_choice == 1) {
  181. // If the user doesn't want to overwrite:
  182. file_chooser->show();
  183. continue;
  184. }
  185. }
  186. break;
  187. }
  188. try {
  189. saveViewerParameters(filename, servername);
  190. } catch (Exception& e) {
  191. vlog.error("%s", e.str());
  192. fl_alert(_("Unable to save the specified configuration "
  193. "file:\n\n%s"), e.str());
  194. }
  195. delete(file_chooser);
  196. }
  197. void ServerDialog::handleAbout(Fl_Widget *widget, void *data)
  198. {
  199. about_vncviewer();
  200. }
  201. void ServerDialog::handleCancel(Fl_Widget *widget, void *data)
  202. {
  203. ServerDialog *dialog = (ServerDialog*)data;
  204. dialog->serverName->value("");
  205. dialog->hide();
  206. }
  207. void ServerDialog::handleConnect(Fl_Widget *widget, void *data)
  208. {
  209. ServerDialog *dialog = (ServerDialog*)data;
  210. const char* servername = dialog->serverName->value();
  211. dialog->hide();
  212. try {
  213. saveViewerParameters(NULL, servername);
  214. } catch (Exception& e) {
  215. vlog.error("%s", e.str());
  216. fl_alert(_("Unable to save the default configuration:\n\n%s"),
  217. e.str());
  218. }
  219. try {
  220. vector<string>::iterator elem = std::find(dialog->serverHistory.begin(), dialog->serverHistory.end(), servername);
  221. // avoid duplicates in the history
  222. if(dialog->serverHistory.end() == elem) {
  223. dialog->serverHistory.insert(dialog->serverHistory.begin(), servername);
  224. dialog->saveServerHistory();
  225. }
  226. } catch (Exception& e) {
  227. vlog.error("%s", e.str());
  228. fl_alert(_("Unable to save the server history:\n\n%s"),
  229. e.str());
  230. }
  231. }
  232. void ServerDialog::loadServerHistory()
  233. {
  234. serverHistory.clear();
  235. #ifdef _WIN32
  236. loadHistoryFromRegKey(serverHistory);
  237. return;
  238. #endif
  239. char* homeDir = NULL;
  240. if (getvnchomedir(&homeDir) == -1)
  241. throw Exception(_("Could not obtain the home directory path"));
  242. char filepath[PATH_MAX];
  243. snprintf(filepath, sizeof(filepath), "%s%s", homeDir, SERVER_HISTORY);
  244. delete[] homeDir;
  245. /* Read server history from file */
  246. FILE* f = fopen(filepath, "r");
  247. if (!f) {
  248. if (errno == ENOENT) {
  249. // no history file
  250. return;
  251. }
  252. throw Exception(_("Could not open \"%s\": %s"),
  253. filepath, strerror(errno));
  254. }
  255. int lineNr = 0;
  256. while (!feof(f)) {
  257. char line[256];
  258. // Read the next line
  259. lineNr++;
  260. if (!fgets(line, sizeof(line), f)) {
  261. if (feof(f))
  262. break;
  263. fclose(f);
  264. throw Exception(_("Failed to read line %d in file %s: %s"),
  265. lineNr, filepath, strerror(errno));
  266. }
  267. if (strlen(line) == (sizeof(line) - 1)) {
  268. fclose(f);
  269. throw Exception(_("Failed to read line %d in file %s: %s"),
  270. lineNr, filepath, _("Line too long"));
  271. }
  272. int len = strlen(line);
  273. if (line[len-1] == '\n') {
  274. line[len-1] = '\0';
  275. len--;
  276. }
  277. if (line[len-1] == '\r') {
  278. line[len-1] = '\0';
  279. len--;
  280. }
  281. serverHistory.push_back(line);
  282. }
  283. fclose(f);
  284. }
  285. void ServerDialog::saveServerHistory()
  286. {
  287. #ifdef _WIN32
  288. saveHistoryToRegKey(serverHistory);
  289. return;
  290. #endif
  291. char* homeDir = NULL;
  292. if (getvnchomedir(&homeDir) == -1)
  293. throw Exception(_("Could not obtain the home directory path"));
  294. char filepath[PATH_MAX];
  295. snprintf(filepath, sizeof(filepath), "%s%s", homeDir, SERVER_HISTORY);
  296. delete[] homeDir;
  297. /* Write server history to file */
  298. FILE* f = fopen(filepath, "w+");
  299. if (!f)
  300. throw Exception(_("Could not open \"%s\": %s"),
  301. filepath, strerror(errno));
  302. // Save the last X elements to the config file.
  303. for(size_t i=0; i < serverHistory.size() && i <= SERVER_HISTORY_SIZE; i++)
  304. fprintf(f, "%s\n", serverHistory[i].c_str());
  305. fclose(f);
  306. }
  307. void ServerDialog::updateUsedDir(const char* filename)
  308. {
  309. char * name = strdup(filename);
  310. usedDir = strdup(dirname(name));
  311. free(name);
  312. }