~manishsinha/zeitgeist-sharp/testing-ArrayMismatch

« back to all changes in this revision

Viewing changes to Zeitgeist/Datamodel/Monitor.cs

  • Committer: Manish Sinha
  • Date: 2010-10-09 20:17:55 UTC
  • Revision ID: mail@manishsinha.net-20101009201755-13ihhbz186cokptj
Added support for Monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using NDesk.DBus;
 
4
using GLib;
 
5
using System.Threading;
 
6
using System.ComponentModel;
 
7
 
 
8
namespace Zeitgeist.Datamodel
 
9
{
 
10
        [Interface("org.gnome.zeitgeist.Monitor")]
 
11
        internal interface IMonitor
 
12
        {
 
13
                void NotifyInsert(TimeRange range, RawEvent[] events);
 
14
                
 
15
                void NotifyDelete(TimeRange range, UInt32[] evendIds);
 
16
        }
 
17
        
 
18
        internal class RawMonitor : IMonitor
 
19
        {
 
20
                public RawMonitor(string monitorPath)
 
21
                {
 
22
                        ObjectPath objPath = new ObjectPath (monitorPath);
 
23
                        Bus.Session.Register (objPath, this);
 
24
                        
 
25
                        loop = new MainLoop();
 
26
                        worker = new BackgroundWorker();
 
27
                        
 
28
                        worker.DoWork += delegate(object sender, DoWorkEventArgs e) {
 
29
                                loop.Run();
 
30
                        };
 
31
                        
 
32
                        worker.RunWorkerAsync();
 
33
                }
 
34
                
 
35
                public void NotifyInsert(TimeRange range, RawEvent[] events)
 
36
                {
 
37
                        List<Event> eventList = ZsUtils.FromRawEventList(events);
 
38
                        
 
39
                        if(Inserted != null)
 
40
                                Inserted(range, eventList);
 
41
                }
 
42
                
 
43
                public void NotifyDelete(TimeRange range, UInt32[] evendIds)
 
44
                {
 
45
                        List<UInt32> eventIdList = new List<UInt32>(evendIds);
 
46
                        
 
47
                        if(Deleted != null)
 
48
                                Deleted(range, eventIdList);
 
49
                }
 
50
                
 
51
                public event NotifyInsertHandler Inserted;
 
52
                
 
53
                public event NotifyDeleteHandler Deleted;
 
54
                
 
55
                private BackgroundWorker worker;
 
56
                
 
57
                private MainLoop loop;
 
58
        }
 
59
        
 
60
        /// <summary>
 
61
        /// DBus interface for monitoring the Zeitgeist log for certain types of events.
 
62
        /// When using the Python bindings monitors are normally instantiated indirectly by calling ZeitgeistClient.install_monitor
 
63
        /// </summary>
 
64
        /// <remarks>
 
65
        /// It is important to understand that the Monitor instance lives on the client side, and expose a 
 
66
        /// DBus service there, and the Zeitgeist engine calls back to the monitor when matching events are registered.
 
67
        /// </remarks>
 
68
        public class Monitor
 
69
        {
 
70
                /// <summary>
 
71
                /// Create a new instance of Monitor which can be used for notifying the client that some event has been 
 
72
                /// inserted or deleted which matches the event template
 
73
                /// </summary>
 
74
                /// <param name="monitorPath">
 
75
                /// The DBus path over which the bus is activated <see cref="System.String"/>
 
76
                /// </param>
 
77
                public Monitor(string monitorPath)
 
78
                {
 
79
                        _monitor = new RawMonitor( monitorPath);
 
80
                        
 
81
                        _monitor.Inserted += delegate(TimeRange event_range, List<Event> events) {
 
82
                                if(Inserted != null)
 
83
                                        Inserted(event_range, events);
 
84
                        };
 
85
                        _monitor.Deleted += delegate(TimeRange event_range, List<uint> eventIds) {
 
86
                                if(Deleted != null)
 
87
                                        Deleted(event_range, eventIds);
 
88
                        };
 
89
                }
 
90
                
 
91
                /// <summary>
 
92
                /// The event which is fired in the case of an event is inserted which matches the Event Templates
 
93
                /// </summary>
 
94
                public event NotifyInsertHandler Inserted;
 
95
                
 
96
                /// <summary>
 
97
                /// The event which is fired in the case of an event is deleted which matches the Event Templates
 
98
                /// </summary>
 
99
                public event NotifyDeleteHandler Deleted;
 
100
                
 
101
                private RawMonitor _monitor;
 
102
        }
 
103
}
 
104