tootle-linux-client/src/Desktop.vala

89 lines
3.2 KiB
Vala
Raw Normal View History

public class Tootle.Desktop {
2018-06-06 16:19:11 +02:00
2018-06-07 13:43:55 +02:00
// Open URI in the user's default application associated with it
2019-03-11 13:28:51 +01:00
public static bool open_uri (string uri) {
2018-06-07 13:43:55 +02:00
try {
Gtk.show_uri (null, uri, Gdk.CURRENT_TIME);
}
catch (GLib.Error e){
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 13:43:55 +02:00
}
2019-03-11 13:28:51 +01:00
return true;
2018-05-04 22:57:31 +02:00
}
2018-06-06 16:19:11 +02:00
// Copy a string to the clipboard
2018-05-21 17:23:31 +02:00
public static void copy (string str) {
2018-06-06 16:19:11 +02:00
var display = window.get_display ();
2018-05-21 17:23:31 +02:00
var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
2019-03-11 15:14:37 +01:00
clipboard.set_text (Widgets.RichLabel.restore_entities (str), -1);
2018-05-21 12:40:49 +02:00
}
2018-06-06 16:19:11 +02:00
// Download a file from the web to a user's configured Downloads folder
2020-05-29 14:19:35 +02: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 12:42:27 +01:00
2018-05-21 18:13:49 +02:00
var i = url.last_index_of ("/");
var name = url.substring (i + 1, url.length - i - 1);
if (name == null)
2020-05-29 14:19:35 +02:00
name = _("Unknown Attachment");
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02: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 12:42:27 +01:00
2020-05-29 14:19:35 +02: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 18:13:49 +02:00
2020-05-29 14:19:35 +02: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 12:28:35 +02:00
2020-05-29 14:19:35 +02:00
} catch (Error e) {
warning ("Error: %s\n", e.message);
ecb (0, e.message);
2018-05-21 18:13:49 +02:00
}
2020-05-29 14:19:35 +02:00
})
.on_error ((code, reason) => ecb)
.exec ();
2018-05-21 18:13:49 +02:00
}
2019-03-09 12:42:27 +01:00
2020-05-31 12:28:35 +02:00
public static string fallback_icon (string normal, string fallback, string fallback2 = "broken") {
var theme = Gtk.IconTheme.get_default ();
2020-05-31 12:28:35 +02:00
if (theme.has_icon (normal))
return normal;
else
return theme.has_icon (fallback) ? fallback : fallback2;
}
2019-03-09 12:42:27 +01:00
public static Gdk.Pixbuf icon_to_pixbuf (string name) {
var theme = Gtk.IconTheme.get_default ();
return theme.load_icon (name, 32, Gtk.IconLookupFlags.GENERIC_FALLBACK);
}
2019-03-09 12:42:27 +01: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 15:16:57 +02:00
}