[rygel] media-export: Use vala's async handling for GIO



commit 379d7f5b256e6c0dc6c1d80701037db179984389
Author: Jens Georg <mail jensge org>
Date:   Fri Oct 16 20:32:59 2009 +0200

    media-export: Use vala's async handling for GIO

 .../media-export/rygel-media-export-harvester.vala |  230 +++++++++-----------
 .../rygel-media-export-recursive-file-monitor.vala |   23 +--
 2 files changed, 116 insertions(+), 137 deletions(-)
---
diff --git a/src/plugins/media-export/rygel-media-export-harvester.vala b/src/plugins/media-export/rygel-media-export-harvester.vala
index b831bc4..b0e5685 100644
--- a/src/plugins/media-export/rygel-media-export-harvester.vala
+++ b/src/plugins/media-export/rygel-media-export-harvester.vala
@@ -77,37 +77,6 @@ public class Rygel.MediaExportHarvester : GLib.Object {
         this.monitor = monitor;
     }
 
-    private void on_close_async (Object? obj, AsyncResult res) {
-        var enumerator = (FileEnumerator) obj;
-        try {
-            enumerator.close_finish (res);
-        } catch (Error error) {
-            // TODO
-        }
-
-        // delete all children which are not in filesystem anymore
-        var container = (DummyContainer) this.containers.peek_head ();
-        try {
-            var children = this.media_db.get_child_ids (container.id);
-
-            foreach (var seen_id in container.seen_children) {
-                children.remove (seen_id);
-            }
-
-            foreach (var child in children) {
-                this.media_db.remove_by_id (child);
-            }
-
-            this.do_update ();
-        } catch (MediaDBError err) {
-            warning("Failed to get children of container %s: %s",
-                    container.id,
-                    err.message);
-        }
-
-        Idle.add(this.on_idle);
-    }
-
     private bool push_if_changed_or_unknown (File       file,
                                              FileInfo   info,
                                              out string id) {
@@ -133,63 +102,93 @@ public class Rygel.MediaExportHarvester : GLib.Object {
         return false;
     }
 
-    private void on_next_files_ready (Object? obj, AsyncResult res) {
-        var enumerator = (FileEnumerator) obj;
-        try {
-            var list = enumerator.next_files_finish (res);
-            if (list != null) {
-                foreach (var info in list) {
-                    if (info.get_name ()[0] == '.') {
-                        continue;
-                    }
-                    var parent_container =
-                                 (DummyContainer)this.containers.peek_head ();
-
-                    var dir = parent_container.file;
-                    var file = dir.get_child (info.get_name ());
-                    if (info.get_file_type () == FileType.DIRECTORY) {
-                        monitor.monitor (file);
-                        var container = new DummyContainer (file,
-                                                            parent_container);
-                        this.containers.push_tail (container);
-                        parent_container.seen (container.id);
-                        int64 timestamp;
-                        if (!this.media_db.exists (container.id,
-                                                   out timestamp)) {
-                            this.media_db.save_object (container);
-                        }
-                    } else {
-                        string id;
-                        push_if_changed_or_unknown (file, info, out id);
-                        parent_container.seen (id);
+    private bool process_children (GLib.List<FileInfo>? list) {
+        if (list == null)
+            return false;
+
+        foreach (var info in list) {
+            if (info.get_name ()[0] == '.') {
+                continue;
+            }
+            var parent_container =
+                (DummyContainer)this.containers.peek_head ();
+
+            var dir = parent_container.file;
+            var file = dir.get_child (info.get_name ());
+            if (info.get_file_type () == FileType.DIRECTORY) {
+                monitor.monitor (file);
+                var container = new DummyContainer (file,
+                                                    parent_container);
+                this.containers.push_tail (container);
+                parent_container.seen (container.id);
+                try {
+                    int64 timestamp;
+                    if (!this.media_db.exists (container.id,
+                                               out timestamp)) {
+                        this.media_db.save_object (container);
                     }
+                } catch (Error err) {
+                    warning ("Failed to update database: %s",
+                             err.message);
                 }
-
-                enumerator.next_files_async (10,
-                                             Priority.DEFAULT,
-                                             null,
-                                             this.on_next_files_ready);
             } else {
-                enumerator.close_async (Priority.DEFAULT,
-                                        null,
-                                        this.on_close_async);
+                string id;
+                push_if_changed_or_unknown (file, info, out id);
+                parent_container.seen (id);
             }
-        } catch (Error error) {
-            // TODO
         }
+
+        return true;
+    }
+
+    private async void enumerate_directory (File directory) {
+        try {
+            var enumerator = yield directory.enumerate_children_async (
+                    FILE_ATTRIBUTE_STANDARD_TYPE + "," +
+                    FILE_ATTRIBUTE_STANDARD_NAME + "," +
+                    FILE_ATTRIBUTE_TIME_MODIFIED,
+                    FileQueryInfoFlags.NONE,
+                    Priority.DEFAULT,
+                    null);
+
+
+            GLib.List<FileInfo> list = null;
+            do {
+                list = yield enumerator.next_files_async (10,
+                        Priority.DEFAULT,
+                        null);
+            } while (process_children (list));
+
+            yield enumerator.close_async (Priority.DEFAULT, null);
+        } catch (Error err) {
+            warning ("failed to enumerate directory: %s",
+                     err.message);
+        }
+
+        cleanup_database (this.containers.peek_head() as DummyContainer);
+        this.do_update ();
+        Idle.add(this.on_idle);
     }
 
-    private void on_enumerate_ready (Object? obj, AsyncResult res) {
-        var file = (File) obj;
+    void cleanup_database (DummyContainer container) {
+        // delete all children which are not in filesystem anymore
+        container = (DummyContainer) this.containers.peek_head ();
         try {
-            var enumerator = file.enumerate_children_finish (res);
-            enumerator.next_files_async (10,
-                                         Priority.DEFAULT,
-                                         null,
-                                         on_next_files_ready);
-        } catch (Error error) {
-            // TODO
+            var children = this.media_db.get_child_ids (container.id);
+
+            foreach (var seen_id in container.seen_children) {
+                children.remove (seen_id);
+            }
+
+            foreach (var child in children) {
+                this.media_db.remove_by_id (child);
+            }
+        } catch (MediaDBError err) {
+            warning("Failed to get children of container %s: %s",
+                    container.id,
+                    err.message);
         }
+
     }
 
     private bool on_idle () {
@@ -198,14 +197,7 @@ public class Rygel.MediaExportHarvester : GLib.Object {
             this.extractor.extract (candidate);
         } else if (this.containers.get_length () > 0) {
             var directory = ((DummyContainer)this.containers.peek_head ()).file;
-            directory.enumerate_children_async (
-                            FILE_ATTRIBUTE_STANDARD_TYPE + "," +
-                            FILE_ATTRIBUTE_STANDARD_NAME + "," +
-                            FILE_ATTRIBUTE_TIME_MODIFIED,
-                            FileQueryInfoFlags.NONE,
-                            Priority.DEFAULT,
-                            null,
-                            this.on_enumerate_ready);
+            enumerate_directory (directory);
         } else {
             // nothing to do
             harvested (this.origin);
@@ -214,10 +206,35 @@ public class Rygel.MediaExportHarvester : GLib.Object {
         return false;
     }
 
-    private void on_initial_info_ready (Object? source, AsyncResult result) {
-        var file = (File) source;
+    /**
+     * Fired for every file passed to harvest.
+     */
+    public signal void harvested (File file);
+
+    /**
+     * Extract all metainformation from a given file.
+     *
+     * What action will be taken depends on the arguments
+     * * file is a simple file. Then only information of this
+     *   file will be extracted
+     * * file is a directory and recursive is false. The children
+     *   of the directory (if not directories themselves) will be
+     *   enqueued for extraction
+     * * file is a directory and recursive is true. ++ All ++ children
+     *   of the directory will be enqueued for extraction, even directories
+     *
+     * No matter how many children are contained within file's hierarchy,
+     * only one event is sent when all the children are done.
+     */
+    public async void harvest (File file) {
         try {
-            var info = file.query_info_finish (result);
+            var info = yield file.query_info_async (
+                                          FILE_ATTRIBUTE_STANDARD_NAME + "," +
+                                          FILE_ATTRIBUTE_STANDARD_TYPE + "," +
+                                          FILE_ATTRIBUTE_TIME_MODIFIED,
+                                          FileQueryInfoFlags.NONE,
+                                          Priority.DEFAULT,
+                                          null);
 
             if (info.get_file_type () == FileType.DIRECTORY) {
                 this.origin = file;
@@ -244,42 +261,11 @@ public class Rygel.MediaExportHarvester : GLib.Object {
             warning ("Failed to harvest file %s: %s",
                      file.get_uri (),
                      err.message);
-            this.harvested (file);
+            harvested (file);
         }
     }
 
-    /**
-     * Fired for every file passed to harvest.
-     */
-    public signal void harvested (File file);
-
-    /**
-     * Extract all metainformation from a given file.
-     *
-     * What action will be taken depends on the arguments
-     * * file is a simple file. Then only information of this
-     *   file will be extracted
-     * * file is a directory and recursive is false. The children
-     *   of the directory (if not directories themselves) will be
-     *   enqueued for extraction
-     * * file is a directory and recursive is true. ++ All ++ children
-     *   of the directory will be enqueued for extraction, even directories
-     *
-     * No matter how many children are contained within file's hierarchy,
-     * only one event is sent when all the children are done.
-     */
-    public void harvest (File file) {
-        file.query_info_async (FILE_ATTRIBUTE_STANDARD_NAME + "," +
-                               FILE_ATTRIBUTE_STANDARD_TYPE + "," +
-                               FILE_ATTRIBUTE_TIME_MODIFIED,
-                               FileQueryInfoFlags.NONE,
-                               Priority.DEFAULT,
-                               null,
-                               this.on_initial_info_ready);
-    }
-
     private void on_extracted_cb (File file, Gst.TagList tag_list) {
-
         var entry = this.files.peek_head ();
         if (entry == null) {
             // this event may be triggered by another instance
diff --git a/src/plugins/media-export/rygel-media-export-recursive-file-monitor.vala b/src/plugins/media-export/rygel-media-export-recursive-file-monitor.vala
index 76b35b9..558489e 100644
--- a/src/plugins/media-export/rygel-media-export-recursive-file-monitor.vala
+++ b/src/plugins/media-export/rygel-media-export-recursive-file-monitor.vala
@@ -57,11 +57,13 @@ public class Rygel.MediaExportRecursiveFileMonitor : Object {
         }
     }
 
-    private void on_info_ready (Object? source, AsyncResult res) {
-        var file = (File) source;
-
+    public async void monitor (File file) {
         try {
-            var info = file.query_info_finish (res);
+            var info = yield file.query_info_async (
+                                                 FILE_ATTRIBUTE_STANDARD_TYPE,
+                                                 FileQueryInfoFlags.NONE,
+                                                 Priority.DEFAULT,
+                                                 null);
             if (info.get_file_type () == FileType.DIRECTORY) {
                 var file_monitor = file.monitor_directory (
                                                          FileMonitorFlags.NONE,
@@ -69,20 +71,11 @@ public class Rygel.MediaExportRecursiveFileMonitor : Object {
                 this.monitors.set (file, file_monitor);
                 file_monitor.changed.connect (this.on_monitor_changed);
             }
-        } catch (Error error) {
-            warning ("Failed to get file info for %s",
-                     file.get_uri ());
+        } catch (Error err) {
+            warning ("Failed to get file info for %s", file.get_uri ());
         }
     }
 
-    public void monitor (File file) {
-        file.query_info_async (FILE_ATTRIBUTE_STANDARD_TYPE,
-                               FileQueryInfoFlags.NONE,
-                               Priority.DEFAULT,
-                               null,
-                               this.on_info_ready);
-    }
-
     public void cancel () {
         if (this.cancellable != null) {
             this.cancellable.cancel ();



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