Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 1 | /* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 2 | * Copyright 2012 Samuel Mannehed <samuel@cendio.se> for Cendio AB |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 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 | |
Peter Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 24 | #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 Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 29 | #include <FL/fl_ask.H> |
| 30 | #include <FL/Fl_Box.H> |
| 31 | #include <FL/Fl_File_Chooser.H> |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 32 | |
| 33 | #include "ServerDialog.h" |
Pierre Ossman | d463b57 | 2011-05-16 12:04:43 +0000 | [diff] [blame] | 34 | #include "OptionsDialog.h" |
| 35 | #include "fltk_layout.h" |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 36 | #include "i18n.h" |
Pierre Ossman | 39ceb50 | 2011-07-12 15:54:25 +0000 | [diff] [blame] | 37 | #include "vncviewer.h" |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 38 | #include "parameters.h" |
| 39 | #include "rfb/Exception.h" |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 40 | |
| 41 | ServerDialog::ServerDialog() |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 42 | : Fl_Window(450, 160, _("VNC Viewer: Connection Details")) |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 43 | { |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 44 | int x, y; |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 45 | Fl_Button *button; |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 46 | Fl_Box *divider; |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 47 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 48 | int margin = 20; |
| 49 | int server_label_width = gui_str_len(_("VNC server:")); |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 50 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 51 | 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 Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 55 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 56 | 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 Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 87 | button->callback(this->handleAbout, this); |
| 88 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 89 | x = w() - margin - adjust - button_width - 20; |
Pierre Ossman | d463b57 | 2011-05-16 12:04:43 +0000 | [diff] [blame] | 90 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 91 | button = new Fl_Button(x, y, button_width, BUTTON_HEIGHT, _("Cancel")); |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 92 | button->callback(this->handleCancel, this); |
| 93 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 94 | x += adjust; |
| 95 | |
| 96 | button = new Fl_Return_Button(x, y, button_width+20, BUTTON_HEIGHT, _("Connect")); |
| 97 | button->callback(this->handleConnect, this); |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 98 | |
| 99 | callback(this->handleCancel, this); |
| 100 | |
| 101 | set_modal(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | ServerDialog::~ServerDialog() |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame^] | 110 | void ServerDialog::run(const char* servername, char *newservername) |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 111 | { |
| 112 | ServerDialog dialog; |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 113 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 114 | dialog.serverName->value(servername); |
| 115 | |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 116 | dialog.show(); |
| 117 | while (dialog.shown()) Fl::wait(); |
| 118 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame^] | 119 | if (dialog.serverName->value() == NULL) { |
| 120 | newservername[0] = '\0'; |
| 121 | return; |
| 122 | } |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 123 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame^] | 124 | strncpy(newservername, dialog.serverName->value(), VNCSERVERNAMELEN); |
| 125 | newservername[VNCSERVERNAMELEN - 1] = '\0'; |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 128 | void ServerDialog::handleOptions(Fl_Widget *widget, void *data) |
| 129 | { |
| 130 | OptionsDialog::showDialog(); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void ServerDialog::handleLoad(Fl_Widget *widget, void *data) |
| 135 | { |
| 136 | ServerDialog *dialog = (ServerDialog*)data; |
| 137 | Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", "TigerVNC configuration (*.tigervnc)", |
| 138 | 0, "Select a TigerVNC configuration file"); |
| 139 | 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 | |
| 153 | const char* filename = strdup(file_chooser->value()); |
| 154 | |
| 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 | |
| 165 | void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data) |
| 166 | { |
| 167 | ServerDialog *dialog = (ServerDialog*)data; |
| 168 | const char* servername = strdup(dialog->serverName->value()); |
| 169 | char* filename; |
| 170 | |
| 171 | Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", "TigerVNC configuration (*.tigervnc)", |
| 172 | 2, "Save the TigerVNC configuration to file"); |
| 173 | |
| 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 | |
| 190 | filename = strdup(file_chooser->value()); |
| 191 | |
| 192 | FILE* f = fopen(filename, "r"); |
| 193 | if (f) { |
| 194 | |
| 195 | // The file already exists. |
| 196 | fclose(f); |
| 197 | int overwrite_choice = fl_choice("%s already exists. Do you want to overwrite?", |
| 198 | "Overwrite", "No", NULL, filename); |
| 199 | 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 Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 219 | |
| 220 | void ServerDialog::handleAbout(Fl_Widget *widget, void *data) |
| 221 | { |
| 222 | about_vncviewer(); |
| 223 | } |
| 224 | |
| 225 | |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 226 | void 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 Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 235 | void ServerDialog::handleConnect(Fl_Widget *widget, void *data) |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 236 | { |
| 237 | ServerDialog *dialog = (ServerDialog*)data; |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 238 | const char* servername = strdup(dialog->serverName->value()); |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 239 | |
| 240 | dialog->hide(); |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 241 | |
| 242 | try { |
| 243 | saveViewerParameters(NULL, servername); |
| 244 | } catch (rfb::Exception& e) { |
| 245 | fl_alert("%s", e.str()); |
| 246 | } |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 247 | } |