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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. }
  88. ServerDialog::~ServerDialog()
  89. {
  90. if (usedDir)
  91. free(usedDir);
  92. }
  93. void ServerDialog::run(const char* servername, char *newservername)
  94. {
  95. ServerDialog dialog;
  96. dialog.serverName->value(servername);
  97. dialog.show();
  98. try {
  99. size_t i;
  100. dialog.loadServerHistory();
  101. dialog.serverName->clear();
  102. for(i = 0; i < dialog.serverHistory.size(); ++i)
  103. dialog.serverName->add(dialog.serverHistory[i].c_str());
  104. } catch (Exception& e) {
  105. vlog.error("%s", e.str());
  106. fl_alert(_("Unable to load the server history:\n\n%s"),
  107. e.str());
  108. }
  109. while (dialog.shown()) Fl::wait();
  110. if (dialog.serverName->value() == NULL) {
  111. newservername[0] = '\0';
  112. return;
  113. }
  114. strncpy(newservername, dialog.serverName->value(), VNCSERVERNAMELEN);
  115. newservername[VNCSERVERNAMELEN - 1] = '\0';
  116. }
  117. void ServerDialog::handleOptions(Fl_Widget *widget, void *data)
  118. {
  119. OptionsDialog::showDialog();
  120. }
  121. void ServerDialog::handleLoad(Fl_Widget *widget, void *data)
  122. {
  123. ServerDialog *dialog = (ServerDialog*)data;
  124. if (!dialog->usedDir)
  125. getuserhomedir(&(dialog->usedDir));
  126. Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"),
  127. 0, _("Select a TigerVNC configuration file"));
  128. file_chooser->preview(0);
  129. file_chooser->previewButton->hide();
  130. file_chooser->show();
  131. // Block until user picks something.
  132. while(file_chooser->shown())
  133. Fl::wait();
  134. // Did the user hit cancel?
  135. if (file_chooser->value() == NULL) {
  136. delete(file_chooser);
  137. return;
  138. }
  139. const char* filename = file_chooser->value();
  140. dialog->updateUsedDir(filename);
  141. try {
  142. dialog->serverName->value(loadViewerParameters(filename));
  143. } catch (Exception& e) {
  144. vlog.error("%s", e.str());
  145. fl_alert(_("Unable to load the specified configuration file:\n\n%s"),
  146. e.str());
  147. }
  148. delete(file_chooser);
  149. }
  150. void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
  151. {
  152. ServerDialog *dialog = (ServerDialog*)data;
  153. const char* servername = dialog->serverName->value();
  154. const char* filename;
  155. if (!dialog->usedDir)
  156. getuserhomedir(&dialog->usedDir);
  157. Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"),
  158. 2, _("Save the TigerVNC configuration to file"));
  159. file_chooser->preview(0);
  160. file_chooser->previewButton->hide();
  161. file_chooser->show();
  162. while(1) {
  163. // Block until user picks something.
  164. while(file_chooser->shown())
  165. Fl::wait();
  166. // Did the user hit cancel?
  167. if (file_chooser->value() == NULL) {
  168. delete(file_chooser);
  169. return;
  170. }
  171. filename = file_chooser->value();
  172. dialog->updateUsedDir(filename);
  173. FILE* f = fopen(filename, "r");
  174. if (f) {
  175. // The file already exists.
  176. fclose(f);
  177. int overwrite_choice = fl_choice(_("%s already exists. Do you want to overwrite?"),
  178. _("Overwrite"), _("No"), NULL, filename);
  179. if (overwrite_choice == 1) {
  180. // If the user doesn't want to overwrite:
  181. file_chooser->show();
  182. continue;
  183. }
  184. }
  185. break;
  186. }
  187. try {
  188. saveViewerParameters(filename, servername);
  189. } catch (Exception& e) {
  190. vlog.error("%s", e.str());
  191. fl_alert(_("Unable to save the specified configuration "
  192. "file:\n\n%s"), e.str());
  193. }
  194. delete(file_chooser);
  195. }
  196. void ServerDialog::handleAbout(Fl_Widget *widget, void *data)
  197. {
  198. about_vncviewer();
  199. }
  200. void ServerDialog::handleCancel(Fl_Widget *widget, void *data)
  201. {
  202. ServerDialog *dialog = (ServerDialog*)data;
  203. dialog->serverName->value("");
  204. dialog->hide();
  205. }
  206. void ServerDialog::handleConnect(Fl_Widget *widget, void *data)
  207. {
  208. ServerDialog *dialog = (ServerDialog*)data;
  209. const char* servername = dialog->serverName->value();
  210. dialog->hide();
  211. try {
  212. saveViewerParameters(NULL, servername);
  213. } catch (Exception& e) {
  214. vlog.error("%s", e.str());
  215. fl_alert(_("Unable to save the default configuration:\n\n%s"),
  216. e.str());
  217. }
  218. try {
  219. vector<string>::iterator elem = std::find(dialog->serverHistory.begin(), dialog->serverHistory.end(), servername);
  220. // avoid duplicates in the history
  221. if(dialog->serverHistory.end() == elem) {
  222. dialog->serverHistory.insert(dialog->serverHistory.begin(), servername);
  223. dialog->saveServerHistory();
  224. }
  225. } catch (Exception& e) {
  226. vlog.error("%s", e.str());
  227. fl_alert(_("Unable to save the server history:\n\n%s"),
  228. e.str());
  229. }
  230. }
  231. void ServerDialog::loadServerHistory()
  232. {
  233. serverHistory.clear();
  234. #ifdef _WIN32
  235. loadHistoryFromRegKey(serverHistory);
  236. return;
  237. #endif
  238. char* homeDir = NULL;
  239. if (getvnchomedir(&homeDir) == -1)
  240. throw Exception(_("Could not obtain the home directory path"));
  241. char filepath[PATH_MAX];
  242. snprintf(filepath, sizeof(filepath), "%s%s", homeDir, SERVER_HISTORY);
  243. delete[] homeDir;
  244. /* Read server history from file */
  245. FILE* f = fopen(filepath, "r");
  246. if (!f) {
  247. if (errno == ENOENT) {
  248. // no history file
  249. return;
  250. }
  251. throw Exception(_("Could not open \"%s\": %s"),
  252. filepath, strerror(errno));
  253. }
  254. int lineNr = 0;
  255. while (!feof(f)) {
  256. char line[256];
  257. // Read the next line
  258. lineNr++;
  259. if (!fgets(line, sizeof(line), f)) {
  260. if (feof(f))
  261. break;
  262. fclose(f);
  263. throw Exception(_("Failed to read line %d in file %s: %s"),
  264. lineNr, filepath, strerror(errno));
  265. }
  266. int len = strlen(line);
  267. if (len == (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. if ((len > 0) && (line[len-1] == '\n')) {
  273. line[len-1] = '\0';
  274. len--;
  275. }
  276. if ((len > 0) && (line[len-1] == '\r')) {
  277. line[len-1] = '\0';
  278. len--;
  279. }
  280. if (len == 0)
  281. continue;
  282. serverHistory.push_back(line);
  283. }
  284. fclose(f);
  285. }
  286. void ServerDialog::saveServerHistory()
  287. {
  288. #ifdef _WIN32
  289. saveHistoryToRegKey(serverHistory);
  290. return;
  291. #endif
  292. char* homeDir = NULL;
  293. if (getvnchomedir(&homeDir) == -1)
  294. throw Exception(_("Could not obtain the home directory path"));
  295. char filepath[PATH_MAX];
  296. snprintf(filepath, sizeof(filepath), "%s%s", homeDir, SERVER_HISTORY);
  297. delete[] homeDir;
  298. /* Write server history to file */
  299. FILE* f = fopen(filepath, "w+");
  300. if (!f)
  301. throw Exception(_("Could not open \"%s\": %s"),
  302. filepath, strerror(errno));
  303. // Save the last X elements to the config file.
  304. for(size_t i=0; i < serverHistory.size() && i <= SERVER_HISTORY_SIZE; i++)
  305. fprintf(f, "%s\n", serverHistory[i].c_str());
  306. fclose(f);
  307. }
  308. void ServerDialog::updateUsedDir(const char* filename)
  309. {
  310. char * name = strdup(filename);
  311. usedDir = strdup(dirname(name));
  312. free(name);
  313. }