~ubuntu-branches/ubuntu/saucy/zeitgeist/saucy

« back to all changes in this revision

Viewing changes to extensions/ds-registry.vala

  • Committer: Luke Yelavich
  • Date: 2013-06-26 00:25:54 UTC
  • mfrom: (28.1.2 zeitgeist)
  • Revision ID: luke.yelavich@canonical.com-20130626002554-k570ix39vjum9v9p
Tags: 0.9.14-0ubuntu1
Merge branch lp:~zeitgeist/zeitgeist/saucy-packaging-0-9-14

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 *
24
24
 */
25
25
 
 
26
using Zeitgeist;
 
27
 
26
28
namespace Zeitgeist
27
29
{
28
 
    [DBus (name = "org.gnome.zeitgeist.DataSourceRegistry")]
29
 
    public interface RemoteRegistry: Object
30
 
    {
31
 
        [DBus (signature = "a(sssa(asaasay)bxb)")]
32
 
        public abstract Variant get_data_sources () throws Error;
33
 
        public abstract bool register_data_source (string unique_id,
34
 
            string name, string description,
35
 
            [DBus (signature = "a(asaasay)")] Variant event_templates, BusName? sender)
36
 
            throws Error;
37
 
        public abstract void set_data_source_enabled (string unique_id,
38
 
            bool enabled) throws Error;
39
 
        [DBus (signature = "(sssa(asaasay)bxb)")]
40
 
        public abstract Variant get_data_source_from_id (string id) throws Error;
41
 
 
42
 
        public signal void data_source_disconnected (
43
 
            [DBus (signature = "(sssa(asaasay)bxb)")] Variant data_source);
44
 
        public signal void data_source_enabled (string unique_id,
45
 
            bool enabled);
46
 
        public signal void data_source_registered (
47
 
            [DBus (signature = "(sssa(asaasay)bxb)")] Variant data_source);
48
 
    }
49
 
 
50
 
    class DataSource: Object
51
 
    {
52
 
        public string unique_id { get; set; }
53
 
        public string name { get; set; }
54
 
        public string description { get; set; }
55
 
 
56
 
        public GenericArray<Event>? event_templates { get; set; }
57
 
 
58
 
        public bool enabled { get; set; }
59
 
        public bool running { get; set; }
60
 
        public int64 timestamp { get; set; }
61
 
 
62
 
        public DataSource ()
63
 
        {
64
 
            Object ();
65
 
        }
66
 
 
67
 
        public DataSource.full (string unique_id, string name,
68
 
            string description, GenericArray<Event> templates)
69
 
        {
70
 
            Object (unique_id: unique_id, name: name, description: description,
71
 
                event_templates: templates);
72
 
        }
73
 
 
74
 
        public DataSource.from_variant (Variant variant,
75
 
            bool reset_running=false) throws EngineError
76
 
        {
77
 
            warn_if_fail (
78
 
                variant.get_type_string () == "(sssa("+Utils.SIG_EVENT+")bxb)"
79
 
                || variant.get_type_string () == "sssa("+Utils.SIG_EVENT+")");
80
 
            var iter = variant.iterator ();
81
 
 
82
 
            assert (iter.n_children () >= 4);
83
 
            unique_id = iter.next_value ().get_string ();
84
 
            name = iter.next_value ().get_string ();
85
 
            description = iter.next_value ().get_string ();
86
 
            event_templates = Events.from_variant (iter.next_value ());
87
 
 
88
 
            if (iter.n_children () > 4)
89
 
            {
90
 
                running = iter.next_value ().get_boolean ();
91
 
                if (reset_running)
92
 
                    running = false;
93
 
                timestamp = iter.next_value ().get_int64 ();
94
 
                enabled = iter.next_value ().get_boolean ();
95
 
            }
96
 
        }
97
 
 
98
 
        public Variant to_variant ()
99
 
        {
100
 
            var vb = new VariantBuilder (new VariantType (
101
 
                "(sssa("+Utils.SIG_EVENT+")bxb)"));
102
 
 
103
 
            vb.add ("s", unique_id);
104
 
            vb.add ("s", name);
105
 
            vb.add ("s", description);
106
 
            if (event_templates != null && event_templates.length > 0)
107
 
            {
108
 
                vb.add_value (Events.to_variant (event_templates));
109
 
            }
110
 
            else
111
 
            {
112
 
                vb.open (new VariantType ("a("+Utils.SIG_EVENT+")"));
113
 
                vb.close ();
114
 
            }
115
 
 
116
 
            vb.add ("b", running);
117
 
            vb.add ("x", timestamp);
118
 
            vb.add ("b", enabled);
119
 
 
120
 
            return vb.end ();
121
 
        }
122
 
    }
123
30
 
124
31
    namespace DataSources
125
32
    {
126
 
        private const string SIG_DATASOURCES =
127
 
            "a(sssa("+Utils.SIG_EVENT+")bxb)";
128
 
 
129
 
        private static HashTable<string, DataSource> from_variant (
130
 
            Variant sources_variant, bool reset_running=false) throws EngineError
 
33
        private static HashTable<string, DataSource> registry_from_variant (
 
34
            Variant sources_variant, bool reset_running=false) throws DataModelError
131
35
        {
132
36
            var registry = new HashTable<string, DataSource> (
133
37
                str_hash, str_equal);
134
38
 
135
39
            warn_if_fail (
136
 
                sources_variant.get_type_string() == SIG_DATASOURCES);
 
40
                sources_variant.get_type_string() == DataSources.SIG_DATASOURCES);
137
41
            foreach (Variant ds_variant in sources_variant)
138
42
            {
139
43
                DataSource ds = new DataSource.from_variant (ds_variant,
143
47
 
144
48
            return registry;
145
49
        }
146
 
 
147
 
        private static Variant to_variant (
148
 
            HashTable<string, DataSource> sources)
149
 
        {
150
 
            var vb = new VariantBuilder (new VariantType (SIG_DATASOURCES));
151
 
 
152
 
            List<unowned DataSource> data_sources = sources.get_values ();
153
 
            data_sources.sort ((a, b) =>
154
 
            {
155
 
                return strcmp (a.unique_id, b.unique_id);
156
 
            });
157
 
 
158
 
            foreach (unowned DataSource ds in data_sources)
159
 
            {
160
 
                vb.add_value (ds.to_variant ());
161
 
            }
162
 
 
163
 
            return vb.end ();
164
 
        }
165
50
    }
166
51
 
167
 
    class DataSourceRegistry: Extension, RemoteRegistry
 
52
    public class DataSourceRegistryExtension: Extension, RemoteRegistry
168
53
    {
169
54
        private const string MULTIPLE_MARKER = "<multiple>";
170
55
        private HashTable<string, DataSource> sources;
175
60
 
176
61
        private static const uint DISK_WRITE_TIMEOUT = 5 * 60; // 5 minutes
177
62
 
178
 
        DataSourceRegistry ()
 
63
        DataSourceRegistryExtension ()
179
64
        {
180
65
            Object ();
181
66
        }
192
77
            {
193
78
                try
194
79
                {
195
 
                    sources = DataSources.from_variant (registry, true);
 
80
                    sources = DataSources.registry_from_variant (registry, true);
196
81
                }
197
 
                catch (EngineError e)
 
82
                catch (DataModelError e)
198
83
                {
199
84
                    warning ("Error while loading datasource registry: %s", e.message);
200
85
                    sources = new HashTable<string, DataSource> (
245
130
            }
246
131
 
247
132
            flush ();
248
 
            debug ("%s, this.ref_count = %u", Log.METHOD, this.ref_count);
 
133
            debug ("%s, this.ref_count = %u", GLib.Log.METHOD, this.ref_count);
249
134
        }
250
135
 
251
 
        public Variant get_data_sources ()
 
136
        public async Variant get_data_sources (Cancellable? cancellable=null)
252
137
        {
253
138
            return DataSources.to_variant (sources);
254
139
        }
264
149
            return false;
265
150
        }
266
151
 
267
 
        public bool register_data_source (string unique_id, string name,
268
 
            string description, Variant event_templates, BusName? sender) throws EngineError
 
152
        public async bool register_data_source (string unique_id,
 
153
            string name, string description, Variant event_templates,
 
154
            Cancellable? cancellable=null, BusName? sender)
 
155
            throws DataModelError
269
156
        {
270
 
            debug ("%s: %s, %s, %s", Log.METHOD, unique_id, name, description);
 
157
            debug ("%s: %s, %s, %s", GLib.Log.METHOD, unique_id,
 
158
                name, description);
271
159
            if (sender == null)
272
160
            {
273
 
                warning ("%s: sender == null, ignoring request", Log.METHOD);
 
161
                warning ("%s: sender == null, ignoring request", GLib.Log.METHOD);
274
162
                return false;
275
163
            }
276
164
 
297
185
                bus_name_2_ds.insert (sender, MULTIPLE_MARKER);
298
186
            }
299
187
 
 
188
            GenericArray<Event> templates; // Vala: block scopes in async buggy
300
189
            unowned DataSource? ds = sources.lookup (unique_id);
301
190
            if (ds != null)
302
191
            {
303
 
                var templates = Events.from_variant (event_templates);
 
192
                templates = Events.from_variant (event_templates);
304
193
                ds.name = name;
305
194
                ds.description = description;
306
195
                ds.event_templates = templates;
307
 
                ds.timestamp = Timestamp.now ();
 
196
                ds.timestamp = Timestamp.from_now ();
308
197
                ds.running = true;
309
198
                dirty = true;
310
199
 
314
203
            }
315
204
            else
316
205
            {
317
 
                var templates = Events.from_variant (event_templates);
 
206
                templates = Events.from_variant (event_templates);
318
207
                DataSource new_ds = new DataSource.full (unique_id, name,
319
208
                    description, templates);
320
209
                new_ds.enabled = true;
321
210
                new_ds.running = true;
322
 
                new_ds.timestamp = Timestamp.now ();
 
211
                new_ds.timestamp = Timestamp.from_now ();
323
212
                sources.insert (unique_id, new_ds);
324
213
                dirty = true;
325
214
 
330
219
 
331
220
        }
332
221
 
333
 
        public void set_data_source_enabled (string unique_id, bool enabled)
 
222
        public async void set_data_source_enabled (string unique_id, bool enabled,
 
223
            Cancellable? cancellable=null)
334
224
        {
335
 
            debug ("%s: %s, %d", Log.METHOD, unique_id, (int) enabled);
 
225
            debug ("%s: %s, %d", GLib.Log.METHOD, unique_id, (int) enabled);
336
226
            unowned DataSource? ds = sources.lookup (unique_id);
337
227
            if (ds != null)
338
228
            {
349
239
            }
350
240
        }
351
241
 
352
 
        public Variant get_data_source_from_id (string unique_id) throws Error
 
242
        public async Variant get_data_source_from_id (string unique_id,
 
243
            Cancellable? cancellable=null) throws Error
353
244
        {
354
245
            unowned DataSource? ds = sources.lookup (unique_id);
355
246
            if (ds != null)
371
262
                {
372
263
                    var data_source = sources.lookup (unique_id);
373
264
 
374
 
                    data_source.timestamp = Timestamp.now ();
 
265
                    data_source.timestamp = Timestamp.from_now ();
375
266
                    dirty = true;
376
267
 
377
268
                    // if one sender registers multiple unique data sources,
429
320
                debug ("Client disconnected: %s [%s]", ds.name, uid);
430
321
 
431
322
                // FIXME: Update here or change semantics to "last insert"?
432
 
                ds.timestamp = Timestamp.now ();
 
323
                ds.timestamp = Timestamp.from_now ();
433
324
                dirty = true;
434
325
 
435
326
                if (running_ds.lookup (uid).length == 0)
458
349
 
459
350
    [ModuleInit]
460
351
#if BUILTIN_EXTENSIONS
461
 
    public static Type data_source_registry_init (TypeModule module)
 
352
    public static Type data_source_registry_extension_init (TypeModule module)
462
353
    {
463
354
#else
464
355
    public static Type extension_register (TypeModule module)
465
356
    {
466
357
#endif
467
 
        return typeof (DataSourceRegistry);
 
358
        return typeof (DataSourceRegistryExtension);
468
359
    }
469
360
}
470
361