~ubuntu-branches/ubuntu/precise/rygel/precise

« back to all changes in this revision

Viewing changes to src/plugins/media-export/rygel-media-export-recursive-file-monitor.vala

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Henriksson
  • Date: 2009-09-26 14:04:05 UTC
  • Revision ID: james.westby@ubuntu.com-20090926140405-d5x3j13k10psa1gu
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Jens Georg <mail@jensge.org>.
 
3
 *
 
4
 * This file is part of Rygel.
 
5
 *
 
6
 * Rygel is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU Lesser General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * Rygel is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public License
 
17
 * along with this program; if not, write to the Free Software Foundation,
 
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
 
 
22
using Gee;
 
23
using Rygel;
 
24
 
 
25
public class Rygel.MediaExportRecursiveFileMonitor : Object {
 
26
    private Cancellable cancellable;
 
27
    HashMap<File, FileMonitor> monitors;
 
28
 
 
29
    public MediaExportRecursiveFileMonitor (Cancellable? cancellable) {
 
30
        this.cancellable = cancellable;
 
31
        this.monitors = new HashMap<File, FileMonitor> (GLib.file_hash,
 
32
                                                        GLib.file_equal);
 
33
    }
 
34
 
 
35
    public void on_monitor_changed (File             file,
 
36
                                    File?            other_file,
 
37
                                    FileMonitorEvent event_type) {
 
38
        changed (file, other_file, event_type);
 
39
 
 
40
        switch (event_type) {
 
41
            case FileMonitorEvent.CREATED:
 
42
                this.monitor (file);
 
43
                break;
 
44
            case FileMonitorEvent.DELETED:
 
45
                var file_monitor = this.monitors.get (file);
 
46
                if (file_monitor != null) {
 
47
                    debug ("Directory %s gone, removing watch",
 
48
                           file.get_uri ());
 
49
                    this.monitors.remove (file);
 
50
                    file_monitor.cancel ();
 
51
                    file_monitor.changed.disconnect (this.on_monitor_changed);
 
52
                }
 
53
                break;
 
54
            default:
 
55
                // do nothing
 
56
                break;
 
57
        }
 
58
    }
 
59
 
 
60
    private void on_info_ready (Object? source, AsyncResult res) {
 
61
        var file = (File) source;
 
62
 
 
63
        try {
 
64
            var info = file.query_info_finish (res);
 
65
            if (info.get_file_type () == FileType.DIRECTORY) {
 
66
                var file_monitor = file.monitor_directory (
 
67
                                                         FileMonitorFlags.NONE,
 
68
                                                         this.cancellable);
 
69
                this.monitors.set (file, file_monitor);
 
70
                file_monitor.changed.connect (this.on_monitor_changed);
 
71
            }
 
72
        } catch (Error error) {
 
73
            warning ("Failed to get file info for %s",
 
74
                     file.get_uri ());
 
75
        }
 
76
    }
 
77
 
 
78
    public void monitor (File file) {
 
79
        file.query_info_async (FILE_ATTRIBUTE_STANDARD_TYPE,
 
80
                               FileQueryInfoFlags.NONE,
 
81
                               Priority.DEFAULT,
 
82
                               null,
 
83
                               this.on_info_ready);
 
84
    }
 
85
 
 
86
    public void cancel () {
 
87
        if (this.cancellable != null) {
 
88
            this.cancellable.cancel ();
 
89
        } else {
 
90
            foreach (var monitor in this.monitors.get_values ()) {
 
91
                monitor.cancel ();
 
92
            }
 
93
        }
 
94
    }
 
95
 
 
96
    public signal void changed (File             file,
 
97
                                File?            other_file,
 
98
                                FileMonitorEvent event_type);
 
99
}