| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Text; |
|---|
| 4 | using System.Globalization; |
|---|
| 5 | using System.Xml.Serialization; |
|---|
| 6 | using System.IO; |
|---|
| 7 | using System.Xml; |
|---|
| 8 | using Misuzilla.Net.Irc; |
|---|
| 9 | using System.Diagnostics; |
|---|
| 10 | |
|---|
| 11 | namespace Misuzilla.Applications.TwitterIrcGateway |
|---|
| 12 | { |
|---|
| 13 | public class Groups : SortedList<string, Group> |
|---|
| 14 | { |
|---|
| 15 | public Groups() |
|---|
| 16 | : base(StringComparer.InvariantCultureIgnoreCase) |
|---|
| 17 | { |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | private static Object _syncObject = new object(); |
|---|
| 21 | private static XmlSerializer _serializer = null; |
|---|
| 22 | static Groups() |
|---|
| 23 | { |
|---|
| 24 | lock (_syncObject) |
|---|
| 25 | { |
|---|
| 26 | if (_serializer == null) |
|---|
| 27 | { |
|---|
| 28 | _serializer = new XmlSerializer(typeof(Group[])); |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | private static XmlSerializer Serializer |
|---|
| 33 | { |
|---|
| 34 | get |
|---|
| 35 | { |
|---|
| 36 | return _serializer; |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | public void Serialize(Stream stream) |
|---|
| 41 | { |
|---|
| 42 | Group[] groups = new Group[this.Values.Count]; |
|---|
| 43 | this.Values.CopyTo(groups, 0); |
|---|
| 44 | using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, Encoding.UTF8)) |
|---|
| 45 | { |
|---|
| 46 | _serializer.Serialize(xmlTextWriter, groups); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public static Groups Deserialize(Stream stream) |
|---|
| 51 | { |
|---|
| 52 | Group[] groups = _serializer.Deserialize(stream) as Group[]; |
|---|
| 53 | Groups retGroups = new Groups(); |
|---|
| 54 | foreach (Group group in groups) |
|---|
| 55 | { |
|---|
| 56 | retGroups[group.Name] = group; |
|---|
| 57 | group.IsJoined = false; |
|---|
| 58 | group.ChannelModes = group.ChannelModes == null ? new List<ChannelMode>() : group.ChannelModes; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | return retGroups; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /// <summary> |
|---|
| 65 | /// |
|---|
| 66 | /// </summary> |
|---|
| 67 | /// <param name="path"></param> |
|---|
| 68 | /// <returns></returns> |
|---|
| 69 | public static Groups Load(String path) |
|---|
| 70 | { |
|---|
| 71 | // group �ǂݎ� |
|---|
| 72 | if (File.Exists(path)) |
|---|
| 73 | { |
|---|
| 74 | Trace.WriteLine(String.Format("Load Group: {0}", path)); |
|---|
| 75 | try |
|---|
| 76 | { |
|---|
| 77 | using (FileStream fs = new FileStream(path, FileMode.Open)) |
|---|
| 78 | { |
|---|
| 79 | try |
|---|
| 80 | { |
|---|
| 81 | Groups groups = Groups.Deserialize(fs); |
|---|
| 82 | if (groups != null) |
|---|
| 83 | return groups; |
|---|
| 84 | } |
|---|
| 85 | catch (XmlException xe) { Trace.WriteLine(xe.Message); } |
|---|
| 86 | catch (InvalidOperationException ioe) { Trace.WriteLine(ioe.Message); } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | catch (IOException ie) |
|---|
| 90 | { |
|---|
| 91 | Trace.WriteLine(ie.Message); |
|---|
| 92 | throw; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | return new Groups(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /// <summary> |
|---|
| 99 | /// |
|---|
| 100 | /// </summary> |
|---|
| 101 | /// <param name="path"></param> |
|---|
| 102 | public void Save(String path) |
|---|
| 103 | { |
|---|
| 104 | Trace.WriteLine(String.Format("Save Group: {0}", path)); |
|---|
| 105 | try |
|---|
| 106 | { |
|---|
| 107 | String dir = Path.GetDirectoryName(path); |
|---|
| 108 | Directory.CreateDirectory(dir); |
|---|
| 109 | using (FileStream fs = new FileStream(path, FileMode.Create)) |
|---|
| 110 | { |
|---|
| 111 | try |
|---|
| 112 | { |
|---|
| 113 | this.Serialize(fs); |
|---|
| 114 | } |
|---|
| 115 | catch (XmlException xe) { Trace.WriteLine(xe.Message); } |
|---|
| 116 | catch (InvalidOperationException ioe) { Trace.WriteLine(ioe.Message); } |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | catch (IOException ie) |
|---|
| 120 | { |
|---|
| 121 | Trace.WriteLine(ie.Message); |
|---|
| 122 | throw; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | public class Group : IComparable |
|---|
| 128 | { |
|---|
| 129 | public String Name { get; set; } |
|---|
| 130 | public String Mode { get; set; } |
|---|
| 131 | public List<String> Members { get; set; } |
|---|
| 132 | public Boolean IsJoined { get; set; } |
|---|
| 133 | public String Topic { get; set; } |
|---|
| 134 | public List<ChannelMode> ChannelModes { get; set; } |
|---|
| 135 | |
|---|
| 136 | public Group() |
|---|
| 137 | { |
|---|
| 138 | ChannelModes = new List<ChannelMode>(); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | public Group(String name) |
|---|
| 142 | { |
|---|
| 143 | if (!name.StartsWith("#") || name.Length < 2) |
|---|
| 144 | { |
|---|
| 145 | throw new ArgumentException("�`�����l�������Ŏn�܂��v���������B"); |
|---|
| 146 | } |
|---|
| 147 | Name = name; |
|---|
| 148 | Members = new List<string>(); |
|---|
| 149 | ChannelModes = new List<ChannelMode>(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | public Boolean Exists(String id) |
|---|
| 153 | { |
|---|
| 154 | Int32 pos; |
|---|
| 155 | lock (Members) |
|---|
| 156 | { |
|---|
| 157 | pos = Members.BinarySearch(id, StringComparer.InvariantCultureIgnoreCase); |
|---|
| 158 | } |
|---|
| 159 | return pos > -1; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | public void Add(String id) |
|---|
| 163 | { |
|---|
| 164 | lock (Members) |
|---|
| 165 | { |
|---|
| 166 | Members.Add(id); |
|---|
| 167 | Members.Sort(StringComparer.InvariantCultureIgnoreCase); |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | public void Remove(String id) |
|---|
| 172 | { |
|---|
| 173 | lock (Members) |
|---|
| 174 | { |
|---|
| 175 | Members.Remove(id); |
|---|
| 176 | Members.Sort(StringComparer.InvariantCultureIgnoreCase); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | public Boolean IgnoreEchoBack |
|---|
| 181 | { |
|---|
| 182 | get |
|---|
| 183 | { |
|---|
| 184 | return ChannelModes.Exists(mode => mode.Mode == ChannelModeTypes.Private); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | #region IComparable �����o |
|---|
| 189 | |
|---|
| 190 | public int CompareTo(object obj) |
|---|
| 191 | { |
|---|
| 192 | if (!(obj is Group)) |
|---|
| 193 | return -1; |
|---|
| 194 | |
|---|
| 195 | return String.Compare((obj as Group).Name, this.Name, true, CultureInfo.InvariantCulture); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | #endregion |
|---|
| 199 | } |
|---|
| 200 | } |
|---|