blob: fec178964439f1b07aa92d3149748d9b33ecc315 [file] [log] [blame]
Pierre Ossman561ff0c2011-05-13 14:04:59 +00001/* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
Peter Åstrand8a2b0812012-08-08 11:49:01 +00002 * Copyright 2012 Samuel Mannehed <samuel@cendio.se> for Cendio AB
Pierre Ossman561ff0c2011-05-13 14:04:59 +00003 *
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
Peter Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossman561ff0c2011-05-13 14:04:59 +000024#include <FL/Fl.H>
25#include <FL/Fl_Input.H>
26#include <FL/Fl_Button.H>
27#include <FL/Fl_Return_Button.H>
28#include <FL/fl_draw.H>
Peter Åstrand8a2b0812012-08-08 11:49:01 +000029#include <FL/fl_ask.H>
30#include <FL/Fl_Box.H>
31#include <FL/Fl_File_Chooser.H>
Pierre Ossman561ff0c2011-05-13 14:04:59 +000032
33#include "ServerDialog.h"
Pierre Ossmand463b572011-05-16 12:04:43 +000034#include "OptionsDialog.h"
35#include "fltk_layout.h"
Pierre Ossman561ff0c2011-05-13 14:04:59 +000036#include "i18n.h"
Pierre Ossman39ceb502011-07-12 15:54:25 +000037#include "vncviewer.h"
Peter Åstrand8a2b0812012-08-08 11:49:01 +000038#include "parameters.h"
39#include "rfb/Exception.h"
Pierre Ossman561ff0c2011-05-13 14:04:59 +000040
41ServerDialog::ServerDialog()
Peter Åstrand8a2b0812012-08-08 11:49:01 +000042 : Fl_Window(450, 160, _("VNC Viewer: Connection Details"))
Pierre Ossman561ff0c2011-05-13 14:04:59 +000043{
Peter Åstrand8a2b0812012-08-08 11:49:01 +000044 int x, y;
Pierre Ossman561ff0c2011-05-13 14:04:59 +000045 Fl_Button *button;
Peter Åstrand8a2b0812012-08-08 11:49:01 +000046 Fl_Box *divider;
Pierre Ossman561ff0c2011-05-13 14:04:59 +000047
Peter Åstrand8a2b0812012-08-08 11:49:01 +000048 int margin = 20;
49 int server_label_width = gui_str_len(_("VNC server:"));
Pierre Ossman561ff0c2011-05-13 14:04:59 +000050
Peter Åstrand8a2b0812012-08-08 11:49:01 +000051 x = margin + server_label_width;
52 y = margin;
53
54 serverName = new Fl_Input(x, y, w() - margin*2 - server_label_width, INPUT_HEIGHT, _("VNC server:"));
Pierre Ossman561ff0c2011-05-13 14:04:59 +000055
Peter Åstrand8a2b0812012-08-08 11:49:01 +000056 int adjust = (w() - 20) / 4;
57 int button_width = adjust - margin/2;
58
59 x = margin;
60 y = margin + margin/2 + INPUT_HEIGHT;
61
62 y += margin/2;
63
64 button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Options..."));
65 button->callback(this->handleOptions, this);
66
67 x += adjust;
68
69 button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Load..."));
70 button->callback(this->handleLoad, this);
71
72 x += adjust;
73
74 button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Save As..."));
75 button->callback(this->handleSaveAs, this);
76
77 x = 0;
78 y += margin/2 + BUTTON_HEIGHT;
79
80 divider = new Fl_Box(x, y, w(), 2);
81 divider->box(FL_THIN_DOWN_FRAME);
82
83 x += margin;
84 y += margin/2;
85
86 button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("About..."));
Pierre Ossman561ff0c2011-05-13 14:04:59 +000087 button->callback(this->handleAbout, this);
88
Peter Åstrand8a2b0812012-08-08 11:49:01 +000089 x = w() - margin - adjust - button_width - 20;
Pierre Ossmand463b572011-05-16 12:04:43 +000090
Peter Åstrand8a2b0812012-08-08 11:49:01 +000091 button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Cancel"));
Pierre Ossman561ff0c2011-05-13 14:04:59 +000092 button->callback(this->handleCancel, this);
93
Peter Åstrand8a2b0812012-08-08 11:49:01 +000094 x += adjust;
95
96 button = new Fl_Return_Button(x, y, button_width+20, BUTTON_HEIGHT, _("Connect"));
97 button->callback(this->handleConnect, this);
Pierre Ossman561ff0c2011-05-13 14:04:59 +000098
99 callback(this->handleCancel, this);
100
101 set_modal();
102}
103
104
105ServerDialog::~ServerDialog()
106{
107}
108
109
Adam Tkac8ac4b302013-01-23 13:55:46 +0000110void ServerDialog::run(const char* servername, char *newservername)
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000111{
112 ServerDialog dialog;
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000113
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000114 dialog.serverName->value(servername);
115
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000116 dialog.show();
117 while (dialog.shown()) Fl::wait();
118
Adam Tkac8ac4b302013-01-23 13:55:46 +0000119 if (dialog.serverName->value() == NULL) {
120 newservername[0] = '\0';
121 return;
122 }
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000123
Adam Tkac8ac4b302013-01-23 13:55:46 +0000124 strncpy(newservername, dialog.serverName->value(), VNCSERVERNAMELEN);
125 newservername[VNCSERVERNAMELEN - 1] = '\0';
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000126}
127
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000128void ServerDialog::handleOptions(Fl_Widget *widget, void *data)
129{
130 OptionsDialog::showDialog();
131}
132
133
134void ServerDialog::handleLoad(Fl_Widget *widget, void *data)
135{
136 ServerDialog *dialog = (ServerDialog*)data;
Alexander Shopov038bb432017-04-29 11:35:30 +0200137 Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", _("TigerVNC configuration (*.tigervnc)"),
138 0, _("Select a TigerVNC configuration file"));
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000139 file_chooser->preview(0);
140 file_chooser->previewButton->hide();
141 file_chooser->show();
142
143 // Block until user picks something.
144 while(file_chooser->shown())
145 Fl::wait();
146
147 // Did the user hit cancel?
148 if (file_chooser->value() == NULL) {
149 delete(file_chooser);
150 return;
151 }
152
Jan Grulicha5720e52018-11-20 10:44:39 +0100153 const char* filename = file_chooser->value();
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000154
155 try {
156 dialog->serverName->value(loadViewerParameters(filename));
157 } catch (rfb::Exception& e) {
158 fl_alert("%s", e.str());
159 }
160
161 delete(file_chooser);
162}
163
164
165void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
166{
167 ServerDialog *dialog = (ServerDialog*)data;
Jan Grulicha5720e52018-11-20 10:44:39 +0100168 const char* servername = dialog->serverName->value();
169 const char* filename;
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000170
Alexander Shopov038bb432017-04-29 11:35:30 +0200171 Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", _("TigerVNC configuration (*.tigervnc)"),
172 2, _("Save the TigerVNC configuration to file"));
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000173
174 file_chooser->preview(0);
175 file_chooser->previewButton->hide();
176 file_chooser->show();
177
178 while(1) {
179
180 // Block until user picks something.
181 while(file_chooser->shown())
182 Fl::wait();
183
184 // Did the user hit cancel?
185 if (file_chooser->value() == NULL) {
186 delete(file_chooser);
187 return;
188 }
189
Jan Grulicha5720e52018-11-20 10:44:39 +0100190 filename = file_chooser->value();
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000191
192 FILE* f = fopen(filename, "r");
193 if (f) {
194
195 // The file already exists.
196 fclose(f);
Alexander Shopov038bb432017-04-29 11:35:30 +0200197 int overwrite_choice = fl_choice(_("%s already exists. Do you want to overwrite?"),
198 _("Overwrite"), _("No"), NULL, filename);
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000199 if (overwrite_choice == 1) {
200
201 // If the user doesn't want to overwrite:
202 file_chooser->show();
203 continue;
204 }
205 }
206
207 break;
208 }
209
210 try {
211 saveViewerParameters(filename, servername);
212 } catch (rfb::Exception& e) {
213 fl_alert("%s", e.str());
214 }
215
216 delete(file_chooser);
217}
218
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000219
220void ServerDialog::handleAbout(Fl_Widget *widget, void *data)
221{
222 about_vncviewer();
223}
224
225
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000226void ServerDialog::handleCancel(Fl_Widget *widget, void *data)
227{
228 ServerDialog *dialog = (ServerDialog*)data;
229
230 dialog->serverName->value(NULL);
231 dialog->hide();
232}
233
234
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000235void ServerDialog::handleConnect(Fl_Widget *widget, void *data)
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000236{
237 ServerDialog *dialog = (ServerDialog*)data;
Jan Grulicha5720e52018-11-20 10:44:39 +0100238 const char* servername = dialog->serverName->value();
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000239
240 dialog->hide();
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000241
242 try {
243 saveViewerParameters(NULL, servername);
244 } catch (rfb::Exception& e) {
245 fl_alert("%s", e.str());
246 }
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000247}