[gnome-contacts/nielsdg/new-operations: 7/7] app: Flush pending operations before shutdown




commit 17fbcb2cab265126f2aaa0c0e2bc8aa9bef695b0
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Mon Jun 6 14:26:09 2022 +0200

    app: Flush pending operations before shutdown
    
    Make sure that any operations which are still pending (due to having an
    initial timeout, or whatever other reason), are executed immediately.
    
    This fixes several recent bugs that were reported recently on
    linking/deletion/... not working if you closed the app soon after.

 src/contacts-app.vala | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
---
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index e7b04714..7696736f 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -33,7 +33,7 @@ public class Contacts.App : Adw.Application {
   }
 
   private const GLib.ActionEntry[] action_entries = {
-    { "quit",             quit                },
+    { "quit",             quit_action         },
     { "help",             show_help           },
     { "about",            show_about          },
     { "change-book",      change_address_book },
@@ -203,8 +203,12 @@ public class Contacts.App : Adw.Application {
 
   private void create_window () {
     var win = new MainWindow (this.settings, this.operations, this, this.contacts_store);
-    win.show ();
+    win.close_request.connect_after ((win) => {
+      activate_action ("quit", null);
+      return false;
+    });
     this.window = win;
+    win.present ();
 
     show_contact_list ();
   }
@@ -296,4 +300,33 @@ public class Contacts.App : Adw.Application {
       show_individual_for_id.begin (individual_id);
   }
 
+  private void quit_action (SimpleAction action, Variant? param) {
+    if (!this.operations.has_pending_operations ()) {
+      debug ("No more operations pending. Quitting immediately");
+      base.quit ();
+    }
+
+    debug ("Some operations still pending, delaying shutdown");
+
+    // We still have operations pending but the user requested to quit, so
+    // give it still a limited amount of time to still get them done
+    if (this.window != null)
+      this.window.hide ();
+
+    Timeout.add_seconds (5, () => {
+      warning ("Some operations have not finished yet!");
+      base.quit ();
+      return Source.REMOVE;
+    });
+
+    this.operations.flush.begin ((obj, res) => {
+      try {
+        this.operations.flush.end (res);
+        debug ("Succesfully flushed operations before quitting");
+      } catch (Error e) {
+        warning ("Error flushing operations: %s", e.message);
+      }
+      base.quit ();
+    });
+  }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]