blob: 25a010a18afc7951fa323f7daf031ebbd1677de8 [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
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000110const char *ServerDialog::run(const char* servername)
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000111{
112 ServerDialog dialog;
113 static char buffer[256];
114
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000115 dialog.serverName->value(servername);
116
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000117 dialog.show();
118 while (dialog.shown()) Fl::wait();
119
120 if (dialog.serverName->value() == NULL)
121 return NULL;
122
123 strncpy(buffer, dialog.serverName->value(), sizeof(buffer));
124 buffer[sizeof(buffer)-1] = '\0';
125
126 return buffer;
127}
128
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000129void ServerDialog::handleOptions(Fl_Widget *widget, void *data)
130{
131 OptionsDialog::showDialog();
132}
133
134
135void ServerDialog::handleLoad(Fl_Widget *widget, void *data)
136{
137 ServerDialog *dialog = (ServerDialog*)data;
138 Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", "TigerVNC configuration (*.tigervnc)",
139 0, "Select a TigerVNC configuration file");
140 file_chooser->preview(0);
141 file_chooser->previewButton->hide();
142 file_chooser->show();
143
144 // Block until user picks something.
145 while(file_chooser->shown())
146 Fl::wait();
147
148 // Did the user hit cancel?
149 if (file_chooser->value() == NULL) {
150 delete(file_chooser);
151 return;
152 }
153
154 const char* filename = strdup(file_chooser->value());
155
156 try {
157 dialog->serverName->value(loadViewerParameters(filename));
158 } catch (rfb::Exception& e) {
159 fl_alert("%s", e.str());
160 }
161
162 delete(file_chooser);
163}
164
165
166void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
167{
168 ServerDialog *dialog = (ServerDialog*)data;
169 const char* servername = strdup(dialog->serverName->value());
170 char* filename;
171
172 Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", "TigerVNC configuration (*.tigervnc)",
173 2, "Save the TigerVNC configuration to file");
174
175 file_chooser->preview(0);
176 file_chooser->previewButton->hide();
177 file_chooser->show();
178
179 while(1) {
180
181 // Block until user picks something.
182 while(file_chooser->shown())
183 Fl::wait();
184
185 // Did the user hit cancel?
186 if (file_chooser->value() == NULL) {
187 delete(file_chooser);
188 return;
189 }
190
191 filename = strdup(file_chooser->value());
192
193 FILE* f = fopen(filename, "r");
194 if (f) {
195
196 // The file already exists.
197 fclose(f);
198 int overwrite_choice = fl_choice("%s already exists. Do you want to overwrite?",
199 "Overwrite", "No", NULL, filename);
200 if (overwrite_choice == 1) {
201
202 // If the user doesn't want to overwrite:
203 file_chooser->show();
204 continue;
205 }
206 }
207
208 break;
209 }
210
211 try {
212 saveViewerParameters(filename, servername);
213 } catch (rfb::Exception& e) {
214 fl_alert("%s", e.str());
215 }
216
217 delete(file_chooser);
218}
219
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000220
221void ServerDialog::handleAbout(Fl_Widget *widget, void *data)
222{
223 about_vncviewer();
224}
225
226
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000227void ServerDialog::handleCancel(Fl_Widget *widget, void *data)
228{
229 ServerDialog *dialog = (ServerDialog*)data;
230
231 dialog->serverName->value(NULL);
232 dialog->hide();
233}
234
235
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000236void ServerDialog::handleConnect(Fl_Widget *widget, void *data)
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000237{
238 ServerDialog *dialog = (ServerDialog*)data;
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000239 const char* servername = strdup(dialog->serverName->value());
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000240
241 dialog->hide();
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000242
243 try {
244 saveViewerParameters(NULL, servername);
245 } catch (rfb::Exception& e) {
246 fl_alert("%s", e.str());
247 }
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000248}