public static void LoadBossUsers() { try { Rwl.AcquireWriterLock(5000); try { if (File.Exists("bossusers.xml")) { string bossusers; try { bossusers = File.ReadAllText("bossusers.xml", Encoding.UTF8); } catch { return; } AppVars.BossContacts = new SortedList <string, BossContact>(); var rawList = new List <BossContact>(); var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(bossusers); var bossusersNodeList = xmlDocument.GetElementsByTagName("contactentry"); foreach (XmlNode bossUser in bossusersNodeList) { if (bossUser.Attributes == null) { continue; } var name = bossUser.Attributes["name"].Value; var lastbossupdated = Convert.ToDateTime(bossUser.Attributes["lastbossupdated"].Value, CultureInfo.InvariantCulture); var contact = new BossContact(name, true, lastbossupdated); rawList.Add(contact); } rawList.Sort(SortByLastBossUpdated); var count = Math.Min(rawList.Count, 100); for (var i = 0; i < count; i++) { AppVars.BossContacts.Add(rawList[i].Name.ToLower(), rawList[i]); } } } finally { Rwl.ReleaseWriterLock(); } } catch (ApplicationException) { } }
public static void AddUsers(string args) { var added = false; try { Rwl.AcquireWriterLock(5000); try { var users = args.Split(new[] { "],[" }, StringSplitOptions.RemoveEmptyEntries); foreach (var user in users) { var spar = user.Split(','); if (spar.Length < 5) { continue; } if (spar[0].Equals("1")) { var nick = spar[1].Trim('\"'); if (AppVars.BossContacts.ContainsKey(nick.ToLower())) { AppVars.BossContacts[nick.ToLower()].LastBossUpdated = DateTime.Now.Subtract(AppVars.Profile.ServDiff); } else { var contact = new BossContact(nick, true, DateTime.Now.Subtract(AppVars.Profile.ServDiff)); AppVars.BossContacts.Add(contact.Name.ToLower(), contact); var message = $"Контакт [{nick}] добавлен в слежение"; try { if (AppVars.MainForm != null) { AppVars.MainForm.BeginInvoke( new UpdateChatDelegate(AppVars.MainForm.UpdateChat), message); } } catch (InvalidOperationException) { } added = true; } } } } finally { Rwl.ReleaseWriterLock(); } } catch (ApplicationException) { } if (added) { SaveBossUsers(); } }
private static int SortByLastBossUpdated(BossContact x, BossContact y) { return(y.LastBossUpdated.CompareTo(x.LastBossUpdated)); }