summaryrefslogtreecommitdiff
path: root/src/plugins/media-export/rygel-media-export-harvester.vala
blob: 3c4f3fd332348fb60bec79dfda5f5d6dd6723aef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2010 Jens Georg <mail@jensge.org>.
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using Gee;

/**
 * This class takes care of the book-keeping of running and finished
 * extraction tasks running within the media-export plugin
 */
internal class Rygel.MediaExport.Harvester : GLib.Object {
    private const uint FILE_CHANGE_DEFAULT_GRACE_PERIOD = 5;

    private HashMap<File, HarvestingTask> tasks;
    private HashMap<File, uint> extraction_grace_timers;
    private RecursiveFileMonitor monitor;
    private Cancellable cancellable;

    // Properties
    public ArrayList<File> locations { get; private set; }

    public signal void done ();

    /**
     * Create a new instance of the meta-data extraction manager.
     */
    public Harvester (Cancellable     cancellable,
                      ArrayList<File> locations) {
        this.cancellable = cancellable;
        this.locations = new ArrayList<File> ((EqualDataFunc<File>) File.equal);
        foreach (var file in locations) {
            if (file.query_exists ()) {
                this.locations.add (file);
            }
        }

        this.monitor = new RecursiveFileMonitor (cancellable);
        this.monitor.changed.connect (this.on_file_changed);

        this.tasks = new HashMap<File, HarvestingTask>
                                        ((HashDataFunc<File>) File.hash,
                                         (EqualDataFunc<File>) File.equal);
        this.extraction_grace_timers = new HashMap<File, uint>
                                        ((HashDataFunc<File>) File.hash,
                                         (EqualDataFunc<File>) File.equal);
    }

    /**
     * Check if a FileInfo is considered for extraction
     *
     * @param info a FileInfo
     * @return true if file should be extracted, false otherwise
     */
    public static bool is_eligible (File file, FileInfo info) {
        var is_supported_content_type =
            info.get_content_type ().has_prefix ("image/") ||
            info.get_content_type ().has_prefix ("video/") ||
            info.get_content_type ().has_prefix ("audio/") ||
            info.get_content_type () == "application/ogg" ||
            info.get_content_type () == "application/xml" ||
            info.get_content_type () == "text/xml" ||
            info.get_content_type () == "text/plain" ||
            info.get_content_type () == "application/x-cd-image";
        var cache = MediaCache.get_default ();
        var is_ignored = cache.is_ignored (file);

        if (is_ignored) {
            debug ("URI %s is not eligible due, it is ignored",
                   file.get_uri ());
        }

        return is_supported_content_type && ! is_ignored;
    }

    /**
     * Schedule rescan of all top-level locations known to the harvester.
     *
     * @param parent top-level container of the files
     */
    public void schedule_locations (MediaContainer parent) {
        foreach (var file in this.locations) {
            this.schedule (file, parent);
        }
    }

    /**
     * Put a file on queue for meta-data extraction
     *
     * @param file the file to investigate
     * @param parent container of the filer to be harvested
     */
    public void schedule (File           file,
                          MediaContainer parent) {
        this.extraction_grace_timers.unset (file);

        // Cancel a probably running harvester
        this.cancel (file);

        var task = new HarvestingTask (this.monitor,
                                       file,
                                       parent);
        task.cancellable = this.cancellable;
        task.completed.connect (this.on_file_harvested);
        this.tasks[file] = task;
        task.run.begin ();
    }

    /**
     * Cancel a running meta-data extraction run
     *
     * @param file file cancel the current run for
     */
    public void cancel (File file) {
        if (this.tasks.has_key (file)) {
            var task = this.tasks[file];
            task.completed.disconnect (this.on_file_harvested);
            this.tasks.unset (file);
            task.cancel ();
        }
    }

    /**
     * Callback for finished harvester.
     *
     * Updates book-keeping hash.
     * @param state_machine HarvestingTask sending the event
     */
    private void on_file_harvested (StateMachine state_machine) {
        var task = state_machine as HarvestingTask;
        var file = task.origin;
        message (_("“%s” harvested"), file.get_uri ());

        this.tasks.unset (file);
        if (this.tasks.is_empty) {
            done ();
        }
    }

    private void on_file_changed (File             file,
                                  File?            other,
                                  FileMonitorEvent event) {
        try {
            switch (event) {
                case FileMonitorEvent.CREATED: {
                    var info = file.query_info (FileAttribute.STANDARD_TYPE,
                                                FileQueryInfoFlags.NONE,
                                                this.cancellable);

                    if (info.get_file_type () == FileType.DIRECTORY) {
                        this.on_changes_done (file);
                    }
                    break;
                }
                case FileMonitorEvent.CHANGES_DONE_HINT:
                    this.on_changes_done (file);
                    break;
                case FileMonitorEvent.DELETED:
                    this.on_file_removed (file);
                    break;
                default:
                    break;
            }
        } catch (Error error) {
            debug ("Failed to query information for %s: %s",
                   file.get_uri (),
                   error.message);
        }
    }

    private void on_file_added (File file) {
        debug ("Filesystem events settled for %s, scheduling extraction…",
               file.get_uri ());
        try {
            var cache = MediaCache.get_default ();
            var info = file.query_info (FileAttribute.STANDARD_TYPE + "," +
                                        FileAttribute.STANDARD_CONTENT_TYPE,
                                        FileQueryInfoFlags.NONE,
                                        this.cancellable);
            if (info.get_file_type () == FileType.DIRECTORY ||
                Harvester.is_eligible (file, info)) {
                var id = MediaCache.get_id (file.get_parent ());
                try {
                    var parent_container = cache.get_object (id)
                                        as MediaContainer;
                    this.schedule (file, parent_container);
                } catch (Database.DatabaseError error) {
                    warning (_("Error fetching object “%s” from database: %s"),
                            id,
                            error.message);
                }
            } else {
                debug ("%s is not eligible for extraction", file.get_uri ());
            }
        } catch (Error error) {
            warning (_("Failed to query info of a file %s: %s"),
                     file.get_uri (),
                     error.message);
        }
    }

    private void on_file_removed (File file) {
        var cache = MediaCache.get_default ();
        if (this.extraction_grace_timers.has_key (file)) {
            Source.remove (this.extraction_grace_timers[file]);
            this.extraction_grace_timers.unset (file);
        }

        this.cancel (file);
        try {
            // the full object is fetched instead of simply calling
            // exists because we need the parent to signal the
            // change
            var id = MediaCache.get_id (file);
            var object = cache.get_object (id);

            if (object != null && object.parent != null) {
                var parent = object.parent;

                if (parent is WritableDbContainer) {
                    var container = parent as WritableDbContainer;

                    container.remove_tracked (object);
                } else if (parent is TrackableDbContainer) {
                    // This should not be possible, but just to be sure.
                    var container = parent as TrackableContainer;

                    container.remove_child_tracked.begin (object);
                }
            } else {
                warning (_("Could not find object %s or its parent. Database is inconsistent"),
                         id);
            }
        } catch (Error error) {
            warning (_("Error removing object from database: %s"),
                     error.message);
        }
    }

    private void on_changes_done (File file) throws Error {
        if (file.get_basename ().has_prefix (".")) {
            return;
        }

        var period = FILE_CHANGE_DEFAULT_GRACE_PERIOD;
        try {
            var config = MetaConfig.get_default ();
            period = config.get_int ("MediaExport",
                                     "monitor-grace-timeout",
                                     0,
                                     500);
        } catch (Error error) { }


        if (this.extraction_grace_timers.has_key (file)) {
            Source.remove (this.extraction_grace_timers[file]);
        } else if (period > 0) {
            debug ("Starting grace timer for harvesting %s…",
                    file.get_uri ());
        }

        SourceFunc callback = () => {
            this.on_file_added (file);

            return false;
        };

        if (period > 0) {
            var timeout = Timeout.add_seconds (FILE_CHANGE_DEFAULT_GRACE_PERIOD,
                                               (owned) callback);
            this.extraction_grace_timers[file] = timeout;
        } else {
            Idle.add ((owned) callback);
        }
    }
}