core,gst-renderer: Move ChangeLog to core
[profile/ivi/rygel.git] / src / rygel / rygel-changelog.vala
1 /*
2  * Copyright (C) 2008 OpenedHand Ltd.
3  * Copyright (C) 2009 Nokia Corporation.
4  *
5  * Author: Jorn Baayen <jorn@openedhand.com>
6  *         Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
7  *                               <zeeshan.ali@nokia.com>
8  *
9  * Rygel is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Rygel is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 using GUPnP;
25 using Gee;
26
27 // Helper class for building LastChange messages
28 public class Rygel.ChangeLog : Object {
29     public unowned Service service { get; set; }
30
31     private string service_ns;
32
33     private StringBuilder str;
34
35     private HashMap<string, string> hash;
36
37     private uint timeout_id = 0;
38
39     public ChangeLog (Service? service, string service_ns) {
40         this.service = service;
41         this.service_ns = service_ns;
42         this.str = new StringBuilder ();
43         this.hash = new HashMap<string, string> ();
44     }
45
46     ~ChangeLog () {
47         if (this.timeout_id != 0) {
48             Source.remove (this.timeout_id);
49         }
50     }
51
52     private bool timeout () {
53         // Emit notification
54         this.service.notify ("LastChange", typeof (string), this.finish ());
55         debug ("LastChange sent");
56
57         // Reset
58         this.hash.clear ();
59         this.str.erase (0, -1);
60         this.timeout_id = 0;
61
62         return false;
63     }
64
65     private void ensure_timeout () {
66         // Make sure we have a notification timeout
67         if (this.service != null && this.timeout_id == 0) {
68             debug (_("Setting up timeout for LastChange"));
69             this.timeout_id = Timeout.add (200, this.timeout);
70         }
71     }
72
73     public void log (string variable, string value) {
74         debug (@"'%s = %s' logged", variable, value);
75         this.hash.set (variable, "<%s val=\"%s\"/>".printf (variable, value));
76
77         this.ensure_timeout ();
78     }
79
80     public void log_with_channel (string variable,
81                                   string value,
82                                   string channel) {
83         this.hash.set (variable,
84                        "<%s val=\"%s\" channel=\"%s\"/>".printf (variable,
85                                                                  value,
86                                                                  channel));
87
88         this.ensure_timeout ();
89     }
90
91     public string finish () {
92         this.str.append ("<Event xmlns=\"" +
93                          this.service_ns +
94                          "\"><InstanceID val=\"0\">");
95         foreach (string line in this.hash.values) {
96             this.str.append (line);
97         }
98         this.str.append ("</InstanceID></Event>");
99
100         return this.str.str;
101     }
102 }