summaryrefslogtreecommitdiff
path: root/Zeitgeist/Datamodel/DataSource.cs
blob: cae66849cf0b8167a08f02a967f5ac58c57a46b9 (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
/******************************************************************************
 * The MIT/X11/Expat License
 * Copyright (c) 2010 Manish Sinha<mail@manishsinha.net>

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE. 
********************************************************************************/

using System;
using org.freedesktop;
using org.freedesktop.DBus;
using System.Collections.Generic;

namespace Zeitgeist.Datamodel
{
	/// <summary>
	/// Optimized and convenient data structure representing a datasource.
	/// </summary>
	public class DataSource 
	{
		/// <summary>
		/// The Id of the DataSource
		/// </summary>
		public string UniqueId
		{
			get;set;
		}
		
		/// <summary>
		/// The Name of the DataSource
		/// </summary>
		public string Name
		{
			get;set;
		}
		
		/// <summary>
		/// The Description of the DataSource
		/// </summary>
		public string Description
		{
			get;set;
		}
		
		/// <summary>
		/// The list of Events associated with this DataSource
		/// </summary>
		public List<Event> Events
		{
			get;set;
		}
		
		/// <summary>
		/// Is the datasouce running : TODO
		/// </summary>
		public bool Running
		{
			get;set;
		}
		
		/// <summary>
		/// Last time the DataSource did something
		/// </summary>
		public long LastSeen
		{
			get;set;
		}
		
		/// <summary>
		/// Is the DataSource Enabled : TODO
		/// </summary>
		public bool Enabled
		{
			get;set;
		}
		
		
	}
	
	/// <summary>
	/// A raw DBus  based representation of Event
	/// </summary>
	internal struct RawDataSource 
	{
		/// <summary>
		/// The Id of the RawDataSource
		/// </summary>
		public string UniqueId
		{
			get
			{
				return _uniqueId;
			}
			set
			{
				_uniqueId = value;
			}
		}
		
		/// <summary>
		/// The Name of the RawDataSource
		/// </summary>
		public string Name
		{
			get
			{
				return _name;
			}
			set
			{
				_name = value;
			}
		}
		
		/// <summary>
		/// The Description of the RawDataSource
		/// </summary>
		public string Description
		{
			get
			{
				return _description;
			}
			set
			{
				_description = value;
			}
		}
		
		/// <summary>
		/// The list of RawEvents associated with this RawDataSource
		/// </summary>
		public RawEvent[] RawEvents
		{
			get
			{
				return _rawEvents;
			}
			set
			{
				_rawEvents = value;
			}
		}
		
		/// <summary>
		/// Is the datasouce running : TODO
		/// </summary>
		public bool Running
		{
			get
			{
				return _running;
			}
			set
			{
				_running = value;
			}
		}
		
		/// <summary>
		/// Last time the RawDataSource did something
		/// </summary>
		public UInt64 LastSeen
		{
			get
			{
				return _lastSeen;
			}
			set
			{
				_lastSeen = value;
			}
		}
		
		/// <summary>
		/// Is the DataSource Enabled : TODO
		/// </summary>
		public bool Enabled
		{
			get
			{
				return _enabled;
			}
			set
			{
				_enabled = value;
			}
		}
		
		/// <summary>
		/// A constructor for creating a RawDataSource. Even though the functiionality is provided,
		/// still please create an instance of DartaSource and then use DataSource.ToRaw()
		/// </summary>
		/// <param name="uid">
		/// The RawDataSource id <see cref="System.String"/>
		/// </param>
		/// <param name="name">
		/// The name of the Source <see cref="System.String"/>
		/// </param>
		/// <param name="desc">
		/// The description of the Source  <see cref="System.String"/>
		/// </param>
		/// <param name="events">
		/// The RawEvents associated with this RawDataSource <see cref="RawEvent[]"/>
		/// </param>
		/// <param name="running">
		/// is the RawDataSource running <see cref="System.Boolean"/>
		/// </param>
		/// <param name="Lastseen">
		/// When was the last time RawDataSource was seen <see cref="UInt64"/>
		/// </param>
		/// <param name="enabled">
		/// Is the RawDataSource enabled <see cref="System.Boolean"/>
		/// </param>
		public RawDataSource(string uid, string name, string desc, RawEvent[] events, bool running, UInt64 Lastseen, bool enabled)
		{
			UniqueId = uid;
			Name = name;
			Description = desc;
			RawEvents = events;
			Running = running;
			LastSeen = Lastseen;
			Enabled = enabled;
		}
		
		/// <summary>
		/// Convert an object of type RawDataSource to DataSource
		/// </summary>
		/// <param name="raw">
		/// An instance of RawDataSource <see cref="RawDataSource"/>
		/// </param>
		/// <returns>
		/// An instance of DataSource <see cref="DataSource"/>
		/// </returns>
		public static DataSource FromRaw(RawDataSource  raw)
		{
			DataSource src = new DataSource();
			
			src.UniqueId = raw.UniqueId;
			src.Name = raw.Name;
			src.Description = raw.Description;
			src.Running = raw.Running;
			src.LastSeen = long.Parse(raw.LastSeen.ToString());
			src.Enabled = raw.Enabled;
			
			if(raw.RawEvents != null)
			{
				src.Events = new List<Event>();
				foreach(RawEvent evn in raw.RawEvents)
				{
					Event ev = RawEvent.FromRaw(evn);
					src.Events.Add(ev);
				}
			}
			
			return src;
		}
		
		/// <summary>
		/// Convert an object of type DataSource to RawDataSource
		/// </summary>
		/// <param name="src">
		/// An instance of DataSource <see cref="Zeitgeist.Datamodel.DataSource"/>
		/// </param>
		/// <returns>
		/// An instance of RawDataSource <see cref="Zeitgeist.Datamodel.RawDataSource"/>
		/// </returns>
		public static RawDataSource ToRaw(DataSource src)
		{
			RawDataSource raw = new RawDataSource();
			
			raw.UniqueId = src.UniqueId;
			raw.Name = src.Name;
			raw.Description = src.Description;
			raw.Running = src.Running;
			raw.LastSeen = UInt64.Parse(src.LastSeen.ToString());
			raw.Enabled = src.Enabled;
			
			if(raw.RawEvents != null)
			{
				List<RawEvent> events = new List<RawEvent>();
				foreach(Event evn in src.Events)
				{
					RawEvent ev = evn.GetRawEvent();
					events.Add(ev);
				}
			}
			
			return raw;
		}
		
		#region Private Fields
		
		private string _uniqueId;
		private string _name;
		private string _description;
		private RawEvent[] _rawEvents;
		private bool _running;
		private UInt64 _lastSeen;
		private bool _enabled;
		
		#endregion
	}
}