www.pudn.com > Fetion.rar > ConversationManagerImp.cs
namespace Imps.Client.Pc
{
using Imps.Client;
using Imps.Client.Core;
using Imps.Client.Pc.BizControls.NotifyWindows;
using Imps.Client.Pc.BizControls.NotifyWindows.Templates;
using Imps.Client.Pc.ScheduleSmsUI;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Client.Utils.Win32;
using Imps.Common;
using Imps.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Media;
using System.Threading;
using System.Windows.Forms;
public class ConversationManagerImp : IConversationManager
{
private List _allConversatonWindows;
private CustomEmotionAddFromEntityForm _ceAddFromEntityForm;
private CustomEmotionManagerForm _ceManagerForm;
private bool _forceToCloseWindow;
private IFrameworkWindow _frameworkWnd;
private ManageForm _scheduleSmsform;
private bool _showFileType;
private GroupSMSForm _smsForm;
public ConversationManagerImp(IFrameworkWindow framework)
{
try
{
this._frameworkWnd = framework;
this.CurrentUser.ConversationManager.FindConversationForm += new EventHandler(this.ConversationManager_FindConversationWindow);
((Form) framework).FormClosed += new FormClosedEventHandler(this.mainFormclosed);
framework.AccountManager.BeforeUserLogOut += new EventHandler(this.AccountManager_BeforeUserLogOut);
this.CurrentUser.StatusChanged += new EventHandler(this.CurrentUser_StatusChanged);
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
}
private void AccountManager_BeforeUserLogOut(object sender, ImpsCancelEventArgs e)
{
ImpsCancelOperationDelegate item = null;
bool flag = this.AllConversatonWindows.Count > 0;
bool flag2 = (this._smsForm != null) && !this._smsForm.IsDisposed;
if (flag || flag2)
{
string str;
e.Cancel = true;
if (flag)
{
str = "注销后所有的会话窗口都将被关闭,确认要注销吗?";
if (flag2)
{
str = "注销后群发短信窗口和所有的会话窗口都将被关闭,确认要注销吗?";
}
}
else
{
str = "注销后群发短信窗口将会被关闭,确认要注销吗?";
}
e.CancelQuestions.Add(str);
if (item == null)
{
item = delegate (bool realCancelled) {
if (!realCancelled)
{
this._forceToCloseWindow = true;
this.CloseAllConversationWindow();
if ((this._smsForm != null) && !this._smsForm.IsDisposed)
{
this._smsForm.Close();
}
}
};
}
e.CancelOperations.Add(item);
}
}
public void AddToOwnerEmotion(IWin32Window owner, string shortCut, string name, string filePath)
{
if ((this._ceAddFromEntityForm != null) && !this._ceAddFromEntityForm.IsDisposed)
{
this._ceAddFromEntityForm.Close();
}
this._ceAddFromEntityForm = new CustomEmotionAddFromEntityForm(this._frameworkWnd, shortCut, name, filePath);
ControlHelper.ShowFormCenterOnParent(this._ceAddFromEntityForm, owner);
}
public void CloseAllConversationWindow()
{
try
{
for (int i = this.AllConversatonWindows.Count - 1; i >= 0; i--)
{
this.AllConversatonWindows[i].Close();
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral(exception.ToString());
}
}
private void ConversationManager_FindConversationWindow(object sender, FindConversationFormEventArgs e)
{
try
{
if (e.Conversation.Dialogs.Count > 0)
{
IicUri uri = new IicUri(e.Conversation.Dialogs[0].To);
if (uri.Type == IicUriType.PersonalGroup)
{
return;
}
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
try
{
lock (this.AllConversatonWindows)
{
foreach (ConversationWindow window in this.AllConversatonWindows)
{
if ((window.CurrentConversation != null) && (window.CurrentConversation.CallId == e.Conversation.CallId))
{
this.DoFindForm(window, e.Conversation);
return;
}
}
foreach (ConversationWindow window2 in this.AllConversatonWindows)
{
if (window2.Uri == e.Conversation.CurrentDialog.To)
{
this.DoFindForm(window2, e.Conversation);
return;
}
}
foreach (ConversationWindow window3 in this.AllConversatonWindows)
{
if (!string.IsNullOrEmpty(window3.MobileNo) && (e.Conversation.CurrentDialog.Participants.Count == 1))
{
Imps.Client.Core.Contact contact = e.Conversation.CurrentDialog.Participants[0].Contact;
if ((contact is NonFederationContact) && ((contact as NonFederationContact).MobileNo == window3.MobileNo))
{
this.DoFindForm(window3, e.Conversation);
return;
}
}
}
}
ConversationWindow newForm = this.CreateConversationWindow(e.Conversation);
if (BossKey.Hidden)
{
newForm.ShowInTaskbar = false;
newForm.Show();
newForm.Visible = false;
}
else
{
newForm.WindowState = FormWindowState.Minimized;
newForm.Show();
}
if (((this.CurrentUser.Configuration.UserSetting.NotifyWinodwSetting.NewMessageNotify != null) && (this.CurrentUser.Presence.MainPresence != MainPresence.Busy)) && ((this.CurrentUser.Presence.MainPresence != MainPresence.DoNotDisturb) && !BossKey.Hidden))
{
ShowMessageReceiveNotifyWindow(newForm, e.Contact, e.Message);
}
}
catch (Exception exception2)
{
ClientLogger.WriteException(exception2);
}
finally
{
e.AutoEvent.Set();
}
}
private ConversationWindow CreateConversationWindow(Imps.Client.Core.Conversation conv)
{
ConversationWindow item = new ConversationWindow(this, conv);
item.LoadWindowSetting();
this.AllConversatonWindows.Add(item);
item.FormClosed += new FormClosedEventHandler(this.form_FormClosed);
item.Activated += new EventHandler(this.form_Activated);
item.Deactivate += new EventHandler(this.form_Deactivate);
return item;
}
private void CurrentUser_StatusChanged(object sender, UserSatusChangedEventArgs e)
{
try
{
if (e.NewStatus != UserAccountStatus.Logon)
{
if (this._ceManagerForm != null)
{
this._ceManagerForm.Close();
}
if (this._ceAddFromEntityForm != null)
{
this._ceAddFromEntityForm.Close();
}
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
}
private void DoFindForm(ConversationWindow form, Imps.Client.Core.Conversation conv)
{
if (form.CurrentConversation != conv)
{
form.CurrentConversation = conv;
}
}
public static void ExecuteFile(IWin32Window owner, string filePath)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ConversationManagerImp.ExecuteFileInThread), filePath);
}
private static void ExecuteFileInThread(object obj)
{
string url = obj as string;
try
{
if (url.ToLower().StartsWith("http://") || url.ToLower().StartsWith("www."))
{
ShellHelper.StartUrl(url);
}
else
{
Process.Start(url);
}
}
catch (Win32Exception exception)
{
if (File.Exists(url))
{
try
{
Imps.Client.Utils.Win32.NativeMethods.ShellExecute(IntPtr.Zero, null, "RUNDLL32.EXE", "shell32.dll,OpenAs_RunDLL " + url, null, 5);
}
catch (Exception exception2)
{
ClientLogger.WriteException(exception2);
}
}
else
{
ClientLogger.WriteException(exception);
}
}
catch (Exception exception3)
{
ClientLogger.WriteGeneral(exception3.ToString());
}
}
private void form_Activated(object sender, EventArgs e)
{
(sender as ConversationWindow).BeActivated = true;
}
private void form_Deactivate(object sender, EventArgs e)
{
(sender as ConversationWindow).BeActivated = false;
}
private void form_FormClosed(object sender, FormClosedEventArgs e)
{
this.AllConversatonWindows.Remove(sender as ConversationWindow);
if (this.AllConversatonWindows.Count == 0)
{
this.AllConversatonWindows = null;
}
}
public static bool IsShareContentUrl(Imps.Client.Core.User currentUser, string contactUri, string url)
{
Imps.Client.Core.Contact contact = currentUser.ContactList.Contacts[contactUri];
return (((contact != null) && ((contact.Type & ContactType.ImpsContact) != ContactType.None)) && (url.Contains("?") && url.Contains(contact.Uri.Sid.ToString())));
}
private void mainFormclosed(object sender, EventArgs e)
{
this.CloseAllConversationWindow();
}
internal void PlayMessageReceiveSource()
{
try
{
if ((this.CurrentUser.Configuration.UserSetting.SoundSetting.MsgEnabled != null) && File.Exists((string) this.CurrentUser.Configuration.UserSetting.SoundSetting.MsgNotify))
{
using (SoundPlayer player = new SoundPlayer((string) this.CurrentUser.Configuration.UserSetting.SoundSetting.MsgNotify))
{
player.Play();
}
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral("播放声音文件失败!", exception.ToString());
}
}
public void SendFile(IWin32Window owner, string contactUri)
{
this.StartConversation(owner, ConversationOperation.SendFile, new string[] { contactUri });
}
public void SendFile(IWin32Window owner, string contactUri, params string[] filePaths)
{
if ((filePaths == null) || (filePaths.Length == 0))
{
this.SendFile(owner, contactUri);
}
else
{
this.StartConversation(owner, ConversationOperation.SendFile, filePaths, false, new string[] { contactUri });
}
}
public void SendFileAfterSelectContacts(IWin32Window owner, List list)
{
try
{
using (InviteForm form = new InviteForm(this._frameworkWnd, StringTable.Conversation.MsgSelectSendFileContacts, this.CurrentUser.Configuration.SystemSetting.SysConversationSetting.MaxParticipations - 1, true, true, list, null, false, false, false, false))
{
form.SelectType = SelectContactsType.MultiConversation;
if ((ControlHelper.ShowDialogCenterOnParent(form, owner, false) == DialogResult.OK) && (form.SelectedContacts.Count != 0))
{
string[] contactUris = new string[form.SelectedContacts.Count];
for (int i = 0; i < form.SelectedContacts.Count; i++)
{
contactUris[i] = form.SelectedContacts[i].Uri.Raw;
}
this.StartConversation(owner, ConversationOperation.SendFile, contactUris);
}
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
}
public void SendFileToSelf(IWin32Window owner)
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "打开";
if (dialog.ShowDialog() == DialogResult.OK)
{
string fileName = dialog.FileName;
if (Array.IndexOf((this.CurrentUser.Configuration.SystemSetting.FileShareSetting.ForbiddenShareContentType + ",lnk").ToLower().Split(new char[] { ',', ';' }), Path.GetExtension(fileName).Replace(".", "").ToLower()) >= 0)
{
this._frameworkWnd.UnifiedMessageBox.ShowInfo(owner, StringTable.Conversation.MsgNotSupportFileFormat);
}
else
{
UploadShareContent sc = new UploadShareContent(fileName, this.CurrentUser, true);
if (sc.FileSize == 0L)
{
this._frameworkWnd.UnifiedMessageBox.ShowInfo(owner, StringTable.Conversation.MsgCanNotSendBlankFile);
sc.Close();
}
else if (sc.FileSize > this.CurrentUser.Configuration.SystemSetting.FileShareSetting.MaxSizeWhenOffline)
{
this._frameworkWnd.UnifiedMessageBox.ShowInfo(string.Format(StringTable.Conversation.MsgSendFileLimit, StringHelper.GetFileSize((long) this.CurrentUser.Configuration.SystemSetting.FileShareSetting.MaxSizeWhenOffline)));
sc.Close();
}
else
{
sc.SendToSelf = true;
SendFileToSelfForm form = new SendFileToSelfForm(sc, this._frameworkWnd);
form.Text = "发送文件";
ControlHelper.ShowFormCenterOnParent(form, this._frameworkWnd.MainWindow);
ThreadPool.QueueUserWorkItem(new WaitCallback(sc.SendFileInThread));
}
}
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
}
public void ShowCustomEmotionManageForm(IWin32Window owner)
{
if ((this._ceManagerForm == null) || this._ceManagerForm.IsDisposed)
{
if (this._ceManagerForm != null)
{
this._ceManagerForm.Close();
}
this._ceManagerForm = new CustomEmotionManagerForm(this._frameworkWnd);
ControlHelper.ShowFormCenterOnParent(this._ceManagerForm, owner);
}
else
{
Imps.Client.Utils.Win32.NativeMethods.ShowWindow(this._ceManagerForm.Handle, 9);
this._ceManagerForm.Activate();
this._ceManagerForm.Focus();
}
}
public static void ShowMessageReceiveNotifyWindow(Form newForm, Imps.Client.Core.Contact contact, string message)
{
NotifyWindowEventHandler handler = null;
try
{
if (contact != null)
{
ReciveMessageNotifyForm notifyWindow = new ReciveMessageNotifyForm(contact, message);
notifyWindow.Cursor = Cursors.Hand;
if (handler == null)
{
handler = delegate {
if (!newForm.IsDisposed)
{
newForm.Activate();
newForm.WindowState = FormWindowState.Normal;
}
};
}
notifyWindow.ContentClick += handler;
NotifyWindowManager.Add(notifyWindow);
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral("收到消息 弹出爬楼梯窗口失败!", exception.ToString());
}
}
public void ShowScheduleSmsForm(IWin32Window owner)
{
if ((this._scheduleSmsform == null) || this._scheduleSmsform.IsDisposed)
{
this._scheduleSmsform = new ManageForm(this._frameworkWnd);
ControlHelper.ShowFormCenterOnParent(this._scheduleSmsform, owner);
if (this._scheduleSmsform != null)
{
this._scheduleSmsform.Left -= 13;
this._scheduleSmsform.Top -= 0x12;
}
}
else
{
Imps.Client.Utils.Win32.NativeMethods.ShowWindow(this._scheduleSmsform.Handle, 9);
this._scheduleSmsform.Activate();
}
}
public void StartConversation(IWin32Window owner, ConversationOperation operation, params string[] contactUris)
{
this.StartConversation(owner, operation, false, contactUris);
}
public void StartConversation(IWin32Window owner, ConversationOperation operation, bool showSelectContactsForm, params string[] contactUris)
{
if (this.CurrentUser.Status == UserAccountStatus.Logon)
{
if ((contactUris != null) && !showSelectContactsForm)
{
if (((operation == ConversationOperation.SmsChat) && (contactUris.Length == 1)) && (contactUris[0] == this.CurrentUser.Uri.Raw))
{
List list2 = new List();
Imps.Client.Core.Contact item = this.CurrentUser.ContactList.FindOrCreateContact(this.CurrentUser.Uri.Raw, new AsyncBizOperation());
list2.Add(item);
this.StartMultiSMS(owner, list2);
}
else
{
this.StartConversation(owner, operation, null, false, contactUris);
}
}
else
{
List list = null;
if (contactUris != null)
{
list = new List();
foreach (string str in contactUris)
{
Imps.Client.Core.Contact contact = this.CurrentUser.ContactList.FindOrCreateContact(str, new AsyncBizOperation());
if (contact != null)
{
list.Add(contact);
}
}
}
switch (operation)
{
case ConversationOperation.ImChat:
this.StartConversationAfterSelectContacts(owner, list);
return;
case ConversationOperation.SmsChat:
this.StartMultiSMS(owner, list);
return;
case ConversationOperation.MmsChat:
return;
case ConversationOperation.SendFile:
this.SendFileAfterSelectContacts(owner, list);
return;
}
}
}
}
private void StartConversation(IWin32Window owner, ConversationOperation operation, object context, bool allowStranger, params string[] contactUris)
{
try
{
if ((this.CurrentUser.Status == UserAccountStatus.Logon) && (operation != ConversationOperation.MmsChat))
{
if ((contactUris.Length == 1) && (contactUris[0] == this.CurrentUser.Uri.Raw))
{
this._frameworkWnd.ShowOptions("PsGeneral");
}
else
{
List list = new List();
for (int i = 0; i < contactUris.Length; i++)
{
if (!(contactUris[i] == this.CurrentUser.Uri.Raw))
{
Imps.Client.Core.Contact item = this.CurrentUser.ContactList.FindOrCreateContact(contactUris[i], new AsyncBizOperation());
if (item != null)
{
list.Add(item);
}
}
}
if ((operation == ConversationOperation.SmsChat) && (list.Count > 1))
{
this.StartMultiSMS(owner, list);
}
else
{
if (list.Count > 1)
{
for (int j = list.Count - 1; j >= 0; j--)
{
if (!list[j].EnableMultiIM)
{
list.Remove(list[j]);
}
}
}
if (list.Count != 0)
{
Imps.Client.Core.Conversation conversation = null;
string to = null;
if (list.Count == 1)
{
to = list[0].Uri.Raw;
foreach (Imps.Client.Core.Conversation conversation2 in this.CurrentUser.ConversationManager.Conversations)
{
if (conversation2.CurrentDialog != null)
{
if (!conversation2.CurrentDialog.IsMulitiConversation)
{
if (conversation2.CurrentDialog.To == to)
{
conversation = conversation2;
}
}
else if ((conversation2.CurrentDialog.Participants.Count == 1) && (conversation2.CurrentDialog.Participants[0].Contact.Uri.Raw == to))
{
conversation = conversation2;
}
continue;
}
try
{
if (this.CurrentUser.ConversationManager.Conversations.Contains(conversation))
{
this.CurrentUser.ConversationManager.Conversations.Remove(conversation);
}
conversation = null;
continue;
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
continue;
}
}
}
ConversationWindow window = null;
if (list.Count == 1)
{
foreach (ConversationWindow window2 in this.AllConversatonWindows)
{
if (window2.Uri == list[0].Uri.Raw)
{
window = window2;
break;
}
if ((conversation != null) && (window2.CurrentConversation == conversation))
{
window = window2;
break;
}
}
if ((window == null) && !allowStranger)
{
Imps.Client.Core.Contact contact2 = list[0];
if (((contact2.RelationStatus != 1) || (contact2 is Imps.Client.Core.ChatFriend)) || (contact2 is Stranger))
{
string str2;
if ((contact2.RelationStatus == 0) || (contact2.RelationStatus == 2))
{
str2 = "由于对方尚未同意成为您的好友,所以您不能给对方发消息,\r\n\r\n是否再次发送添加好友申请?";
}
else
{
str2 = "由于对方不是您的好友,所以您不能给对方发送消息,\r\n\r\n是否添加对方为好友?";
}
if (this._frameworkWnd.UnifiedMessageBox.ShowConfirmation(this._frameworkWnd.MainWindow, str2) == DialogResult.Yes)
{
string mobileNo = string.IsNullOrEmpty(contact2.PersonalInfo.MobileNo) ? contact2.PersonalInfo.MobileNoUserSet : contact2.PersonalInfo.MobileNo;
int? targetGroupId = null;
if (contact2.BelongToGroups.Count > 0)
{
targetGroupId = new int?(contact2.BelongToGroups[0]);
}
this._frameworkWnd.ContactManager.ShowAddBuddyWindow(this._frameworkWnd.MainWindow, mobileNo, new long?(contact2.Uri.Sid), string.Empty, targetGroupId, ContactType.ImpsContact);
}
return;
}
}
}
if (conversation == null)
{
conversation = this.CurrentUser.ConversationManager.OpenConversation(to, list);
}
if (window == null)
{
window = this.CreateConversationWindow(conversation);
window.Show();
}
else
{
if (window.CurrentConversation == null)
{
window.CurrentConversation = conversation;
}
Imps.Client.Utils.Win32.NativeMethods.ShowWindow(window.Handle, 9);
window.Activate();
}
if (operation == ConversationOperation.SendFile)
{
if (context == null)
{
window.SendFile();
}
else if (context is string[])
{
foreach (string str4 in context as string[])
{
window.SendFile(str4);
}
}
}
if ((operation == ConversationOperation.SmsChat) && (contactUris.Length == 1))
{
window.SendSMS = true;
}
else if ((((operation == ConversationOperation.ImChat) && window.SendSMS) && ((contactUris.Length == 1) && (list.Count > 0))) && !(list[0] is Imps.Client.Core.MobileBuddy))
{
window.SendSMS = false;
}
}
}
}
}
}
catch (Exception exception2)
{
ClientLogger.WriteException(exception2);
}
}
public void StartConversationAfterSelectContacts(IWin32Window owner, List list)
{
try
{
using (InviteForm form = new InviteForm(this._frameworkWnd, StringTable.Conversation.MsgSelectConversationContacts, this.CurrentUser.Configuration.SystemSetting.SysConversationSetting.MaxParticipations - 1, true, true, list, null, false, false, false, false))
{
form.SelectType = SelectContactsType.MultiConversation;
if ((ControlHelper.ShowDialogCenterOnParent(form, owner, false) == DialogResult.OK) && (form.SelectedContacts.Count != 0))
{
string[] contactUris = new string[form.SelectedContacts.Count];
for (int i = 0; i < form.SelectedContacts.Count; i++)
{
contactUris[i] = form.SelectedContacts[i].Uri.Raw;
}
this.StartConversation(owner, ConversationOperation.ImChat, null, false, contactUris);
}
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
}
public void StartMultiSMS(IWin32Window owner)
{
this.StartMultiSMS(owner, null);
}
public void StartMultiSMS(IWin32Window owner, List list)
{
FormClosedEventHandler handler = null;
if (((this._smsForm == null) || this._smsForm.IsDisposed) || (list != null))
{
if (this._smsForm != null)
{
this._smsForm.Close();
}
this._smsForm = new GroupSMSForm(this.FrameworkWnd, list);
if (handler == null)
{
handler = delegate {
this._smsForm = null;
};
}
this._smsForm.FormClosed += handler;
ControlHelper.ShowFormCenterOnParent(this._smsForm, owner);
}
else
{
Imps.Client.Utils.Win32.NativeMethods.ShowWindow(this._smsForm.Handle, 9);
this._smsForm.Activate();
}
}
public void StartStrangerConversation(IWin32Window owner, string uri)
{
this.StartConversation(owner, ConversationOperation.ImChat, null, true, new string[] { uri });
}
public List AllConversatonWindows
{
get
{
if (this._allConversatonWindows == null)
{
this._allConversatonWindows = new List();
}
return this._allConversatonWindows;
}
set
{
this._allConversatonWindows = value;
}
}
internal Imps.Client.Core.User CurrentUser
{
get
{
return this._frameworkWnd.AccountManager.CurrentUser;
}
}
public bool ForceToCloseWindow
{
get
{
return this._forceToCloseWindow;
}
set
{
this._forceToCloseWindow = value;
}
}
internal IFrameworkWindow FrameworkWnd
{
get
{
return this._frameworkWnd;
}
}
internal bool ShowFileType
{
get
{
return this._showFileType;
}
set
{
this._showFileType = value;
}
}
}
}