| 1 |
/* 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 abstract class Connection : GLib.Object { |
| 24 |
public string uuid; |
| 25 |
|
| 26 |
public signal void show (); |
| 27 |
|
| 28 |
public abstract Gtk.Widget widget { get; protected set; } |
| 29 |
public abstract bool scaling { get; set; } |
| 30 |
|
| 31 |
private Cancellable auth_cancellable = new Cancellable (); |
| 32 |
private Secret.Schema secret_auth_schema |
| 33 |
= new Secret.Schema ("org.gnome.Connections", |
| 34 |
Secret.SchemaFlags.NONE, |
| 35 |
"gnome-connections-connection-uuid", Secret.SchemaAttributeType.STRING); |
| 36 |
|
| 37 |
public bool connected; |
| 38 |
|
| 39 |
public string uri { |
| 40 |
owned get { |
| 41 |
return GLib.Uri.join (UriFlags.NONE, protocol, null, host, port, "", null, null); |
| 42 |
} |
| 43 |
|
| 44 |
set { |
| 45 |
try { |
| 46 |
var address = GLib.Uri.parse (value, UriFlags.NONE); |
| 47 |
protocol = address.get_scheme (); |
| 48 |
host = address.get_host (); |
| 49 |
port = (address.get_port () != -1) ? address.get_port () : port; |
| 50 |
} catch (UriError e) { |
| 51 |
warning ("Failed to parse URI: %s (%s)", value, e.message); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public string host { get; protected set; } |
| 57 |
public abstract int port { get; protected set; } |
| 58 |
public string protocol { get; protected set; } |
| 59 |
public string display_name { get; set; default = ""; } |
| 60 |
|
| 61 |
public abstract void send_keys (uint[] keys); |
| 62 |
|
| 63 |
public abstract Gdk.Pixbuf? thumbnail { set; owned get; } |
| 64 |
|
| 65 |
public async void take_screenshot () { |
| 66 |
if (thumbnail == null) |
| 67 |
return; |
| 68 |
|
| 69 |
try { |
| 70 |
var timestamp = new GLib.DateTime.now_local ().format ("%Y-%m-%d %H-%M-%S"); |
| 71 |
|
| 72 |
// Translators: %s => the timestamp of when the screenshot was taken. |
| 73 |
var filename = _("Screenshot from %s").printf (timestamp); |
| 74 |
var path = Path.build_filename (Environment.get_user_special_dir (GLib.UserDirectory.PICTURES), |
| 75 |
filename); |
| 76 |
thumbnail.save (path + ".png", "png"); |
| 77 |
|
| 78 |
Notification.OKFunc open = () => { |
| 79 |
debug ("Opening screenshot file"); |
| 80 |
try { |
| 81 |
Gtk.show_uri_on_window (Application.application.main_window, |
| 82 |
File.new_for_path (path).get_uri () + ".png", |
| 83 |
Gdk.CURRENT_TIME); |
| 84 |
} catch (GLib.Error error) { |
| 85 |
warning ("Failed to open screenshot: %s", error.message); |
| 86 |
} |
| 87 |
}; |
| 88 |
var message = _("Screenshot taken"); |
| 89 |
// Translators: Open is a verb |
| 90 |
Application.application.main_window.notifications_bar.display_for_action (message, |
| 91 |
_("Open"), |
| 92 |
(owned) open, |
| 93 |
null); |
| 94 |
} catch (GLib.Error error) { |
| 95 |
warning ("Failed to take screenshot %s", error.message); |
| 96 |
} |
| 97 |
|
| 98 |
flash_display (); |
| 99 |
} |
| 100 |
|
| 101 |
private void flash_display () { |
| 102 |
var style_context = widget.get_parent ().get_style_context (); |
| 103 |
style_context.add_class ("flash"); |
| 104 |
Timeout.add (200, () => { |
| 105 |
style_context.remove_class ("flash"); |
| 106 |
|
| 107 |
return false; |
| 108 |
}); |
| 109 |
} |
| 110 |
|
| 111 |
public bool need_password; |
| 112 |
public bool need_username; |
| 113 |
public bool need_domain; |
| 114 |
public string? username { get; set; } |
| 115 |
public string? password { get; set; } |
| 116 |
public string? domain { get; set; } |
| 117 |
|
| 118 |
protected void handle_auth () { |
| 119 |
if (!need_username && !need_password) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
DialogsWindow.AuthenticationFunc authentication_func = (username, password, domain) => { |
| 124 |
if (username != "") |
| 125 |
this.username = username; |
| 126 |
if (password != "") |
| 127 |
this.password = password; |
| 128 |
if (domain != "") |
| 129 |
this.domain = domain; |
| 130 |
|
| 131 |
Application.application.open_connection (this); |
| 132 |
|
| 133 |
authentication_complete (); |
| 134 |
}; |
| 135 |
|
| 136 |
if (!((need_username && this.username == null) || |
| 137 |
(need_password && this.password == null) || |
| 138 |
(need_domain && this.domain == null))) { |
| 139 |
authentication_complete (); |
| 140 |
} else { |
| 141 |
Secret.password_lookup.begin (secret_auth_schema, auth_cancellable, (obj, res) => { |
| 142 |
try { |
| 143 |
var parsing_error = new IOError.FAILED("couldn't unpack a string for the connection credentials"); |
| 144 |
|
| 145 |
var credentials_str = Secret.password_lookup.end (res); |
| 146 |
if (credentials_str == null || credentials_str == "") |
| 147 |
throw parsing_error; |
| 148 |
|
| 149 |
try { |
| 150 |
var credentials_variant = GLib.Variant.parse (null, credentials_str, null, null); |
| 151 |
|
| 152 |
string username_str; |
| 153 |
credentials_variant.lookup ("username", "s", out username_str); |
| 154 |
if (username_str != null && username_str != "") |
| 155 |
this.username = username_str; |
| 156 |
|
| 157 |
string password_str; |
| 158 |
credentials_variant.lookup ("password", "s", out password_str); |
| 159 |
if (password_str != null && password_str != "") |
| 160 |
this.password = password_str; |
| 161 |
|
| 162 |
string domain_str; |
| 163 |
credentials_variant.lookup ("domain", "s", out domain_str); |
| 164 |
if (domain_str != null && domain_str != "") |
| 165 |
this.domain = domain_str; |
| 166 |
|
| 167 |
authentication_complete (); |
| 168 |
} catch (GLib.Error error) { |
| 169 |
throw parsing_error; |
| 170 |
} |
| 171 |
} catch (GLib.Error error) { |
| 172 |
debug ("No credentials found in keyring. Prompting user."); |
| 173 |
|
| 174 |
Application.application.main_window.dialogs_window.show_authentication (get_visible_name (), |
| 175 |
need_username, |
| 176 |
need_password, |
| 177 |
need_domain, |
| 178 |
(owned) authentication_func); |
| 179 |
} |
| 180 |
}, "gnome-connections-connection-uuid", uuid); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
public async void delete_auth_credentials () { |
| 185 |
if (uuid == null) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
try { |
| 190 |
yield Secret.password_clear (secret_auth_schema, null, |
| 191 |
"gnome-connections-connection-uuid", uuid); |
| 192 |
} catch (GLib.Error error) { |
| 193 |
debug ("Failed to delete credentials for connection %s: %s", uuid, error.message); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
protected void on_connection_error_cb (string reason) { |
| 198 |
Application.application.main_window.notifications_bar.display_for_error (reason); |
| 199 |
Application.application.main_window.show_collection_view (); |
| 200 |
} |
| 201 |
|
| 202 |
protected void auth_failed (string reason) { |
| 203 |
disconnect_it (); |
| 204 |
|
| 205 |
delete_auth_credentials.begin (); |
| 206 |
username = password = domain = null; |
| 207 |
|
| 208 |
var message = _("Authentication failed: %s").printf (reason); |
| 209 |
Application.application.main_window.show_collection_view (); |
| 210 |
Application.application.main_window.notifications_bar.display_for_error (message); |
| 211 |
} |
| 212 |
|
| 213 |
construct { |
| 214 |
notify.connect (save); |
| 215 |
} |
| 216 |
|
| 217 |
public void save (GLib.ParamSpec? pspec = null) { |
| 218 |
if (uuid != null && pspec != null) { |
| 219 |
/* Don't save the credentials in plain text */ |
| 220 |
if (pspec.name == "username" || pspec.name == "password" || pspec.name == "domain") { |
| 221 |
store_auth_credentials (); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
Database.get_default ().save_property (this, pspec.name); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
private void store_auth_credentials () { |
| 230 |
if (this.password == "" || this.password == null) |
| 231 |
return; |
| 232 |
|
| 233 |
var builder = new GLib.VariantBuilder (GLib.VariantType.VARDICT); |
| 234 |
|
| 235 |
if (this.username != null) |
| 236 |
builder.add ("{sv}", "username", new GLib.Variant ("s", this.username)); |
| 237 |
|
| 238 |
if (this.domain != null) |
| 239 |
builder.add ("{sv}", "domain", new GLib.Variant ("s", this.domain)); |
| 240 |
|
| 241 |
builder.add ("{sv}", "password", new GLib.Variant ("s", this.password)); |
| 242 |
|
| 243 |
var credentials_str = builder.end ().print (true); |
| 244 |
|
| 245 |
var label = ("GNOME Connections credentials for '%s'").printf (uuid); |
| 246 |
Secret.password_store.begin (secret_auth_schema, |
| 247 |
Secret.COLLECTION_DEFAULT, |
| 248 |
label, |
| 249 |
credentials_str, |
| 250 |
null, |
| 251 |
(obj, res) => { |
| 252 |
try { |
| 253 |
Secret.password_store.end (res); |
| 254 |
} catch (GLib.Error error) { |
| 255 |
warning ("Failed to store password for '%s' in the keyring: %s", uuid, error.message); |
| 256 |
} |
| 257 |
}, "gnome-connections-connection-uuid", uuid); |
| 258 |
} |
| 259 |
|
| 260 |
public bool certificate_verified { get; set; } |
| 261 |
public signal void certificate_verification_complete (); |
| 262 |
protected void handle_certificate_verification (string subject, |
| 263 |
string issuer, |
| 264 |
string fingerprint) { |
| 265 |
DialogsWindow.CertificateVerifyFunc certificate_verify_func = (verified) => { |
| 266 |
certificate_verified = verified; |
| 267 |
certificate_verification_complete (); |
| 268 |
}; |
| 269 |
|
| 270 |
Application.application.main_window.dialogs_window.show_certificate_verification (get_visible_name (), |
| 271 |
fingerprint, |
| 272 |
(owned) certificate_verify_func); |
| 273 |
} |
| 274 |
|
| 275 |
public bool certificate_change_verified { get; set; } |
| 276 |
public signal void certificate_change_verification_complete (); |
| 277 |
protected void handle_certificate_change_verification (string new_subject, |
| 278 |
string new_issuer, |
| 279 |
string new_fingerprint, |
| 280 |
string old_subject, |
| 281 |
string old_issuer, |
| 282 |
string old_fingerprint) { |
| 283 |
DialogsWindow.CertificateChangeVerifyFunc certificate_change_verify_func = (verified) => { |
| 284 |
certificate_change_verified = verified; |
| 285 |
certificate_change_verification_complete (); |
| 286 |
}; |
| 287 |
|
| 288 |
Application.application.main_window.dialogs_window.show_certificate_change_verification (get_visible_name (), |
| 289 |
new_fingerprint, |
| 290 |
(owned) certificate_change_verify_func); |
| 291 |
} |
| 292 |
|
| 293 |
public signal void authentication_complete (); |
| 294 |
|
| 295 |
public string get_visible_name () { |
| 296 |
if (display_name != null && display_name != "") |
| 297 |
return display_name; |
| 298 |
|
| 299 |
return uri; |
| 300 |
} |
| 301 |
|
| 302 |
public abstract void connect_it (); |
| 303 |
public abstract void disconnect_it (); |
| 304 |
|
| 305 |
public abstract void dispose_display (); |
| 306 |
} |
| 307 |
} |