Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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