Debian Codegraph @

src/vnc-connection.vala

raw
1 /* vnc-connection.vala
2 *
3 * Copyright (C) Red Hat, Inc
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Felipe Borges <[email protected]>
19 *
20 */
21
22 using Vnc;
23
24 namespace Connections {
25 private class VncConnection : Connection {
26 private Gtk.Window sidecar_vnc_window;
27 private Gtk.Clipboard clipboard;
28
29 private Vnc.Display display;
30 public override Gtk.Widget widget {
31 set {
32 display = value as Vnc.Display;
33 }
34
35 get {
36 if (sidecar_vnc_window.get_child () != null)
37 sidecar_vnc_window.remove (display);
38
39 return display as Gtk.Widget;
40 }
41 }
42
43 protected override Gdk.Pixbuf? thumbnail {
44 set {
45 return;
46 }
47
48 owned get {
49 return display.get_pixbuf ();
50 }
51 }
52
53 public override bool scaling {
54 set {
55 display.set_scaling (value);
56 }
57
58 get {
59 return display.get_scaling ();
60 }
61 }
62
63 public bool view_only {
64 set {
65 display.set_read_only (value);
66 }
67
68 get {
69 return display.get_read_only ();
70 }
71 }
72
73 public bool show_local_pointer {
74 set {
75 display.set_pointer_local (value);
76 }
77
78 get {
79 return display.get_pointer_local ();
80 }
81 }
82
83 private bool _enable_audio = true;
84 public bool enable_audio {
85 set {
86 var connection = display.get_connection ();
87 _enable_audio = value;
88
89 if (_enable_audio)
90 connection.audio_enable ();
91 else
92 connection.audio_disable ();
93 }
94
95 get {
96 return _enable_audio;
97 }
98 }
99
100 public override int port { get; protected set; default = 5900; }
101 public string bandwidth { get; set; default = "hight-quality"; }
102 public string scale_mode { get; set; default = "fit-window"; }
103
104 construct {
105 display = new Vnc.Display ();
106 display.set_keyboard_grab (true);
107 display.set_pointer_grab (true);
108 display.set_force_size (false);
109 display.set_keep_aspect_ratio (true);
110
111 dispose_display ();
112
113 clipboard = Gtk.Clipboard.get_default (Application.application.main_window.get_display ());
114
115 display.vnc_error.connect (on_connection_error_cb);
116 display.vnc_initialized.connect (() => { show (); });
117 display.vnc_auth_credential.connect (on_vnc_auth_credential_cb);
118 display.vnc_auth_failure.connect (on_vnc_auth_failure_cb);
119 display.size_allocate.connect (scale);
120 display.vnc_server_cut_text.connect (on_vnc_server_cut_text_cb);
121 clipboard.owner_change.connect (on_owner_change_cb);
122
123 notify["scale-mode"].connect (scale);
124
125 var connection = display.get_connection ();
126 connection.set_audio_format (new Vnc.AudioFormat () {
127 frequency = 44100,
128 nchannels = 2
129 });
130 connection.set_audio (new Vnc.AudioPulse ());
131 connection.audio_enable ();
132 authentication_complete.connect (update_display_authenticated);
133 }
134
135 public VncConnection (string uuid) {
136 this.uuid = uuid;
137 }
138
139 public VncConnection.from_uri (string uri) {
140 this.uuid = Uuid.string_random ();
141 this.uri = uri;
142 }
143
144 public VncConnection.from_vnc_file(string file_path) {
145 var key_file = new KeyFile();
146
147 try {
148 key_file.load_from_file (file_path, GLib.KeyFileFlags.NONE);
149 } catch (GLib.Error e) {
150 warning (_ ("Couldn’t parse the file"));
151 return;
152 }
153
154 try {
155 this.host = key_file.get_string ("Connection", "Host");
156 } catch (GLib.Error e) {
157 // Translators: %s is a VNC file key
158 info ( _("VNC File is missing key “%s”".printf ("Host")));
159 }
160 try {
161 this.port = key_file.get_integer ("Connection", "Port");
162 } catch (GLib.Error e) {
163 info ( _ ("VNC File is missing key “%s”".printf ("Port")));
164 }
165 try {
166 this.username = key_file.get_string ("Connection", "Username");
167 } catch (GLib.Error e) {
168 info ( _ ("VNC File is missing key “%s”".printf ("Username")));
169 }
170 try {
171 this.password = key_file.get_string ("Connection", "Password ");
172 } catch (GLib.Error e) {
173 info ( _ ("VNC File is missing key “%s”".printf ("Password")));
174 }
175
176 this.uuid = Uuid.string_random ();
177 }
178
179 ~VncConnection () {
180 debug ("Closing connection with %s", widget.name);
181
182 disconnect_it ();
183 }
184
185 private void update_display_authenticated () {
186 connect_it ();
187 }
188
189 public override void connect_it () {
190 if (connected) {
191 show ();
192
193 return;
194 }
195 connected = true;
196
197 display.set_credential (DisplayCredential.USERNAME, username);
198 display.set_credential (DisplayCredential.PASSWORD, password);
199 display.set_credential (DisplayCredential.CLIENTNAME, "connections");
200
201 display.open_host (host, port.to_string ());
202 }
203
204 public override void disconnect_it () {
205 if (display.is_open ())
206 display.close ();
207 connected = false;
208 }
209
210 public override void dispose_display () {
211 /* The Vnc.Display widget doesn't like not having a realized window.
212 * so we pack it on a temporary window for initialization and for
213 * when the connection persists but the display is not being presented. */
214 if (sidecar_vnc_window == null)
215 sidecar_vnc_window = new Gtk.Window ();
216
217 sidecar_vnc_window.add (display);
218 display.realize ();
219 }
220
221 public override void send_keys (uint[] keyvals) {
222 display.send_keys (keyvals);
223 }
224
225 private void on_vnc_auth_credential_cb (ValueArray creds) {
226 foreach (var cred in creds) {
227 var credential = (DisplayCredential) cred;
228
229 switch (credential) {
230 case DisplayCredential.USERNAME:
231 need_username = true;
232 handle_auth ();
233 break;
234
235 case DisplayCredential.PASSWORD:
236 need_password = true;
237 handle_auth ();
238 break;
239
240 case DisplayCredential.CLIENTNAME:
241 break;
242
243 default:
244 debug ("Unsupported credential: %s".printf (credential.to_string ()));
245 break;
246 }
247 }
248
249 disconnect_it ();
250 connected = false;
251 }
252
253 private void on_vnc_auth_failure_cb (string reason) {
254 debug ("Failed to authenticate %s", reason);
255
256 auth_failed (reason);
257 }
258
259 private void on_vnc_server_cut_text_cb (string message) {
260 if (view_only)
261 return;
262
263 clipboard.set_text (message, message.length);
264 clipboard.store ();
265 }
266
267 private void on_owner_change_cb () {
268 if (!connected || view_only)
269 return;
270
271 string message = clipboard.wait_for_text ();
272 if (message != null)
273 display.client_cut_text (message);
274 }
275
276 public void scale () {
277 if (!display.is_open ())
278 return;
279
280 display.scaling = display.expand = false;
281
282 switch (scale_mode) {
283 case "resize-desktop":
284 resize_desktop_to_window ();
285 break;
286 case "fit-window":
287 scale_to_fit_window ();
288 break;
289 case "original":
290 scale_to_original_size ();
291 break;
292 }
293 }
294
295 private void resize_desktop_to_window () {
296 display.set_allow_resize (true);
297 }
298
299 private void scale_to_fit_window () {
300 display.scaling = display.hexpand = true;
301 display.width_request = display.height_request = 0;
302 display.set_allow_resize (false);
303 }
304
305 private void scale_to_original_size () {
306 display.width_request = display.width;
307 display.height_request = display.height;
308 display.set_allow_resize (false);
309 }
310 }
311 }