Fixed #374055:Only the first "tag" is detected in digikam.
[beagle.git] / bludgeon / Action.cs
blob7ca7a9dac14c2b754289d25ae93400e5ba47ab70
2 using System;
3 using System.Collections;
5 using Beagle.Util;
7 namespace Bludgeon {
9 public class Action {
11 // Note that the way we manipulate Action.Count and closure.Id
12 // is violently non-threadsafe. Fixing this should be pretty
13 // easy, and is left as an exercise to the reader.
15 public interface Handle { }
17 // Should be private, but that is just too messy
18 // for this code.
19 static public int Count = 0;
21 private class Closure : Handle {
22 public uint Id;
24 private GLib.TimeoutHandler timeout_handler;
25 private GLib.IdleHandler idle_handler;
27 public Closure (GLib.TimeoutHandler timeout_handler)
29 this.timeout_handler = timeout_handler;
30 ++Count;
33 public Closure (GLib.IdleHandler idle_handler)
35 this.idle_handler = idle_handler;
36 ++Count;
39 public bool Handler ()
41 if (Id == 0)
42 return false;
44 bool rv = false;
45 if (timeout_handler != null)
46 rv = timeout_handler ();
47 else if (idle_handler != null)
48 rv = idle_handler ();
49 if (! rv) {
50 Id = 0;
51 --Count;
53 if (Count == 0)
54 BludgeonMain.Shutdown ();
56 return rv;
60 // RENAMEME: It is linguistically awkward that we pass in
61 // "handlers", and get back "handles".
62 static public Handle Add (GLib.IdleHandler idle_handler)
64 Closure c;
65 c = new Closure (idle_handler);
66 c.Id = GLib.Idle.Add (new GLib.IdleHandler (c.Handler));
67 return c;
70 static public Handle Add (uint t, GLib.TimeoutHandler timeout_handler)
72 Closure c;
73 c = new Closure (timeout_handler);
74 c.Id = GLib.Timeout.Add (t, new GLib.TimeoutHandler (c.Handler));
75 return c;
78 static public void Cancel (Handle h)
80 Closure c = (Closure) h;
82 if (c.Id != 0) {
83 GLib.Source.Remove (c.Id);
84 --Count;
86 if (Count == 0) {
87 // We add an empty idle task, which will increment
88 // the count up back above 0. This keeps us from
89 // shutting down the system until the next time control
90 // is passed to the main loop.
91 Add (null);