2020-08-01 17:56:12 +00:00
|
|
|
using GLib;
|
|
|
|
|
2018-05-30 19:49:16 +00:00
|
|
|
public class Tootle.Desktop {
|
2018-06-06 14:19:11 +00:00
|
|
|
|
2020-08-01 17:56:12 +00:00
|
|
|
// Open a URI in the user's default application
|
2021-02-02 14:07:27 +00:00
|
|
|
public static bool open_uri (string _uri) {
|
|
|
|
var uri = _uri;
|
|
|
|
if (!("//" in uri))
|
|
|
|
uri = "file://" + _uri;
|
|
|
|
|
2020-08-01 17:56:12 +00:00
|
|
|
message (@"Opening URI: $uri");
|
|
|
|
try {
|
2021-02-02 14:07:27 +00:00
|
|
|
var success = AppInfo.launch_default_for_uri (uri, null);
|
|
|
|
if (!success)
|
|
|
|
throw new Oopsie.USER (_("launch_default_for_uri() failed"));
|
2020-08-01 17:56:12 +00:00
|
|
|
}
|
|
|
|
catch (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 (Error e){
|
2021-02-02 14:07:27 +00:00
|
|
|
warning (@"xdg-open failed too: $(e.message)");
|
|
|
|
app.error (_("Open this URL in your browser", uri);
|
2020-10-24 04:04:20 +00:00
|
|
|
return false;
|
2020-08-01 17:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy a string to the clipboard
|
|
|
|
public static void copy (string str) {
|
|
|
|
var display = window.get_display ();
|
|
|
|
var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
|
|
|
|
clipboard.set_text (Widgets.RichLabel.restore_entities (str), -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string get_uri_host (string uri) {
|
|
|
|
var p1 = uri;
|
|
|
|
if ("//" in uri)
|
|
|
|
p1 = uri.split ("//")[1];
|
|
|
|
|
|
|
|
return p1.split ("/")[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download a file from the web to a user's configured Downloads folder
|
2020-11-19 09:46:18 +00:00
|
|
|
public async static string download (string url) throws Error {
|
2020-08-01 17:56:12 +00:00
|
|
|
message (@"Downloading file: $url...");
|
|
|
|
|
|
|
|
var file_name = Path.get_basename (url);
|
|
|
|
var dir_name = Path.get_dirname (url);
|
|
|
|
|
|
|
|
var dir_path = Path.build_path (
|
|
|
|
Path.DIR_SEPARATOR_S,
|
|
|
|
Environment.get_user_special_dir (UserDirectory.DOWNLOAD),
|
|
|
|
Build.NAME,
|
|
|
|
get_uri_host (dir_name));
|
|
|
|
|
|
|
|
var file_path = Path.build_path (
|
|
|
|
Path.DIR_SEPARATOR_S,
|
|
|
|
dir_path,
|
|
|
|
str_hash (dir_name).to_string () + file_name);
|
|
|
|
|
2020-11-19 09:46:18 +00:00
|
|
|
var dir = File.new_for_path (dir_path);
|
|
|
|
if (!dir.query_exists ())
|
|
|
|
dir.make_directory_with_parents ();
|
|
|
|
|
|
|
|
var file = File.new_for_path (file_path);
|
|
|
|
|
|
|
|
if (!file.query_exists ()) {
|
|
|
|
var msg = yield new Request.GET (url)
|
|
|
|
.await ();
|
|
|
|
|
|
|
|
var data = msg.response_body.data;
|
|
|
|
FileOutputStream stream = file.create (FileCreateFlags.PRIVATE);
|
|
|
|
stream.write (data);
|
|
|
|
|
|
|
|
message (@"OK: File written to: $file_path");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
message ("OK: File exists already");
|
|
|
|
|
|
|
|
return file_path;
|
2020-08-01 17:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static string fallback_icon (string normal, string fallback, string fallback2 = "broken") {
|
|
|
|
var theme = Gtk.IconTheme.get_default ();
|
|
|
|
if (theme.has_icon (normal))
|
|
|
|
return normal;
|
|
|
|
else
|
|
|
|
return theme.has_icon (fallback) ? fallback : fallback2;
|
|
|
|
}
|
2019-03-09 11:42:27 +00:00
|
|
|
|
2020-07-10 14:30:57 +00: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);
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:16:57 +00:00
|
|
|
}
|