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">
<default>false</default>
</key>
<key name="aggressive-resolving" type="b">
<default>false</default>
</key>
<key name="window-x" type="i">
<default>-1</default>

View File

@ -1,22 +1,21 @@
using GLib;
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) {
message (@"Opening URI: $uri");
try {
Gtk.show_uri (null, uri, Gdk.CURRENT_TIME);
}
catch (GLib.Error e){
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 (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);
}
catch (Error e){
warning (@"Can't open URI \"$uri\": $(e.message)");
app.error (_("Open this URL in your browser:\n\n%s").printf (uri), "");
}
}
return true;
@ -29,19 +28,32 @@ public class Tootle.Desktop {
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
public delegate void DownloadCallback (string path);
public static void download (string url, DownloadCallback? cb = null, Network.ErrorCallback? ecb = null) {
info (@"Downloading file: $url...");
public static void download (string url, owned DownloadCallback cb, owned Network.ErrorCallback ecb) {
message (@"Downloading file: $url...");
var i = url.last_index_of ("/");
var name = url.substring (i + 1, url.length - i - 1);
if (name == null)
name = _("Unknown Attachment");
var file_name = Path.get_basename (url);
var dir_name = Path.get_dirname (url);
var downloads = GLib.Environment.get_user_special_dir (UserDirectory.DOWNLOAD);
var dir_path = @"$downloads/$(Build.NAME)";
var file_path = @"$dir_path/$name";
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);
new Request.GET (url)
.then ((sess, msg) => {
@ -56,7 +68,7 @@ public class Tootle.Desktop {
FileOutputStream stream = file.create (FileCreateFlags.PRIVATE);
stream.write (data);
}
info ("OK");
message (@"OK: File written to: $file_path");
cb (file_path);
} catch (Error e) {
@ -64,7 +76,7 @@ public class Tootle.Desktop {
ecb (0, e.message);
}
})
.on_error ((code, reason) => ecb)
.on_error ((owned) ecb)
.exec ();
}

View File

@ -11,6 +11,7 @@ public class Tootle.Settings : GLib.Settings {
public int post_text_size { get; set; }
public bool 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_y { get; set; }
@ -28,6 +29,7 @@ public class Tootle.Settings : GLib.Settings {
init ("post-text-size");
init ("live-updates");
init ("public-live-updates");
init ("aggressive-resolving");
init ("window-x");
init ("window-y");

View File

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

View File

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