summaryrefslogtreecommitdiff
path: root/src/MediaMonitor.vala
blob: 5d9c488032c801bb781af168974c5c0f7dfc6b67 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* Copyright 2010-2015 Yorba Foundation
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

public class MonitorableUpdates {
    public Monitorable monitorable;
    
    private File? master_file = null;
    private bool master_file_info_altered = false;
    private FileInfo? master_file_info = null;
    private bool master_in_alteration = false;
    private bool online = false;
    private bool offline = false;
    
    public MonitorableUpdates(Monitorable monitorable) {
        this.monitorable = monitorable;
    }
    
    public File? get_master_file() {
        return master_file;
    }
    
    public FileInfo? get_master_file_info() {
        return master_file_info;
    }
    
    public virtual bool is_in_alteration() {
        return master_in_alteration;
    }
    
    public bool is_set_offline() {
        return offline;
    }
    
    public bool is_set_online() {
        return online;
    }
    
    public virtual void set_master_file(File? file) {
        master_file = file;
        
        if (file != null)
            mark_online();
    }
    
    public virtual void set_master_file_info_altered(bool altered) {
        master_file_info_altered = altered;
        
        if (altered)
            mark_online();
    }
    
    public virtual void set_master_file_info(FileInfo? info) {
        master_file_info = info;
        
        if (master_file_info == null)
            set_master_file_info_altered(false);
    }
    
    public virtual void set_master_in_alteration(bool in_alteration) {
        master_in_alteration = in_alteration;
    }
    
    public virtual void set_master_alterations_complete(FileInfo info) {
        set_master_in_alteration(false);
        set_master_file_info(info);
        mark_online();
    }
    
    public virtual void mark_offline() {
        online = false;
        offline = true;
        
        master_file_info_altered = false;
        master_file_info = null;
        master_in_alteration = false;
    }
    
    public virtual void mark_online() {
        online = true;
        offline = false;
    }
    
    public virtual void reset_online_offline() {
        online = false;
        offline = false;
    }
    
    public virtual bool is_all_updated() {
        return master_file == null
            && master_file_info_altered == false
            && master_file_info == null
            && master_in_alteration == false
            && online == false
            && offline == false;
    }
}

public abstract class MediaMonitor : Object {
    public enum DiscoveredFile {
        REPRESENTED,
        IGNORE,
        UNKNOWN
    }
    
    protected const int MAX_OPERATIONS_PER_CYCLE = 100;
    
    private const int FLUSH_PENDING_UPDATES_MSEC = 500;
    
    private MediaSourceCollection sources;
    private Cancellable cancellable;
    private Gee.HashMap<Monitorable, MonitorableUpdates> pending_updates = new Gee.HashMap<Monitorable,
        MonitorableUpdates>();
    private uint pending_updates_timer_id = 0;
    
    public MediaMonitor(MediaSourceCollection sources, Cancellable cancellable) {
        this.sources = sources;
        this.cancellable = cancellable;
        
        sources.item_destroyed.connect(on_media_source_destroyed);
        sources.unlinked_destroyed.connect(on_media_source_destroyed);
        
        pending_updates_timer_id = Timeout.add(FLUSH_PENDING_UPDATES_MSEC, on_flush_pending_updates,
            Priority.LOW);
    }
    
    ~MediaMonitor() {
        sources.item_destroyed.disconnect(on_media_source_destroyed);
        sources.unlinked_destroyed.disconnect(on_media_source_destroyed);
    }
    
    public abstract MediaSourceCollection get_media_source_collection();
    
    public virtual void close() {
    }
    
    public virtual string to_string() {
        return "MediaMonitor for %s".printf(get_media_source_collection().to_string());
    }
    
    protected virtual MonitorableUpdates create_updates(Monitorable monitorable) {
        return new MonitorableUpdates(monitorable);
    }
    
    protected virtual void on_media_source_destroyed(DataSource source) {
        remove_updates((Monitorable) source);
    }
    
    //
    // The following are called when the startup scan is initiated.
    //
    
    public virtual void notify_discovery_started() {
    }
    
    // Returns the Monitorable represented in some form by the monitors' MediaSourceCollection.
    // If DiscoveredFile.REPRESENTED is returns, monitorable should be set.
    public abstract DiscoveredFile notify_file_discovered(File file, FileInfo info,
        out Monitorable monitorable);
    
    // Returns REPRESENTED if the file has been *definitively* associated with a Monitorable,
    // in which case the file will no longer be considered unknown.  Returns IGNORE if the file
    // is known in some other case and should not be considered unknown.  Returns UNKNOWN otherwise,
    // with potentially a collection of candidates for the file.  The collection may be zero-length.
    //
    // NOTE: This method may be called after the startup scan as well.
    public abstract Gee.Collection<Monitorable>? candidates_for_unknown_file(File file, FileInfo info,
        out DiscoveredFile result);
    
    public virtual File[]? get_auxilliary_backing_files(Monitorable monitorable) {
        return null;
    }
    
    // info is null if the file was not found.  Note that master online/offline state is already
    // set by LibraryMonitor.
    public virtual void update_backing_file_info(Monitorable monitorable, File file, FileInfo? info) {
    }
    
    // Not that discovery has completed, but the MediaMonitor's role in it has finished.
    public virtual void notify_discovery_completing() {
    }
    
    //
    // The following are called after the startup scan for runtime monitoring.
    //
    
    public abstract bool is_file_represented(File file);
    
    public abstract bool notify_file_created(File file, FileInfo info);
    
    public abstract bool notify_file_moved(File old_file, File new_file, FileInfo new_file_info);
    
    public abstract bool notify_file_altered(File file);
    
    public abstract bool notify_file_attributes_altered(File file);
    
    public abstract bool notify_file_alteration_completed(File file, FileInfo info);
    
    public abstract bool notify_file_deleted(File file);
    
    protected static void mdbg(string msg) {
#if TRACE_MONITORING
        debug("%s", msg);
#endif
    }
    
    public bool has_pending_updates() {
        return pending_updates.size > 0;
    }
    
    public Gee.Collection<Monitorable> get_monitorables() {
        return pending_updates.keys;
    }
    
    // This will create a MonitorableUpdates and register it with this updater if not already
    // exists.
    public MonitorableUpdates fetch_updates(Monitorable monitorable) {
        MonitorableUpdates? updates = pending_updates.get(monitorable);
        if (updates != null)
            return updates;
        
        updates = create_updates(monitorable);
        pending_updates.set(monitorable, updates);
        
        return updates;
    }
    
    public MonitorableUpdates? get_existing_updates(Monitorable monitorable) {
        return pending_updates.get(monitorable);
    }
    
    public void remove_updates(Monitorable monitorable) {
        pending_updates.unset(monitorable);
    }
    
    public bool is_online(Monitorable monitorable) {
        MonitorableUpdates? updates = get_existing_updates(monitorable);
        
        return (updates != null) ? updates.is_set_online() : !monitorable.is_offline();
    }
    
    public bool is_offline(Monitorable monitorable) {
        MonitorableUpdates? updates = get_existing_updates(monitorable);
        
        return (updates != null) ? updates.is_set_offline() : monitorable.is_offline();
    }
    
    public File get_master_file(Monitorable monitorable) {
        MonitorableUpdates? updates = get_existing_updates(monitorable);
        
        return (updates != null && updates.get_master_file() != null) ? updates.get_master_file()
            : monitorable.get_master_file();
    }
    
    public void update_master_file(Monitorable monitorable, File file) {
        fetch_updates(monitorable).set_master_file(file);
    }
    
    public void update_master_file_info_altered(Monitorable monitorable) {
        fetch_updates(monitorable).set_master_file_info_altered(true);
    }
    
    public void update_master_file_in_alteration(Monitorable monitorable, bool in_alteration) {
        fetch_updates(monitorable).set_master_in_alteration(in_alteration);
    }
    
    public void update_master_file_alterations_completed(Monitorable monitorable, FileInfo info) {
        fetch_updates(monitorable).set_master_alterations_complete(info);
    }
    
    public void update_online(Monitorable monitorable) {
        fetch_updates(monitorable).mark_online();
    }
    
    public void update_offline(Monitorable monitorable) {
        fetch_updates(monitorable).mark_offline();
    }
    
    // Children should call this method before doing their own processing.  Every operation should
    // be recorded by incrementing op_count.  If it is greater than MAX_OPERATIONS_PER_CYCLE,
    // the method should process what has been done and exit to let the operations be handled in
    // the next cycle.
    protected virtual void process_updates(Gee.Collection<MonitorableUpdates> all_updates,
        TransactionController controller, ref int op_count) throws Error {
        Gee.Map<Monitorable, File> set_master_file = null;
        Gee.Map<Monitorable, FileInfo> set_master_file_info = null;
        Gee.ArrayList<MediaSource> to_offline = null;
        Gee.ArrayList<MediaSource> to_online = null;
        
        foreach (MonitorableUpdates updates in all_updates) {
            if (op_count >= MAX_OPERATIONS_PER_CYCLE)
                break;
            
            if (updates.get_master_file() != null) {
                if (set_master_file == null)
                    set_master_file = new Gee.HashMap<Monitorable, File>();
                
                set_master_file.set(updates.monitorable, updates.get_master_file());
                updates.set_master_file(null);
                op_count++;
            }
            
            if (updates.get_master_file_info() != null) {
                if (set_master_file_info == null)
                    set_master_file_info = new Gee.HashMap<Monitorable, FileInfo>();
                
                set_master_file_info.set(updates.monitorable, updates.get_master_file_info());
                updates.set_master_file_info(null);
                op_count++;
            }
            
            if (updates.is_set_offline()) {
                if (to_offline == null)
                    to_offline = new Gee.ArrayList<LibraryPhoto>();
                
                to_offline.add(updates.monitorable);
                updates.reset_online_offline();
                op_count++;
            }
            
            if (updates.is_set_online()) {
                if (to_online == null)
                    to_online = new Gee.ArrayList<LibraryPhoto>();
                
                to_online.add(updates.monitorable);
                updates.reset_online_offline();
                op_count++;
            }
        }
        
        if (set_master_file != null) {
            mdbg("Changing master file of %d objects in %s".printf(set_master_file.size, to_string()));
            
            Monitorable.set_many_master_file(set_master_file, controller);
        }
        
        if (set_master_file_info != null) {
            mdbg("Updating %d master files timestamps in %s".printf(set_master_file_info.size,
                to_string()));
            
            Monitorable.set_many_master_timestamp(set_master_file_info, controller);
        }
        
        if (to_offline != null || to_online != null) {
            mdbg("Marking %d online, %d offline in %s".printf(
                (to_online != null) ? to_online.size : 0,
                (to_offline != null) ? to_offline.size : 0,
                to_string()));
            
            Monitorable.mark_many_online_offline(to_online, to_offline, controller);
        }
    }
    
    private bool on_flush_pending_updates() {
        if (cancellable.is_cancelled())
            return false;
        
        if (pending_updates.size == 0)
            return true;
        
        Timer timer = new Timer();
        
        // build two lists: one, of MonitorableUpdates that are not in_alteration() (which
        // simplifies matters), and two, of completed MonitorableUpdates that should be removed
        // from the list (which would have happened after the last pass)
        Gee.ArrayList<MonitorableUpdates> to_process = null;
        Gee.ArrayList<Monitorable> to_remove = null;
        foreach (MonitorableUpdates updates in pending_updates.values) {
            if (updates.is_in_alteration())
                continue;
            
            if (updates.is_all_updated()) {
                if (to_remove == null)
                    to_remove = new Gee.ArrayList<Monitorable>();
                
                to_remove.add(updates.monitorable);
                continue;
            }
            
            if (to_process == null)
                to_process = new Gee.ArrayList<MonitorableUpdates>();
            
            to_process.add(updates);
        }
        
        int op_count = 0;
        if (to_process != null) {
            TransactionController controller = get_media_source_collection().transaction_controller;
            
            try {
                controller.begin();
                process_updates(to_process, controller, ref op_count);
                controller.commit();
            } catch (Error err) {
                if (err is DatabaseError)
                    AppWindow.database_error((DatabaseError) err);
                else
                    AppWindow.panic(_("Unable to process monitoring updates: %s").printf(err.message));
            }
        }
        
        if (to_remove != null) {
            foreach (Monitorable monitorable in to_remove)
                remove_updates(monitorable);
        }
        
        double elapsed = timer.elapsed();
        if (elapsed > 0.01 || op_count > 0) {
            mdbg("Total pending queue time for %s: %lf (%d ops)".printf(to_string(), elapsed,
                op_count));
        }
        
        return true;
    }
}