2018-05-30 19:49:16 +00:00
|
|
|
public class Tootle.Desktop {
|
2018-06-06 14:19:11 +00:00
|
|
|
|
2018-06-07 11:43:55 +00:00
|
|
|
// Open URI in the user's default application associated with it
|
2019-03-11 12:28:51 +00:00
|
|
|
public static bool open_uri (string uri) {
|
2018-06-07 11:43:55 +00:00
|
|
|
try {
|
|
|
|
Gtk.show_uri (null, uri, Gdk.CURRENT_TIME);
|
|
|
|
}
|
|
|
|
catch (GLib.Error e){
|
2019-09-16 14:11:39 +00:00
|
|
|
try {
|
|
|
|
string[] spawn_args = {"/usr/bin/xdg-open", uri};
|
|
|
|
Process.spawn_sync (null, spawn_args, null, SpawnFlags.SEARCH_PATH, null, null, null);
|
|
|
|
}
|
|
|
|
catch (GLib.Error e){
|
|
|
|
warning ("Can't open %s: %s", uri, e.message);
|
|
|
|
if (e.message == "Operation not supported") {
|
|
|
|
app.error (_("Open this in a web browser:\n\n"+uri),"");
|
|
|
|
} else {
|
|
|
|
app.error (_("Error"), e.message);
|
|
|
|
}
|
|
|
|
}
|
2018-06-07 11:43:55 +00:00
|
|
|
}
|
2019-03-11 12:28:51 +00:00
|
|
|
return true;
|
2018-05-04 20:57:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 14:19:11 +00:00
|
|
|
// Copy a string to the clipboard
|
2018-05-21 15:23:31 +00:00
|
|
|
public static void copy (string str) {
|
2018-06-06 14:19:11 +00:00
|
|
|
var display = window.get_display ();
|
2018-05-21 15:23:31 +00:00
|
|
|
var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
|
2019-03-11 14:14:37 +00:00
|
|
|
clipboard.set_text (Widgets.RichLabel.restore_entities (str), -1);
|
2018-05-21 10:40:49 +00:00
|
|
|
}
|
2018-05-30 19:49:16 +00:00
|
|
|
|
2018-06-06 14:19:11 +00:00
|
|
|
// Download a file from the web to a user's configured Downloads folder
|
2020-05-29 12:19:35 +00:00
|
|
|
public delegate void DownloadCallback (string path);
|
|
|
|
public static void download (string url, DownloadCallback? cb = null, Network.ErrorCallback? ecb = null) {
|
|
|
|
info (@"Downloading file: $url...");
|
2019-03-09 11:42:27 +00:00
|
|
|
|
2018-05-21 16:13:49 +00:00
|
|
|
var i = url.last_index_of ("/");
|
|
|
|
var name = url.substring (i + 1, url.length - i - 1);
|
|
|
|
if (name == null)
|
2020-05-29 12:19:35 +00:00
|
|
|
name = _("Unknown Attachment");
|
2019-03-09 11:42:27 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
var downloads = GLib.Environment.get_user_special_dir (UserDirectory.DOWNLOAD);
|
|
|
|
var dir_path = @"$downloads/$(Build.NAME)";
|
|
|
|
var file_path = @"$dir_path/$name";
|
2019-03-09 11:42:27 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
new Request.GET (url)
|
|
|
|
.then ((sess, msg) => {
|
|
|
|
try {
|
|
|
|
var dir = File.new_for_path (dir_path);
|
|
|
|
if (!dir.query_exists ())
|
|
|
|
dir.make_directory ();
|
2018-05-21 16:13:49 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
var file = File.new_for_path (file_path);
|
|
|
|
if (!file.query_exists ()) {
|
|
|
|
var data = msg.response_body.data;
|
|
|
|
FileOutputStream stream = file.create (FileCreateFlags.PRIVATE);
|
|
|
|
stream.write (data);
|
|
|
|
}
|
|
|
|
info ("OK");
|
|
|
|
cb (file_path);
|
2020-05-31 10:28:35 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
} catch (Error e) {
|
|
|
|
warning ("Error: %s\n", e.message);
|
|
|
|
ecb (0, e.message);
|
2018-05-21 16:13:49 +00:00
|
|
|
}
|
2020-05-29 12:19:35 +00:00
|
|
|
})
|
|
|
|
.on_error ((code, reason) => ecb)
|
|
|
|
.exec ();
|
2018-05-21 16:13:49 +00:00
|
|
|
}
|
2019-03-09 11:42:27 +00:00
|
|
|
|
2020-05-31 10:28:35 +00:00
|
|
|
public static string fallback_icon (string normal, string fallback, string fallback2 = "broken") {
|
2018-06-17 08:40:24 +00:00
|
|
|
var theme = Gtk.IconTheme.get_default ();
|
2020-05-31 10:28:35 +00:00
|
|
|
if (theme.has_icon (normal))
|
|
|
|
return normal;
|
|
|
|
else
|
|
|
|
return theme.has_icon (fallback) ? fallback : fallback2;
|
2018-06-17 08:40:24 +00:00
|
|
|
}
|
2019-03-09 11:42:27 +00:00
|
|
|
|
|
|
|
public static void set_hotkey_tooltip (Gtk.Widget widget, string? description, string[] accelerators) {
|
|
|
|
widget.tooltip_markup = Granite.markup_accel_tooltip (accelerators, description);
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:16:57 +00:00
|
|
|
}
|