📄 systemmessagemanager.cs
字号:
namespace Imps.Client.Pc
{
using Imps.Client;
using Imps.Client.Core;
using Imps.Client.Core.History;
using Imps.Client.Pc.BizControls.NotifyWindows;
using Imps.Client.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Media;
using System.Windows.Forms;
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, (IntPtr) 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++;
this.PlaySystemMessageReceiveSound();
}
if (this.SaveSystemMessage)
{
this.SaveSystemMessageHistroy(msg);
}
}
private int ConvertExpiredTimeToNotifyTime(DateTime utctime)
{
DateTime time = utctime;
DateTime serverTime = User.ServerTime;
double totalMilliseconds = time.Subtract(serverTime).TotalMilliseconds;
if (totalMilliseconds <= 1000)
{
return 0x3e8;
}
if (totalMilliseconds >= 2147483647)
{
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)
{
IHistoryManager historyManager = _owner.AccountManager.CurrentUser.HistoryManager;
if (historyManager != null)
{
return historyManager.ExistMessage(new SystemMessage(id, string.Empty, string.Empty, DateTime.Now.ToString()));
}
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()
{
IHistoryManager historyManager = _owner.AccountManager.CurrentUser.HistoryManager;
if (historyManager != null)
{
IList<SystemMessage> list = historyManager.LoadMessages<SystemMessage>(MessageType.System, MessageStatus.Failed);
if (list != null)
{
using (IEnumerator<SystemMessage> enumerator = list.GetEnumerator())
{
while (enumerator.MoveNext())
{
SystemMessage message = enumerator.get_Current();
if (this._currentReceivedCount < this.DailyMsgMaxCount)
{
lock (this.SysMessageList)
{
this.SysMessageList.Add(message);
continue;
}
}
return;
}
}
}
}
}
private void PlaySystemMessageReceiveSound()
{
try
{
if ((_owner.AccountManager.CurrentUser.Configuration.UserSetting.SoundSetting.SysEnabled != null) && File.Exists((string) _owner.AccountManager.CurrentUser.Configuration.UserSetting.SoundSetting.SysNotify))
{
using (SoundPlayer player = new SoundPlayer((string) _owner.AccountManager.CurrentUser.Configuration.UserSetting.SoundSetting.SysNotify))
{
player.Play();
}
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral("播放声音文件失败!", exception.ToString());
}
}
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.get_Item(0);
}
this._notifyWnd = new SystemInfoNotify("系统消息", msg);
this._notifyWnd.HandleDestroyed += new EventHandler(this._notifyWnd_Closed);
this._notifyWnd.TimeToStay = this.ConvertExpiredTimeToNotifyTime(msg.Time);
NotifyWindowManager.Add(this._notifyWnd);
lock (this.SysMessageList)
{
this.SysMessageList.RemoveAt(0);
}
if (this.SaveSystemMessage)
{
SystemMessage sysMsg = this._notifyWnd.SystemMessage;
this.UpdateSystemMessageHistory(sysMsg);
}
}
}
}
public void Reset()
{
this._unreadSysMsgs = null;
this._currentReceivedCount = 0;
if (this.SaveSystemMessage)
{
this.LoadValidSystemMessageHistroy();
}
}
private void SaveSystemMessageHistroy(SystemMessage msg)
{
msg.CurrentUser = _owner.AccountManager.CurrentUser;
IHistoryManager historyManager = _owner.AccountManager.CurrentUser.HistoryManager;
if (historyManager != null)
{
historyManager.SaveMessage(msg);
}
}
private void UpdateSystemMessageHistory(SystemMessage sysMsg)
{
IHistoryManager historyManager = _owner.AccountManager.CurrentUser.HistoryManager;
if (historyManager != null)
{
sysMsg.CurrentUser = _owner.AccountManager.CurrentUser;
sysMsg.Readok = true;
historyManager.SaveMessage(sysMsg);
}
}
public int DailyMsgMaxCount
{
get
{
return _owner.AccountManager.CurrentUser.Configuration.SystemSetting.SysClientSetting.NN_DailyMsgCount;
}
}
public bool Popuped
{
get
{
if ((this._notifyWnd != null) && !this._notifyWnd.IsDisposed)
{
return this._notifyWnd.IsHandleCreated;
}
return false;
}
}
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.get_Count();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -