| 1 |
/* rdp-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 |
namespace Connections { |
| 23 |
private class RdpConnection : Connection { |
| 24 |
private FrdpDisplay display; |
| 25 |
public override Gtk.Widget widget { |
| 26 |
set { |
| 27 |
display = value as FrdpDisplay; |
| 28 |
} |
| 29 |
|
| 30 |
get { |
| 31 |
return display as Gtk.Widget; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
protected override Gdk.Pixbuf? thumbnail { |
| 36 |
set { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
owned get { |
| 41 |
Gtk.Allocation alloc; |
| 42 |
widget.get_allocation (out alloc); |
| 43 |
|
| 44 |
var surface = new Cairo.ImageSurface (ARGB32, alloc.width, alloc.height); |
| 45 |
var context = new Cairo.Context (surface); |
| 46 |
widget.draw (context); |
| 47 |
|
| 48 |
return Gdk.pixbuf_get_from_surface (surface, 0, 0, alloc.width, alloc.height); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public override bool scaling { |
| 53 |
set { |
| 54 |
display.set_scaling (value); |
| 55 |
} |
| 56 |
|
| 57 |
get { |
| 58 |
if (!connected) |
| 59 |
return true; |
| 60 |
|
| 61 |
return display.scaling; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public override int port { get; protected set; default = 3389; } |
| 66 |
public bool resize_supported { get; protected set; default = false; } |
| 67 |
public string scale_mode { get; set; default = "fit-window"; } |
| 68 |
protected bool connecting = false; |
| 69 |
|
| 70 |
construct { |
| 71 |
display = new FrdpDisplay (); |
| 72 |
display.hexpand = display.vexpand = true; |
| 73 |
display.bind_property ("username", this, "username", BindingFlags.BIDIRECTIONAL); |
| 74 |
display.bind_property ("password", this, "password", BindingFlags.BIDIRECTIONAL); |
| 75 |
|
| 76 |
display.rdp_error.connect (on_rdp_connection_error_cb); |
| 77 |
display.rdp_connected.connect (on_rdp_connection_connected_cb); |
| 78 |
display.rdp_disconnected.connect (on_rdp_connection_disconnected_cb); |
| 79 |
display.rdp_needs_authentication.connect (on_rdp_auth_credential_cb); |
| 80 |
display.rdp_needs_certificate_verification.connect (on_rdp_certificate_verification_cb); |
| 81 |
display.rdp_needs_certificate_change_verification.connect (on_rdp_certificate_change_verification_cb); |
| 82 |
display.rdp_auth_failure.connect (auth_failed); |
| 83 |
display.size_allocate.connect (scale); |
| 84 |
|
| 85 |
notify["scale-mode"].connect (scale); |
| 86 |
display.notify["resize-supported"].connect (update_resize_supported); |
| 87 |
|
| 88 |
need_username = need_password = need_domain = true; |
| 89 |
|
| 90 |
certificate_verified = false; |
| 91 |
certificate_verification_complete.connect (update_display_verified); |
| 92 |
certificate_change_verified = false; |
| 93 |
certificate_change_verification_complete.connect (update_display_change_verified); |
| 94 |
authentication_complete.connect (update_display_authenticated); |
| 95 |
} |
| 96 |
|
| 97 |
private void update_display_verified () { |
| 98 |
display.certificate_verify_ex_finish (certificate_verified ? 1 : 0); |
| 99 |
} |
| 100 |
|
| 101 |
private void update_display_change_verified () { |
| 102 |
display.certificate_change_verify_ex_finish (certificate_change_verified ? 1 : 0); |
| 103 |
} |
| 104 |
|
| 105 |
private void update_display_authenticated () { |
| 106 |
display.authenticate_finish (username, password, domain); |
| 107 |
} |
| 108 |
|
| 109 |
public RdpConnection (string uuid) { |
| 110 |
this.uuid = uuid; |
| 111 |
} |
| 112 |
|
| 113 |
public RdpConnection.from_uri (string uri) { |
| 114 |
this.uuid = Uuid.string_random (); |
| 115 |
this.uri = uri; |
| 116 |
} |
| 117 |
|
| 118 |
~RdpConnection () { |
| 119 |
debug ("Closing connection with %s", widget.name); |
| 120 |
|
| 121 |
disconnect_it (); |
| 122 |
} |
| 123 |
|
| 124 |
public override void connect_it () { |
| 125 |
if (connecting) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
connecting = true; |
| 130 |
|
| 131 |
if (connected) { |
| 132 |
show (); |
| 133 |
|
| 134 |
connecting = false; |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
display.open_host (host, port); |
| 139 |
|
| 140 |
connecting = false; |
| 141 |
} |
| 142 |
|
| 143 |
public override void disconnect_it () { |
| 144 |
if (connected) { |
| 145 |
display.close (); |
| 146 |
connected = false; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public override void dispose_display () { |
| 151 |
} |
| 152 |
|
| 153 |
public override void send_keys (uint[] keyvals) { |
| 154 |
} |
| 155 |
|
| 156 |
private void on_rdp_auth_credential_cb () { |
| 157 |
handle_auth (); |
| 158 |
} |
| 159 |
|
| 160 |
private void on_rdp_certificate_verification_cb (string host, |
| 161 |
uint port, |
| 162 |
string? common_name, |
| 163 |
string subject, |
| 164 |
string issuer, |
| 165 |
string fingerprint, |
| 166 |
uint flags) { |
| 167 |
handle_certificate_verification (subject, issuer, fingerprint); |
| 168 |
} |
| 169 |
|
| 170 |
private void on_rdp_certificate_change_verification_cb (string host, |
| 171 |
uint port, |
| 172 |
string? common_name, |
| 173 |
string new_subject, |
| 174 |
string new_issuer, |
| 175 |
string new_fingerprint, |
| 176 |
string old_subject, |
| 177 |
string old_issuer, |
| 178 |
string old_fingerprint, |
| 179 |
uint flags) { |
| 180 |
handle_certificate_change_verification (new_subject, |
| 181 |
new_issuer, |
| 182 |
new_fingerprint, |
| 183 |
old_subject, |
| 184 |
old_issuer, |
| 185 |
old_fingerprint); |
| 186 |
} |
| 187 |
|
| 188 |
private void on_rdp_connection_error_cb (string reason) { |
| 189 |
disconnect_it (); |
| 190 |
|
| 191 |
delete_auth_credentials.begin (); |
| 192 |
username = password = null; |
| 193 |
|
| 194 |
on_connection_error_cb (reason); |
| 195 |
} |
| 196 |
|
| 197 |
private void on_rdp_connection_connected_cb () { |
| 198 |
connected = true; |
| 199 |
scaling = true; |
| 200 |
|
| 201 |
display.grab_focus (); |
| 202 |
show (); |
| 203 |
} |
| 204 |
|
| 205 |
private void on_rdp_connection_disconnected_cb () { |
| 206 |
Application.application.main_window.show_collection_view (); |
| 207 |
connected = false; |
| 208 |
} |
| 209 |
|
| 210 |
public void scale () { |
| 211 |
if (!display.is_open ()) |
| 212 |
return; |
| 213 |
|
| 214 |
display.scaling = display.expand = false; |
| 215 |
|
| 216 |
switch (scale_mode) { |
| 217 |
case "resize-desktop": |
| 218 |
resize_desktop_to_window (); |
| 219 |
break; |
| 220 |
case "fit-window": |
| 221 |
scale_to_fit_window (); |
| 222 |
break; |
| 223 |
case "original": |
| 224 |
scale_to_original_size (); |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
private void resize_desktop_to_window () { |
| 230 |
display.allow_resize = true; |
| 231 |
} |
| 232 |
|
| 233 |
private void scale_to_fit_window () { |
| 234 |
display.scaling = display.hexpand = true; |
| 235 |
display.width_request = display.height_request = 0; |
| 236 |
display.allow_resize = false; |
| 237 |
} |
| 238 |
|
| 239 |
private void scale_to_original_size () { |
| 240 |
display.allow_resize = false; |
| 241 |
} |
| 242 |
|
| 243 |
private void update_resize_supported () { |
| 244 |
resize_supported = display.resize_supported; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
private class FrdpDisplay : Frdp.Display { |
| 249 |
} |
| 250 |
} |