Skip to content

Commit

Permalink
feat(host): async only (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr authored Dec 20, 2024
1 parent 3b5751f commit ac32485
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/API/Notification.vala
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public class Tuba.API.Notification : Entity, Widgetizable {
public override void open () {
switch (kind) {
case InstanceAccount.KIND_SEVERED_RELATIONSHIPS:
Host.open_url (@"$(accounts.active.instance)/severed_relationships");
Host.open_url.begin (@"$(accounts.active.instance)/severed_relationships");
break;
case InstanceAccount.KIND_MODERATION_WARNING:
string dispute_id = this.moderation_warning == null ? "" : this.moderation_warning.id;
Host.open_url (@"$(accounts.active.instance)/disputes/strikes/$dispute_id");
Host.open_url.begin (@"$(accounts.active.instance)/disputes/strikes/$dispute_id");
break;
case InstanceAccount.KIND_ADMIN_REPORT:
if (report != null) {
Expand All @@ -74,7 +74,7 @@ public class Tuba.API.Notification : Entity, Widgetizable {
admin_window.present ();
admin_window.open_reports ();
} else {
Host.open_url (@"$(accounts.active.instance)/admin/reports/$(report.id)");
Host.open_url.begin (@"$(accounts.active.instance)/admin/reports/$(report.id)");
}
}
break;
Expand Down
8 changes: 4 additions & 4 deletions src/API/Status/PreviewCard.vala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class Tuba.API.PreviewCard : Entity, Widgetizable {
public bool open_special_card (string t_url) {
switch (this) {
case BASIC:
Host.open_url (t_url);
Host.open_url.begin (t_url);
return true;
default:
return false;
Expand Down Expand Up @@ -182,7 +182,7 @@ public class Tuba.API.PreviewCard : Entity, Widgetizable {
try {
card_special_type.parse_url (card_url, out special_host, out special_api_url);
} catch {
Host.open_url (card_url);
Host.open_url.begin (card_url);
return;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ public class Tuba.API.PreviewCard : Entity, Widgetizable {
}

if (failed || res_url == "") {
Host.open_url (card_url);
Host.open_url.begin (card_url);
} else {
if (bookwyrm_obj == null) {
app.main_window.show_media_viewer (res_url, Tuba.Attachment.MediaType.VIDEO, null, null, false, null, card_url, null, true);
Expand All @@ -226,7 +226,7 @@ public class Tuba.API.PreviewCard : Entity, Widgetizable {
}
})
.on_error (() => {
Host.open_url (card_url);
Host.open_url.begin (card_url);
})
.exec ();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dialogs/Composer/AttachmentsPageAttachment.vala
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public class Tuba.AttachmentsPageAttachment : Widgets.Attachment.Item {

protected override void on_click () {
if (attachment_file != null) {
Host.open_url (attachment_file.get_path ());
Host.open_url.begin (attachment_file.get_path ());
} else if (entity != null) {
base.on_click ();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dialogs/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public class Tuba.Dialogs.MainWindow: Adw.ApplicationWindow, Saveable {

((Widgets.BookWyrmPage) book_widget).selectable = true;
} catch {
if (fallback != null) Host.open_url (fallback);
if (fallback != null) Host.open_url.begin (fallback);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dialogs/NewAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
var esc_redirect = Uri.escape_string (redirect_uri);
var pars = @"scope=$esc_scopes&response_type=code&redirect_uri=$esc_redirect&client_id=$(Uri.escape_string (account.client_id))";
var url = @"$(account.instance)/oauth/authorize?$pars";
Host.open_url (url);
Host.open_url.begin (url);
}

async void request_token () throws Error {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Accounts/Mastodon/Account.vala
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public class Tuba.Mastodon.Account : InstanceAccount {
} catch (Error e) {
warning (@"Failed to resolve URL \"$url\":");
warning (e.message);
Host.open_url (url);
Host.open_url.begin (url);
}
});
}
Expand Down
10 changes: 8 additions & 2 deletions src/Services/Accounts/SecretAccountStore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ public class Tuba.SecretAccountStore : AccountStore {
null,
false,
(obj, res) => {
if (app.question.end (res).truthy ()) Host.open_url (wiki_page);
Process.exit (1);
if (app.question.end (res).truthy ()) {
Host.open_url.begin (wiki_page, (obj, res) => {
Host.open_url.end (res);
Process.exit (1);
});
} else {
Process.exit (1);
}
}
);
}
Expand Down
47 changes: 22 additions & 25 deletions src/Utils/Host.vala
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
public class Tuba.Host {

// Open a URI in the user's default application
public static bool open_url (string _uri) {
public async static bool open_url (string _uri) {
var uri = _uri;
if (!("://" in uri))
uri = "file://" + _uri;

if (settings.strip_tracking)
uri = Tracking.strip_utm (uri);

open_in_default_app (uri);
return true;
return yield open_in_default_app (uri);
}

// To avoid creating multiple Uri instances,
// split opening into two wrappers, one for
// strings and one for GLib.Uri
public static bool open_uri (Uri uri) {
public async static bool open_uri (Uri uri) {
string url;
try {
url = Tracking.strip_utm_from_uri (uri).to_string ();
} catch (Error e) {
warning (@"Error while stripping tracking params: $(e.message)");
url = uri.to_string ();
}
open_in_default_app (url);

return true;
return yield open_in_default_app (url);
}

private static void open_in_default_app (string uri) {
private async static bool open_in_default_app (string uri) {
debug (@"Opening URI: $uri");

var launcher = new Gtk.UriLauncher (uri);
launcher.launch.begin (app.active_window, null, (obj, res) => {
try {
launcher.launch.end (res);
} catch (Error e) {
warning (@"Error opening uri \"$uri\": $(e.message)");
open_in_default_app_using_dbus (uri);
}
});
try {
yield (new Gtk.UriLauncher (uri)).launch (app.active_window, null);
} catch (Error e) {
warning (@"Error opening uri \"$uri\": $(e.message)");
return yield open_in_default_app_using_dbus (uri);
}

return true;
}

private static void open_in_default_app_using_dbus (string uri) {
AppInfo.launch_default_for_uri_async.begin (uri, null, null, (obj, res) => {
try {
AppInfo.launch_default_for_uri_async.end (res);
} catch (Error e) {
warning (@"Error opening using launch_default_for_uri \"$uri\": $(e.message)");
}
});
private async static bool open_in_default_app_using_dbus (string uri) {
try {
yield AppInfo.launch_default_for_uri_async (uri, null, null);
} catch (Error e) {
warning (@"Error opening using launch_default_for_uri \"$uri\": $(e.message)");
return false;
}

return true;
}

public static void copy (string str) {
Expand Down
2 changes: 1 addition & 1 deletion src/Views/MediaViewer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
Item? page = safe_get ((int) carousel.position);
if (page == null) return;

Host.open_url (page.url);
Host.open_url.begin (page.url);
}

private void save_as () {
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Profile.vala
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public class Tuba.Views.Profile : Views.Accounts {

var open_in_browser_action = new SimpleAction ("open_in_browser", null);
open_in_browser_action.activate.connect (v => {
Host.open_url (profile.account.url);
Host.open_url.begin (profile.account.url);
});
actions.add_action (open_in_browser_action);

Expand Down
2 changes: 1 addition & 1 deletion src/Views/Thread.vala
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public class Tuba.Views.Thread : Views.ContentBase, AccountHolder {
app.main_window.open_view (new Views.Thread (status));
}
else
Host.open_url (q);
Host.open_url.begin (q);
})
.exec ();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Widgets/Attachment/Item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Tuba.Widgets.Attachment.Item : Adw.Bin {
}

private void open_in_browser () {
Host.open_url (entity.url);
Host.open_url.begin (entity.url);
}

private void save_as () {
Expand Down Expand Up @@ -226,6 +226,6 @@ public class Tuba.Widgets.Attachment.Item : Adw.Bin {

protected async void open () throws Error {
var path = yield Host.download (entity.url);
Host.open_url (path);
Host.open_url.begin (path);
}
}
4 changes: 2 additions & 2 deletions src/Widgets/BookWyrmPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public class Tuba.Widgets.BookWyrmPage : Gtk.Box {

[GtkCallback]
void open_on_openlibrary () {
Host.open_url (@"https://openlibrary.org/books/$(book.openlibraryKey)");
Host.open_url.begin (@"https://openlibrary.org/books/$(book.openlibraryKey)");
}

[GtkCallback]
void open_on_bw () {
Host.open_url (book.id);
Host.open_url.begin (book.id);
}
}
4 changes: 2 additions & 2 deletions src/Widgets/PreviewCard.vala
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public class Tuba.Widgets.PreviewCard : Gtk.Box {
if (author_account != null) {
author_account.open ();
} else if (author_url != null && author_url != "") {
Host.open_url (author_url);
Host.open_url.begin (author_url);
}
}

Expand Down Expand Up @@ -196,7 +196,7 @@ public class Tuba.Widgets.PreviewCard : Gtk.Box {
if (callback_account != null) {
callback_account.open ();
} else if (callback_url != null && callback_url != "") {
Host.open_url (callback_url);
Host.open_url.begin (callback_url);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/PreviewCardExplore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ public class Tuba.Widgets.PreviewCardExplore : Gtk.ListBoxRow {
}

private void on_card_click () {
Host.open_url (this.url);
Host.open_url.begin (this.url);
}
}
8 changes: 4 additions & 4 deletions src/Widgets/RichLabel.vala
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ public class Tuba.Widgets.RichLabel : Adw.Bin {
warning (@"Failed to resolve URL \"$url\":");
warning (e.message);
if (uri == null) {
Host.open_url (url);
Host.open_url.begin (url);
} else {
Host.open_uri (uri);
Host.open_uri.begin (uri);
}
}
});
} else {
if (uri == null) {
Host.open_url (url);
Host.open_url.begin (url);
} else {
Host.open_uri (uri);
Host.open_uri.begin (uri);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Status.vala
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@
}

private void open_in_browser () {
Host.open_url (status.formal.url ?? status.formal.account.url);
Host.open_url.begin (status.formal.url ?? status.formal.account.url);
}

private void report_status () {
Expand Down

0 comments on commit ac32485

Please sign in to comment.