1
0
mirror of https://gitlab.gnome.org/World/tootle synced 2025-02-17 03:51:11 +01:00

Desktop: refactor file downloading

This commit is contained in:
Bleak Grey 2020-08-01 20:56:12 +03:00
parent e2e3400c86
commit ddde3edd67
5 changed files with 94 additions and 74 deletions

View File

@ -37,6 +37,9 @@
<key name="public-live-updates" type="b"> <key name="public-live-updates" type="b">
<default>false</default> <default>false</default>
</key> </key>
<key name="aggressive-resolving" type="b">
<default>false</default>
</key>
<key name="window-x" type="i"> <key name="window-x" type="i">
<default>-1</default> <default>-1</default>

View File

@ -1,88 +1,100 @@
using GLib;
public class Tootle.Desktop { public class Tootle.Desktop {
// Open URI in the user's default application associated with it // Open a URI in the user's default application
public static bool open_uri (string uri) { public static bool open_uri (string uri) {
try { message (@"Opening URI: $uri");
Gtk.show_uri (null, uri, Gdk.CURRENT_TIME); try {
} Gtk.show_uri (null, uri, Gdk.CURRENT_TIME);
catch (GLib.Error e){ }
try { catch (Error e){
string[] spawn_args = {"/usr/bin/xdg-open", uri}; try {
Process.spawn_sync (null, spawn_args, null, SpawnFlags.SEARCH_PATH, null, null, null); 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); catch (Error e){
if (e.message == "Operation not supported") { warning (@"Can't open URI \"$uri\": $(e.message)");
app.error (_("Open this in a web browser:\n\n"+uri),""); app.error (_("Open this URL in your browser:\n\n%s").printf (uri), "");
} else { }
app.error (_("Error"), e.message); }
} return true;
} }
}
return true;
}
// Copy a string to the clipboard // Copy a string to the clipboard
public static void copy (string str) { public static void copy (string str) {
var display = window.get_display (); var display = window.get_display ();
var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD); var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
clipboard.set_text (Widgets.RichLabel.restore_entities (str), -1); clipboard.set_text (Widgets.RichLabel.restore_entities (str), -1);
} }
// Download a file from the web to a user's configured Downloads folder public static string get_uri_host (string uri) {
public delegate void DownloadCallback (string path); var p1 = uri;
public static void download (string url, DownloadCallback? cb = null, Network.ErrorCallback? ecb = null) { if ("//" in uri)
info (@"Downloading file: $url..."); p1 = uri.split ("//")[1];
var i = url.last_index_of ("/"); return p1.split ("/")[0];
var name = url.substring (i + 1, url.length - i - 1); }
if (name == null)
name = _("Unknown Attachment");
var downloads = GLib.Environment.get_user_special_dir (UserDirectory.DOWNLOAD); // Download a file from the web to a user's configured Downloads folder
var dir_path = @"$downloads/$(Build.NAME)"; public delegate void DownloadCallback (string path);
var file_path = @"$dir_path/$name"; public static void download (string url, owned DownloadCallback cb, owned Network.ErrorCallback ecb) {
message (@"Downloading file: $url...");
new Request.GET (url) var file_name = Path.get_basename (url);
.then ((sess, msg) => { var dir_name = Path.get_dirname (url);
try {
var dir = File.new_for_path (dir_path);
if (!dir.query_exists ())
dir.make_directory ();
var file = File.new_for_path (file_path); var dir_path = Path.build_path (
if (!file.query_exists ()) { Path.DIR_SEPARATOR_S,
var data = msg.response_body.data; Environment.get_user_special_dir (UserDirectory.DOWNLOAD),
FileOutputStream stream = file.create (FileCreateFlags.PRIVATE); Build.NAME,
stream.write (data); get_uri_host (dir_name));
}
info ("OK");
cb (file_path);
} catch (Error e) { var file_path = Path.build_path (
warning ("Error: %s\n", e.message); Path.DIR_SEPARATOR_S,
ecb (0, e.message); dir_path,
} str_hash (dir_name).to_string () + file_name);
})
.on_error ((code, reason) => ecb)
.exec ();
}
public static string fallback_icon (string normal, string fallback, string fallback2 = "broken") { new Request.GET (url)
var theme = Gtk.IconTheme.get_default (); .then ((sess, msg) => {
if (theme.has_icon (normal)) try {
return normal; var dir = File.new_for_path (dir_path);
else if (!dir.query_exists ())
return theme.has_icon (fallback) ? fallback : fallback2; 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);
}
message (@"OK: File written to: $file_path");
cb (file_path);
} catch (Error e) {
warning ("Error: %s\n", e.message);
ecb (0, e.message);
}
})
.on_error ((owned) ecb)
.exec ();
}
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;
}
public static Gdk.Pixbuf icon_to_pixbuf (string name) { public static Gdk.Pixbuf icon_to_pixbuf (string name) {
var theme = Gtk.IconTheme.get_default (); var theme = Gtk.IconTheme.get_default ();
return theme.load_icon (name, 32, Gtk.IconLookupFlags.GENERIC_FALLBACK); return theme.load_icon (name, 32, Gtk.IconLookupFlags.GENERIC_FALLBACK);
} }
public static void set_hotkey_tooltip (Gtk.Widget widget, string? description, string[] accelerators) { public static void set_hotkey_tooltip (Gtk.Widget widget, string? description, string[] accelerators) {
widget.tooltip_markup = Granite.markup_accel_tooltip (accelerators, description); widget.tooltip_markup = Granite.markup_accel_tooltip (accelerators, description);
} }
} }

View File

@ -11,6 +11,7 @@ public class Tootle.Settings : GLib.Settings {
public int post_text_size { get; set; } public int post_text_size { get; set; }
public bool live_updates { get; set; } public bool live_updates { get; set; }
public bool public_live_updates { get; set; } public bool public_live_updates { get; set; }
public bool aggressive_resolving { get; set; }
public int window_x { get; set; } public int window_x { get; set; }
public int window_y { get; set; } public int window_y { get; set; }
@ -28,6 +29,7 @@ public class Tootle.Settings : GLib.Settings {
init ("post-text-size"); init ("post-text-size");
init ("live-updates"); init ("live-updates");
init ("public-live-updates"); init ("public-live-updates");
init ("aggressive-resolving");
init ("window-x"); init ("window-x");
init ("window-y"); init ("window-y");

View File

@ -48,13 +48,16 @@ public class Tootle.Widgets.Attachment.Slot : FlowBoxChild {
void download () { void download () {
Desktop.download (attachment.url, path => { Desktop.download (attachment.url, path => {
app.toast (_("Attachment downloaded")); app.toast (_("File saved to Downloads"));
}); },
() => {});
} }
void open () { void open () {
Desktop.download (attachment.url, path => { Desktop.download (attachment.url, path => {
Desktop.open_uri (path); Desktop.open_uri (path);
}); },
() => {});
} }
protected virtual bool on_clicked (EventButton ev) { protected virtual bool on_clicked (EventButton ev) {

View File

@ -58,7 +58,7 @@ public class Tootle.Widgets.RichLabel : Label {
return true; return true;
} }
var resolve = "@" in url; var resolve = settings.aggressive_resolving || ("@" in url);
if (!resolve) if (!resolve)
Desktop.open_uri (url); Desktop.open_uri (url);
else { else {