📄 systemmessagemanager.cs
字号:
namespace Imps.Client.Pc
{
using Imps.Client;
using Imps.Client.Core;
using Imps.Client.Pc.BizControls.NotifyWindows;
using Imps.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
public class SystemMessageManager
{
private int _currentReceivedCount;
private string _hisPath;
private SystemInfoNotify _notifyWnd;
private static IFrameworkWindow _owner;
private List<SystemMessage> _unreadSysMsgs;
public const string SM_A_Expried = "Expired";
public const string SM_A_ID = "ID";
public const string SM_A_Link = "Link";
public const string SM_A_Read = "Read";
public const string SM_A_Ver = "Ver";
public const string SM_AV_VerNum = "1.0";
public const string SM_Message = "Message";
public const string SM_SystemMessage = "SystemMessage";
public SystemMessageManager(IFrameworkWindow wnd)
{
_owner = wnd;
_owner.AccountManager.CurrentUser.StatusChanged += new EventHandler<UserSatusChangedEventArgs>(this.CurrentUser_StatusChanged);
}
private void _notifyWnd_Closed(object sender, EventArgs e)
{
this._notifyWnd = null;
if (((_owner != null) && !((Form) _owner).IsDisposed) && (_owner.AccountManager.CurrentUser.Status != UserAccountStatus.Logoff))
{
this.PopMessage();
}
}
public void ActivateNotify()
{
if (this._notifyWnd != null)
{
this._notifyWnd.Activate();
}
}
public void AddSystemMessage(SystemMessage msg)
{
if (this._currentReceivedCount < this.DailyMsgMaxCount)
{
if (this.SaveSystemMessage && this.IsSystemMessageRepeated(msg._id))
{
return;
}
lock (this.SysMessageList)
{
this.SysMessageList.Add(msg);
}
this._currentReceivedCount++;
}
if (this.SaveSystemMessage)
{
this.SaveSystemMessageHistroy(msg);
}
}
private int ConvertExpiredTimeToNotifyTime(string utctime)
{
DateTime time = DateTime.Parse(utctime);
DateTime serverTime = User.ServerTime;
double totalMilliseconds = time.Subtract(serverTime).TotalMilliseconds;
if (totalMilliseconds <= 1000.0)
{
return 0x3e8;
}
if (totalMilliseconds >= 2147483647.0)
{
return 0x7fffffff;
}
return (int) ((uint) totalMilliseconds);
}
private void CurrentUser_StatusChanged(object sender, UserSatusChangedEventArgs e)
{
if (e.NewStatus == UserAccountStatus.Logon)
{
this.Reset();
}
else if (e.NewStatus != UserAccountStatus.Loginning)
{
if (this.Popuped)
{
this._notifyWnd.Close();
}
this._unreadSysMsgs = null;
}
}
private bool IsSystemMessageRepeated(string id)
{
XmlDocument doc = new XmlDocument();
if (XmlHelper.LoadXmlDocSafely(this._hisPath, doc, this._hisPath))
{
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
string str = XmlHelper.ReadXmlAttributeString(node, "ID");
if (id == str)
{
return true;
}
}
}
return false;
}
private bool IsTimeExpired(string utctime)
{
if (string.IsNullOrEmpty(utctime))
{
return true;
}
DateTime time = DateTime.Parse(utctime);
DateTime serverTime = User.ServerTime;
return (DateTime.Compare(time, serverTime) < 0);
}
private void LoadValidSystemMessageHistroy()
{
XmlDocument doc = new XmlDocument();
if (XmlHelper.LoadXmlDocSafely(this._hisPath, doc, this._hisPath))
{
foreach (XmlElement element in doc.DocumentElement.ChildNodes)
{
if (!string.IsNullOrEmpty(element.InnerText))
{
string id = XmlHelper.ReadXmlAttributeString(element, "ID");
string link = XmlHelper.ReadXmlAttributeString(element, "Link");
string utctime = XmlHelper.ReadXmlAttributeString(element, "Expired");
if ((XmlHelper.ReadXmlAttributeBoolean(element, "Read") == false) && !this.IsTimeExpired(utctime))
{
if (this._currentReceivedCount >= this.DailyMsgMaxCount)
{
break;
}
SystemMessage item = new SystemMessage(id, element.InnerText, link, utctime);
lock (this.SysMessageList)
{
this.SysMessageList.Add(item);
}
this._currentReceivedCount++;
}
}
}
}
}
public void PopMessage()
{
if ((this.UnreadSysMsgCount > 0) && ((_owner != null) && !((Form) _owner).IsDisposed))
{
if (this.Popuped)
{
this._notifyWnd.Activate();
}
else
{
SystemMessage msg = null;
lock (this.SysMessageList)
{
msg = this.SysMessageList[0];
}
this._notifyWnd = new SystemInfoNotify("系统消息", msg);
this._notifyWnd.HandleDestroyed += new EventHandler(this._notifyWnd_Closed);
this._notifyWnd.TimeToStay = this.ConvertExpiredTimeToNotifyTime(msg._expireTime);
NotifyWindowManager.Add(this._notifyWnd);
lock (this.SysMessageList)
{
this.SysMessageList.RemoveAt(0);
}
if (this.SaveSystemMessage)
{
SystemMessage systemMessage = this._notifyWnd.SystemMessage;
this.UpdateSystemMessageHistory(systemMessage);
}
}
}
}
public void Reset()
{
this._unreadSysMsgs = null;
this._currentReceivedCount = 0;
if (this.SaveSystemMessage)
{
string baseDirForHistory = _owner.PersistentManager.BaseDirForHistory;
if (!Directory.Exists(baseDirForHistory))
{
Directory.CreateDirectory(baseDirForHistory);
}
this._hisPath = baseDirForHistory + _owner.AccountManager.CurrentUser.Sid.ToString() + "_10000.xml";
this.LoadValidSystemMessageHistroy();
}
}
private void SaveSystemMessageHistroy(SystemMessage msg)
{
XmlElement documentElement;
XmlDocument doc = new XmlDocument();
if (!XmlHelper.LoadXmlDocSafely(this._hisPath, doc, this._hisPath))
{
documentElement = doc.CreateElement("SystemMessage");
XmlHelper.SetNodeAttribute(documentElement, "Ver", "1.0");
doc.AppendChild(documentElement);
}
documentElement = doc.DocumentElement;
XmlElement node = doc.CreateElement("Message");
node.InnerText = msg._content;
XmlHelper.SetNodeAttribute(node, "ID", msg._id);
XmlHelper.SetNodeAttribute(node, "Link", msg._link);
XmlHelper.SetNodeAttribute(node, "Expired", msg._expireTime);
XmlHelper.SetNodeAttributeBool(node, "Read", false);
documentElement.AppendChild(node);
XmlHelper.SaveXmlDocSafely(doc, this._hisPath, this._hisPath);
}
private void UpdateSystemMessageHistory(SystemMessage sysMsg)
{
XmlDocument doc = new XmlDocument();
if (XmlHelper.LoadXmlDocSafely(this._hisPath, doc, this._hisPath))
{
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (XmlHelper.ReadXmlAttributeString(node, "ID") == sysMsg._id)
{
XmlHelper.SetNodeAttributeBool(node, "Read", true);
break;
}
}
XmlHelper.SaveXmlDocSafely(doc, this._hisPath, this._hisPath);
}
}
public int DailyMsgMaxCount
{
get
{
return _owner.AccountManager.CurrentUser.Configuration.SystemSetting.SysClientSetting.NN_DailyMsgCount;
}
}
public bool Popuped
{
get
{
if (this._notifyWnd == null)
{
return false;
}
return (!this._notifyWnd.IsDisposed && this._notifyWnd.IsHandleCreated);
}
}
private bool SaveSystemMessage
{
get
{
return _owner.AccountManager.CurrentUser.Configuration.UserSetting.ConversationSetting.SaveMessages;
}
}
public List<SystemMessage> SysMessageList
{
get
{
if (this._unreadSysMsgs == null)
{
this._unreadSysMsgs = new List<SystemMessage>();
}
return this._unreadSysMsgs;
}
}
public int UnreadSysMsgCount
{
get
{
if (this._unreadSysMsgs == null)
{
return 0;
}
return this._unreadSysMsgs.Count;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -