Disable AudioComponent do get everything to build nicely.
[chatter.git] / PersistientState.cs
blobaadb62886da0c7ad7cc9b1c57f8d7c6cf3f40955
1 using System;
2 using System.IO;
3 using System.Xml.Serialization;
5 namespace Chatter
7 public delegate void StateCommitHandler (object obj, bool now);
9 [XmlRootAttribute ("State")]
10 public class PersistientState
12 uint timeout_id;
13 AccountStore account_store = new AccountStore ();
15 public PersistientState () { }
17 public static PersistientState FromNew ()
19 PersistientState state = new PersistientState ();
20 state.account_store.CommitRequired += state.CommitRequired;
21 return state;
24 public static PersistientState FromStream (Stream stream)
26 XmlSerializer serializer = new XmlSerializer (typeof (PersistientState));
27 PersistientState state = (PersistientState) serializer.Deserialize (stream);
28 state.account_store.CommitRequired += state.CommitRequired;
29 state.account_store.LinkAccounts ();
30 return state;
33 public void CommitRequired (object obj, bool now)
35 if (timeout_id > 0) {
36 GLib.Source.Remove (timeout_id);
37 timeout_id = 0;
40 if (now)
41 Serialize ();
42 else
43 timeout_id = GLib.Timeout.Add (10 * 1000, Timeout);
46 //Forget any timeouts and commit if required
47 public void OutOfTime ()
49 if (timeout_id > 0) {
50 GLib.Source.Remove (timeout_id);
51 Serialize ();
55 public bool Timeout ()
57 Serialize ();
58 return false;
61 public void Serialize ()
63 Global.Logger.Message ("Writing config file at {0}", Global.Paths.ConfigFile);
64 try {
65 if (!File.Exists (Global.Paths.ConfigFile)) {
66 if (!Directory.Exists (Global.Paths.ConfigDir))
67 Directory.CreateDirectory (Global.Paths.ConfigDir);
70 XmlSerializer serializer = new XmlSerializer (typeof (PersistientState));
71 //serializer.UnknownNode += new XmlNodeEventHandler (SerializerUnknownNode);
72 //serializer.UnknownAttribute += new XmlAttributeEventHandler (SerializerUnknownAttribute);
74 TextWriter writer = new StreamWriter (Global.Paths.ConfigFile);
75 serializer.Serialize (writer, this);
76 writer.Close ();
77 } catch (Exception e) {
78 Global.Logger.Error ("Failed to write application state: {0}", e.Message);
82 [XmlElement ("AccountStore")]
83 public AccountStore AccountStore
85 get { return account_store; }
86 set { account_store = value; }
90 protected void SerializerUnknownNode (object sender, XmlNodeEventArgs e)
92 Console.WriteLine("Unknown Node:" + e.Name + "\t" + e.Text);
95 protected void SerializerUnknownAttribute (object sender, XmlAttributeEventArgs e)
97 System.Xml.XmlAttribute attr = e.Attr;
98 Console.WriteLine("Unknown attribute " + attr.Name + "='" + attr.Value + "'");