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
|
|
|
|
public static void open_uri (string uri) {
|
|
|
|
try {
|
|
|
|
Gtk.show_uri (null, uri, Gdk.CURRENT_TIME);
|
|
|
|
}
|
|
|
|
catch (GLib.Error e){
|
|
|
|
warning ("Can't open %s: %s", uri, e.message);
|
|
|
|
app.error (_("Error"), e.message);
|
|
|
|
}
|
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);
|
2018-06-06 14:19:11 +00:00
|
|
|
clipboard.set_text (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
|
2018-05-30 19:49:16 +00:00
|
|
|
public static void download_file (string url) {
|
2018-05-21 16:13:49 +00:00
|
|
|
debug ("Downloading file: %s", url);
|
|
|
|
|
|
|
|
var i = url.last_index_of ("/");
|
|
|
|
var name = url.substring (i + 1, url.length - i - 1);
|
|
|
|
if (name == null)
|
|
|
|
name = "unknown";
|
|
|
|
|
2018-06-06 14:19:11 +00:00
|
|
|
var dir_path = "%s/%s".printf (GLib.Environment.get_user_special_dir (UserDirectory.DOWNLOAD), app.program_name);
|
2018-05-21 16:13:49 +00:00
|
|
|
var file_path = "%s/%s".printf (dir_path, name);
|
|
|
|
|
|
|
|
var msg = new Soup.Message("GET", url);
|
|
|
|
msg.finished.connect(() => {
|
|
|
|
try {
|
|
|
|
var dir = File.new_for_path (dir_path);
|
|
|
|
if (!dir.query_exists ())
|
|
|
|
dir.make_directory ();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-06-06 14:19:11 +00:00
|
|
|
app.toast ("Media downloaded");
|
2018-05-21 16:13:49 +00:00
|
|
|
} catch (Error e) {
|
2018-06-06 14:19:11 +00:00
|
|
|
app.toast (e.message);
|
2018-05-21 16:13:49 +00:00
|
|
|
warning ("Error: %s\n", e.message);
|
|
|
|
}
|
|
|
|
});
|
2018-06-06 14:19:11 +00:00
|
|
|
network.queue (msg);
|
2018-05-21 16:13:49 +00:00
|
|
|
}
|
2018-06-06 14:19:11 +00:00
|
|
|
|
2018-06-17 08:40:24 +00:00
|
|
|
public static string fallback_icon (string normal, string fallback) {
|
|
|
|
var theme = Gtk.IconTheme.get_default ();
|
|
|
|
return theme.has_icon (normal) ? normal : fallback;
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:16:57 +00:00
|
|
|
}
|