From cd64104e59bed23608f7053f9f178063d1905d02 Mon Sep 17 00:00:00 2001 From: Jens Georg Date: Sat, 27 Jun 2009 02:47:52 +0200 Subject: [PATCH] media-export: Add recursive file monitor --- configure.ac | 2 +- src/plugins/media-export/Makefile.am | 4 +- .../rygel-media-export-recursive-file-monitor.vala | 104 +++++++++++++++++++++ .../rygel-media-export-root-container.vala | 8 +- 4 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 src/plugins/media-export/rygel-media-export-recursive-file-monitor.vala diff --git a/configure.ac b/configure.ac index b06c850..5fdbde2 100644 --- a/configure.ac +++ b/configure.ac @@ -20,7 +20,7 @@ AC_STDC_HEADERS AC_PROG_LIBTOOL AC_FUNC_MMAP -VALA_REQUIRED=0.7.4 +VALA_REQUIRED=0.7.5 GUPNP_REQUIRED=0.13 GUPNP_AV_REQUIRED=0.5 GUPNP_VALA_REQUIRED=0.6 diff --git a/src/plugins/media-export/Makefile.am b/src/plugins/media-export/Makefile.am index 3a8d2e7..164ec29 100644 --- a/src/plugins/media-export/Makefile.am +++ b/src/plugins/media-export/Makefile.am @@ -11,6 +11,7 @@ AM_CFLAGS = $(LIBGUPNP_CFLAGS) \ librygel_media_export_la_SOURCES = rygel-media-export-plugin.vala \ rygel-media-export-root-container.vala \ + rygel-media-export-recursive-file-monitor.vala \ rygel-media-export-harvester.vala \ rygel-media-export-item.vala @@ -21,8 +22,7 @@ librygel_media_export_la_VALAFLAGS = --vapidir=$(top_srcdir)/src/rygel \ --pkg gupnp-av-1.0 \ --pkg libsoup-2.4 \ --pkg gee-1.0 \ - --pkg libxml-2.0 \ - --pkg gconf-2.0 + --pkg libxml-2.0 librygel_media_export_la_LIBADD = $(LIBGUPNP_LIBS) \ $(LIBGUPNP_AV_LIBS) \ 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 new file mode 100644 index 0000000..9a11cb2 --- /dev/null +++ b/src/plugins/media-export/rygel-media-export-recursive-file-monitor.vala @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2009 Jens Georg . + * + * Author: Jens Georg + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + */ + +using Gee; +using Rygel; + +public class Rygel.MediaExportRecursiveFileMonitor : Object { + private Cancellable cancellable; + HashMap monitors; + + public MediaExportRecursiveFileMonitor (Cancellable? cancellable) { + this.cancellable = cancellable; + this.monitors = new HashMap (GLib.file_hash, + GLib.file_equal); + } + + public void on_monitor_changed (File file, + File? other_file, + FileMonitorEvent event_type) { + changed (file, other_file, event_type); + + switch (event_type) { + case FileMonitorEvent.CREATED: + this.monitor (file); + break; + case FileMonitorEvent.DELETED: + var file_monitor = this.monitors.get (file); + if (file_monitor != null) { + debug ("Directory %s gone, removing watch", + file.get_uri ()); + this.monitors.remove (file); + file_monitor.cancel (); + file_monitor.changed.disconnect (this.on_monitor_changed); + } + break; + default: + // do nothing + break; + } + } + + private void on_info_ready (Object source, AsyncResult res) { + var file = (File) source; + + try { + var info = file.query_info_finish (res); + if (info.get_file_type () == FileType.DIRECTORY) { + debug ("Setting up monitor for %s", + file.get_uri ()); + var file_monitor = file.monitor_directory ( + FileMonitorFlags.NONE, + this.cancellable); + 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 ()); + } + } + + 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 (); + } else { + foreach (var monitor in this.monitors.get_values ()) { + monitor.cancel (); + } + } + } + + public signal void changed (File file, + File? other_file, + FileMonitorEvent event_type); +} diff --git a/src/plugins/media-export/rygel-media-export-root-container.vala b/src/plugins/media-export/rygel-media-export-root-container.vala index 51e8c5c..eafc14b 100644 --- a/src/plugins/media-export/rygel-media-export-root-container.vala +++ b/src/plugins/media-export/rygel-media-export-root-container.vala @@ -25,9 +25,10 @@ using Gee; */ public class Rygel.MediaExportRootContainer : Rygel.MediaDBContainer { private MetadataExtractor extractor; - private Gee.ArrayList harvester; + private ArrayList harvester; + private MediaExportRecursiveFileMonitor monitor; - private Gee.ArrayList get_uris () { + private ArrayList get_uris () { ArrayList uris; var config = MetaConfig.get_default (); @@ -65,7 +66,8 @@ public class Rygel.MediaExportRootContainer : Rygel.MediaDBContainer { base (db, "0", "MediaExportRoot"); this.extractor = new MetadataExtractor (); - this.harvester = new Gee.ArrayList (); + this.harvester = new ArrayList (); + this.monitor = new MediaExportRecursiveFileMonitor (null); var uris = get_uris (); -- 2.7.4