Skip to content

Commit

Permalink
Rewrite offline detection to check the connectivity per connected int…
Browse files Browse the repository at this point in the history
…egration
  • Loading branch information
byquanton committed Jan 14, 2025
1 parent 91c4a79 commit 0261fa4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 52 deletions.
1 change: 1 addition & 0 deletions core/Objects/Source.vala
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public class Objects.Source : Objects.BaseObject {

public signal void sync_started ();
public signal void sync_finished ();
public signal void sync_failed ();

public Source.from_import_json (Json.Node node) {
id = node.get_object ().get_string_member ("id");
Expand Down
3 changes: 2 additions & 1 deletion core/Services/CalDAV/Core.vala
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ public class Services.CalDAV.Core : GLib.Object {
source.sync_finished ();
source.last_sync = new GLib.DateTime.now_local ().to_string ();
} catch (Error e) {
debug (e.message);
debug ("Failed to sync: "+e.message);
source.sync_failed();
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Layouts/SidebarSourceRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class Layouts.SidebarSourceRow : Gtk.ListBoxRow {
source.sync_finished.connect (() => {
sync_button.sync_finished ();
});

source.sync_failed.connect (() => {
sync_button.sync_failed ();
});
}

var add_button = new Gtk.Button.from_icon_name ("plus-large-symbolic") {
Expand Down Expand Up @@ -191,4 +195,4 @@ public class Layouts.SidebarSourceRow : Gtk.ListBoxRow {
int ordered = Services.Settings.get_default ().settings.get_enum ("projects-ordered");
return ordered == 0 ? project2.name.collate (project1.name) : project1.name.collate (project2.name);
}
}
}
8 changes: 3 additions & 5 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,10 @@ public class MainWindow : Adw.ApplicationWindow {

return GLib.Source.REMOVE;
});

Services.NetworkMonitor.instance ().network_changed.connect (() => {
if (Services.NetworkMonitor.instance ().network_available) {
foreach (Objects.Source source in Services.Store.instance ().sources) {
source.run_server ();
}
foreach (Objects.Source source in Services.Store.instance ().sources) {
source.run_server ();
}
});
});
Expand Down
32 changes: 1 addition & 31 deletions src/Services/NetworkMonitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,12 @@ public class Services.NetworkMonitor : GLib.Object {
});
}

bool? _network_available = null;
public bool network_available {
get {
if (_network_available == null) {
_network_available = !is_disconnected ();
}

return _network_available;
}
}

public signal void network_changed ();

construct {
var network_monitor = GLib.NetworkMonitor.get_default ();
network_monitor.network_changed.connect (() => {
_network_available = !is_disconnected ();
network_changed ();
});
}

public bool is_disconnected () {
var host = "www.google.com";

try {
var resolver = GLib.Resolver.get_default ();
var addresses = resolver.lookup_by_name (host, null);
var address = addresses.nth_data (0);
if (address == null) {
return false;
}
} catch (Error e) {
debug ("%s\n", e.message);
return true;
}

return false;
}
}
}
22 changes: 8 additions & 14 deletions src/Widgets/SyncButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,27 @@ public class Widgets.SyncButton : Adw.Bin {
child = main_revealer;

Timeout.add (main_revealer.transition_duration, () => {
network_available ();
return GLib.Source.REMOVE;
});

sync_button.clicked.connect (() => {
clicked ();
});

Services.NetworkMonitor.instance ().network_changed.connect (() => {
network_available ();
});
}

private void network_available () {
if (Services.NetworkMonitor.instance ().network_available) {
stack.visible_child_name = "sync";
} else {
stack.visible_child_name = "error";
tooltip_markup = "<b>%s</b>\n%s".printf (_("Offline Mode Is On"), _("Looks like you'are not connected to the\ninternet. Changes you make in offline\nmode will be synced when you reconnect")); // vala-lint=line-length
}
}

public void sync_started () {
stack.visible_child_name = "sync";
tooltip_markup = "";
sync_button.add_css_class ("is_loading");
}

public void sync_finished () {
sync_button.remove_css_class ("is_loading");
}

public void sync_failed () {
sync_button.remove_css_class ("is_loading");
stack.visible_child_name = "error";
tooltip_markup = "<b>%s</b>\n%s".printf (_("Failed to connect to server"), _("It looks like the server is unreachable,\nare you connected to the internet?\nAny changes you make while disconnected\nwill be synchronized when you reconnect.")); // vala-lint=line-length
}
}

0 comments on commit 0261fa4

Please sign in to comment.