BUGFIX: tf online <path> shouldn't croak if <path> is an add awaiting
[tfs.git] / tools / opentf / Settings.cs
blob5de9a1624fb47f2701c1335c90c58405088c4127
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 public class Settings : SortedList<string, string>
40 private readonly string ConfigFile = "TfClient.config";
41 private bool initialized = false;
42 private static Settings current = new Settings();
44 public static Settings Current
46 get { return current; }
49 private Settings()
53 private void InitializeAsNeeded()
55 if (initialized) return;
57 Add("Checkin.Validate", "false");
58 Add("Checkout.Latest", "false");
59 Add("Credentials.Save", "false");
60 Add("File.Excludes", "");
61 Add("File.ReadWrite", "false");
62 Add("Get.DefaultToCwd", "false");
63 Add("Get.Recursive", "false");
64 Add("History.DefaultToCwd", "false");
65 Add("History.Detailed", "false");
66 Add("History.Recursive", "false");
67 Add("History.StopAfter", "256");
68 Add("Online.Recursive", "false");
69 Add("Server.Default", "");
70 Add("Workspace.Default", "");
72 initialized = true;
74 string configFilePath = GetConfigPath();
75 if (!File.Exists(configFilePath)) return;
77 using (XmlTextReader reader = new XmlTextReader(configFilePath))
79 while (reader.Read())
81 if (reader.NodeType == XmlNodeType.Element && reader.Name == "add")
83 string key = reader.GetAttribute("key");
84 string value = reader.GetAttribute("value");
85 this[key] = value;
91 public string GetConfigPath()
93 return Path.Combine(TeamFoundationServer.ClientSettingsDirectory, ConfigFile);
96 public void Save()
98 InitializeAsNeeded();
100 XmlDocument doc = new XmlDocument();
102 XmlElement root = doc.CreateElement("configuration");
103 doc.AppendChild(root);
105 XmlElement appSettings = doc.CreateElement("appSettings");
106 root.AppendChild(appSettings);
108 foreach (string key in Keys)
110 XmlElement add = doc.CreateElement("add");
111 add.SetAttributeNode("key", "").Value = key;
112 add.SetAttributeNode("value", "").Value = this[key];
113 appSettings.AppendChild(add);
116 if (!Directory.Exists(TeamFoundationServer.ClientSettingsDirectory))
117 Directory.CreateDirectory(TeamFoundationServer.ClientSettingsDirectory);
119 string configFilePath = Path.Combine(TeamFoundationServer.ClientSettingsDirectory, ConfigFile);
120 using (XmlTextWriter writer = new XmlTextWriter(configFilePath, null))
122 writer.Formatting = Formatting.Indented;
123 doc.Save(writer);
127 public string Get(string key)
129 InitializeAsNeeded();
131 string value = String.Empty;
132 TryGetValue(key, out value);
134 return value;
137 public bool GetAsBool(string key)
139 InitializeAsNeeded();
141 string value = String.Empty;
142 if (TryGetValue(key, out value))
144 return Convert.ToBoolean(value);
147 return false;
150 public int GetAsInt(string key)
152 InitializeAsNeeded();
154 string value = String.Empty;
155 if (TryGetValue(key, out value))
157 return Convert.ToInt32(value);
160 return -1;