1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
* Copyright 2012 Samuel Mannehed <samuel@cendio.se> for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <algorithm>
#include <libgen.h>
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Input_Choice.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Return_Button.H>
#include <FL/fl_draw.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_File_Chooser.H>
#include <os/os.h>
#include <rfb/Exception.h>
#include <rfb/LogWriter.h>
#include <rfb/util.h>
#include "fltk/layout.h"
#include "fltk/util.h"
#include "ServerDialog.h"
#include "OptionsDialog.h"
#include "i18n.h"
#include "vncviewer.h"
#include "parameters.h"
using namespace std;
using namespace rfb;
static LogWriter vlog("ServerDialog");
const char* SERVER_HISTORY="tigervnc.history";
ServerDialog::ServerDialog()
: Fl_Window(450, 0, _("VNC Viewer: Connection Details"))
{
int x, y, x2;
Fl_Button *button;
Fl_Box *divider;
x = OUTER_MARGIN;
y = OUTER_MARGIN;
serverName = new Fl_Input_Choice(LBLLEFT(x, y, w() - OUTER_MARGIN*2,
INPUT_HEIGHT, _("VNC server:")));
// Bug fix for wrong background
serverName->color(FL_BACKGROUND2_COLOR);
y += INPUT_HEIGHT + INNER_MARGIN;
x2 = x;
button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Options..."));
button->callback(this->handleOptions, this);
x2 += BUTTON_WIDTH + INNER_MARGIN;
button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Load..."));
button->callback(this->handleLoad, this);
x2 += BUTTON_WIDTH + INNER_MARGIN;
button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Save As..."));
button->callback(this->handleSaveAs, this);
x2 += BUTTON_WIDTH + INNER_MARGIN;
y += BUTTON_HEIGHT + INNER_MARGIN;
divider = new Fl_Box(0, y, w(), 2);
divider->box(FL_THIN_DOWN_FRAME);
y += divider->h() + INNER_MARGIN;
// Symmetric margin around bottom button bar
y += OUTER_MARGIN - INNER_MARGIN;
button = new Fl_Button(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("About..."));
button->callback(this->handleAbout, this);
x2 = w() - OUTER_MARGIN - BUTTON_WIDTH*2 - INNER_MARGIN*1;
button = new Fl_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Cancel"));
button->callback(this->handleCancel, this);
x2 += BUTTON_WIDTH + INNER_MARGIN;
button = new Fl_Return_Button(x2, y, BUTTON_WIDTH, BUTTON_HEIGHT, _("Connect"));
button->callback(this->handleConnect, this);
x2 += BUTTON_WIDTH + INNER_MARGIN;
y += BUTTON_HEIGHT + INNER_MARGIN;
/* Needed for resize to work sanely */
resizable(nullptr);
h(y-INNER_MARGIN+OUTER_MARGIN);
callback(this->handleCancel, this);
}
ServerDialog::~ServerDialog()
{
}
void ServerDialog::run(const char* servername, char *newservername)
{
ServerDialog dialog;
dialog.serverName->value(servername);
dialog.show();
try {
dialog.loadServerHistory();
dialog.serverName->clear();
for (const string& entry : dialog.serverHistory)
fltk_menu_add(dialog.serverName->menubutton(),
entry.c_str(), 0, nullptr);
} catch (Exception& e) {
vlog.error("%s", e.str());
fl_alert(_("Unable to load the server history:\n\n%s"),
e.str());
}
while (dialog.shown()) Fl::wait();
if (dialog.serverName->value() == nullptr) {
newservername[0] = '\0';
return;
}
strncpy(newservername, dialog.serverName->value(), VNCSERVERNAMELEN);
newservername[VNCSERVERNAMELEN - 1] = '\0';
}
void ServerDialog::handleOptions(Fl_Widget* /*widget*/, void* /*data*/)
{
OptionsDialog::showDialog();
}
void ServerDialog::handleLoad(Fl_Widget* /*widget*/, void* data)
{
ServerDialog *dialog = (ServerDialog*)data;
if (dialog->usedDir.empty())
dialog->usedDir = os::getuserhomedir();
Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir.c_str(),
_("TigerVNC configuration (*.tigervnc)"),
0, _("Select a TigerVNC configuration file"));
file_chooser->preview(0);
file_chooser->previewButton->hide();
file_chooser->show();
// Block until user picks something.
while(file_chooser->shown())
Fl::wait();
// Did the user hit cancel?
if (file_chooser->value() == nullptr) {
delete(file_chooser);
return;
}
const char* filename = file_chooser->value();
dialog->updateUsedDir(filename);
try {
dialog->serverName->value(loadViewerParameters(filename));
} catch (Exception& e) {
vlog.error("%s", e.str());
fl_alert(_("Unable to load the specified configuration file:\n\n%s"),
e.str());
}
delete(file_chooser);
}
void ServerDialog::handleSaveAs(Fl_Widget* /*widget*/, void* data)
{
ServerDialog *dialog = (ServerDialog*)data;
const char* servername = dialog->serverName->value();
const char* filename;
if (dialog->usedDir.empty())
dialog->usedDir = os::getuserhomedir();
Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir.c_str(),
_("TigerVNC configuration (*.tigervnc)"),
2, _("Save the TigerVNC configuration to file"));
file_chooser->preview(0);
file_chooser->previewButton->hide();
file_chooser->show();
while(1) {
// Block until user picks something.
while(file_chooser->shown())
Fl::wait();
// Did the user hit cancel?
if (file_chooser->value() == nullptr) {
delete(file_chooser);
return;
}
filename = file_chooser->value();
dialog->updateUsedDir(filename);
FILE* f = fopen(filename, "r");
if (f) {
// The file already exists.
fclose(f);
int overwrite_choice = fl_choice(_("%s already exists. Do you want to overwrite?"),
_("Overwrite"), _("No"), nullptr, filename);
if (overwrite_choice == 1) {
// If the user doesn't want to overwrite:
file_chooser->show();
continue;
}
}
break;
}
try {
saveViewerParameters(filename, servername);
} catch (Exception& e) {
vlog.error("%s", e.str());
fl_alert(_("Unable to save the specified configuration "
"file:\n\n%s"), e.str());
}
delete(file_chooser);
}
void ServerDialog::handleAbout(Fl_Widget* /*widget*/, void* /*data*/)
{
about_vncviewer();
}
void ServerDialog::handleCancel(Fl_Widget* /*widget*/, void* data)
{
ServerDialog *dialog = (ServerDialog*)data;
dialog->serverName->value("");
dialog->hide();
}
void ServerDialog::handleConnect(Fl_Widget* /*widget*/, void *data)
{
ServerDialog *dialog = (ServerDialog*)data;
const char* servername = dialog->serverName->value();
dialog->hide();
try {
saveViewerParameters(nullptr, servername);
} catch (Exception& e) {
vlog.error("%s", e.str());
fl_alert(_("Unable to save the default configuration:\n\n%s"),
e.str());
}
try {
list<string>::iterator elem = std::find(dialog->serverHistory.begin(), dialog->serverHistory.end(), servername);
// avoid duplicates in the history
if(dialog->serverHistory.end() == elem) {
dialog->serverHistory.insert(dialog->serverHistory.begin(), servername);
dialog->saveServerHistory();
}
} catch (Exception& e) {
vlog.error("%s", e.str());
fl_alert(_("Unable to save the server history:\n\n%s"),
e.str());
}
}
void ServerDialog::loadServerHistory()
{
serverHistory.clear();
#ifdef _WIN32
serverHistory = loadHistoryFromRegKey();
return;
#endif
const char* stateDir = os::getvncstatedir();
if (stateDir == nullptr)
throw Exception(_("Could not determine VNC state directory path"));
char filepath[PATH_MAX];
snprintf(filepath, sizeof(filepath), "%s/%s", stateDir, SERVER_HISTORY);
/* Read server history from file */
FILE* f = fopen(filepath, "r");
if (!f) {
if (errno == ENOENT) {
// no history file
return;
}
std::string msg = format(_("Could not open \"%s\""), filepath);
throw rdr::SystemException(msg.c_str(), errno);
}
int lineNr = 0;
while (!feof(f)) {
char line[256];
// Read the next line
lineNr++;
if (!fgets(line, sizeof(line), f)) {
if (feof(f))
break;
fclose(f);
std::string msg = format(_("Failed to read line %d in "
"file \"%s\""), lineNr, filepath);
throw rdr::SystemException(msg.c_str(), errno);
}
int len = strlen(line);
if (len == (sizeof(line) - 1)) {
fclose(f);
throw Exception(_("Failed to read line %d in file %s: %s"),
lineNr, filepath, _("Line too long"));
}
if ((len > 0) && (line[len-1] == '\n')) {
line[len-1] = '\0';
len--;
}
if ((len > 0) && (line[len-1] == '\r')) {
line[len-1] = '\0';
len--;
}
if (len == 0)
continue;
serverHistory.push_back(line);
}
fclose(f);
}
void ServerDialog::saveServerHistory()
{
#ifdef _WIN32
saveHistoryToRegKey(serverHistory);
return;
#endif
const char* stateDir = os::getvncstatedir();
if (stateDir == nullptr)
throw Exception(_("Could not determine VNC state directory path"));
char filepath[PATH_MAX];
snprintf(filepath, sizeof(filepath), "%s/%s", stateDir, SERVER_HISTORY);
/* Write server history to file */
FILE* f = fopen(filepath, "w+");
if (!f) {
std::string msg = format(_("Could not open \"%s\""), filepath);
throw rdr::SystemException(msg.c_str(), errno);
}
// Save the last X elements to the config file.
size_t count = 0;
for (const string& entry : serverHistory) {
if (++count > SERVER_HISTORY_SIZE)
break;
fprintf(f, "%s\n", entry.c_str());
}
fclose(f);
}
void ServerDialog::updateUsedDir(const char* filename)
{
char * name = strdup(filename);
usedDir = dirname(name);
free(name);
}
|