show the difference between two versions for a specified path
[tfs.git] / class / OpenTF.Common / Settings.cs
blobf4aae6e461eda4bb2f95f8d7c9c3a13c636b0d6d
1 //
2 // Settings.cs
3 //
4 // Authors:
5 // Joel Reed (joelwreed@gmail.com)
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Text;
33 using System.Xml;
34 using Microsoft.TeamFoundation.Client;
35 using Microsoft.TeamFoundation.VersionControl.Common;
36 using Microsoft.TeamFoundation.VersionControl.Client;
38 namespace OpenTF.Common
40 public class Settings : SortedList<string, string>
42 private readonly string ConfigFile = "TfClient.config";
43 private bool initialized = false;
44 private static Settings current = new Settings();
46 public static Settings Current
48 get { return current; }
51 private Settings()
55 private void InitializeAsNeeded()
57 if (initialized) return;
59 Add("Checkin.Validate", "false");
60 Add("Checkout.Latest", "false");
61 Add("Credentials.Save", "false");
62 Add("File.Excludes", "");
63 Add("File.ReadWrite", "false");
64 Add("Get.DefaultToCwd", "false");
65 Add("Get.ChangesetMtimes", "false");
66 Add("Get.Recursive", "false");
67 Add("History.DefaultToCwd", "false");
68 Add("History.Detailed", "false");
69 Add("History.Recursive", "false");
70 Add("History.StopAfter", "256");
71 Add("Merges.Recursive", "false");
72 Add("Online.Recursive", "false");
73 Add("Server.Default", "");
74 Add("Workfold.CreateLocal", "false");
75 Add("Workspace.Default", "");
77 initialized = true;
79 string configFilePath = GetConfigPath();
80 if (!File.Exists(configFilePath)) return;
82 using (XmlTextReader reader = new XmlTextReader(configFilePath))
84 while (reader.Read())
86 if (reader.NodeType == XmlNodeType.Element && reader.Name == "add")
88 string key = reader.GetAttribute("key");
89 string value = reader.GetAttribute("value");
90 this[key] = value;
96 public string GetConfigPath()
98 return Path.Combine(TeamFoundationServer.ClientSettingsDirectory, ConfigFile);
101 public void Save()
103 InitializeAsNeeded();
105 XmlDocument doc = new XmlDocument();
107 XmlElement root = doc.CreateElement("configuration");
108 doc.AppendChild(root);
110 XmlElement appSettings = doc.CreateElement("appSettings");
111 root.AppendChild(appSettings);
113 foreach (string key in Keys)
115 XmlElement add = doc.CreateElement("add");
116 add.SetAttributeNode("key", "").Value = key;
117 add.SetAttributeNode("value", "").Value = this[key];
118 appSettings.AppendChild(add);
121 if (!Directory.Exists(TeamFoundationServer.ClientSettingsDirectory))
122 Directory.CreateDirectory(TeamFoundationServer.ClientSettingsDirectory);
124 string configFilePath = Path.Combine(TeamFoundationServer.ClientSettingsDirectory, ConfigFile);
125 using (XmlTextWriter writer = new XmlTextWriter(configFilePath, null))
127 writer.Formatting = Formatting.Indented;
128 doc.Save(writer);
132 public string Get(string key)
134 InitializeAsNeeded();
136 string value = String.Empty;
137 TryGetValue(key, out value);
139 return value;
142 public bool GetAsBool(string key)
144 InitializeAsNeeded();
146 string value = String.Empty;
147 if (TryGetValue(key, out value))
149 return Convert.ToBoolean(value);
152 return false;
155 public int GetAsInt(string key)
157 InitializeAsNeeded();
159 string value = String.Empty;
160 if (TryGetValue(key, out value))
162 return Convert.ToInt32(value);
165 return -1;