/// <summary> /// 删除 /// </summary> private void Delete() { //没有太好的关联方式,目前暂时做成当前窗口(因为只有当前窗口可能删除对应信息); ChatViewModel chatVM = AppData.MainMV.ChatListVM.SelectedItem; if (chatVM != null) { AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } chatVM.HideMessageCommand.Execute(_targetModel); string filePath = System.IO.Path.Combine(SDKClient.SDKClient.Instance.property.CurrentAccount.filePath, _targetModel.ResourceModel.FileName); //if (_targetModel.ResourceModel.FullName == filePath && File.Exists(filePath)) //{ // try // { // File.Delete(filePath); // } // catch // { // } //} } }
/// <summary> /// 撤回 /// </summary> /// <param name="msg"></param> private void WithDraw(MessageModel msg) { AcioningItems.Remove(this); ChatViewModel chatVM = AppData.MainMV.ChatListVM.SelectedItem; if (chatVM != null) { chatVM.SendWithDrawMsg(msg); } }
/// <summary> /// 转离线发送 /// </summary> private void OfflineSend() { if (ChatViewModel != null) { _targetModel.CanOperate = false; this.tbConnecting.Visibility = Visibility.Visible; if (!string.IsNullOrEmpty(_targetModel.MsgKey)) { SDKClient.SDKProperty.chatType messageType = ChatViewModel.IsGroup ? SDKClient.SDKProperty.chatType.groupChat : SDKClient.SDKProperty.chatType.chat; SDKClient.SDKClient.Instance.SendRetractMessage(_targetModel.MsgKey, ChatViewModel.ID.ToString(), messageType, ChatViewModel.IsGroup ? ChatViewModel.ID : 0, SDKClient.SDKProperty.RetractType.OnlineToOffline); string size = Helper.FileHelper.FileSizeToString(_targetModel.ResourceModel.Length); string content = $"您取消了\"{_targetModel.ResourceModel.FileName}\"({size})的发送,文件传输失败。"; MessageModel cancel = new MessageModel() { MsgKey = _targetModel.MsgKey, Content = content, MsgType = MessageType.notification, MessageState = MessageStates.Fail, }; ChatViewModel.Chat.Messages.Insert(ChatViewModel.Chat.Messages.Count - 1, cancel); AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } } if (FileExists()) { AcioningItems.Add(this); _operateTask = new System.Threading.CancellationTokenSource(); ChatViewModel.SendOfflineFile(_targetModel, _operateTask, (result) => { AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } }); } else { ChatViewModel.Chat.Messages.Remove(_targetModel); } } }
public void Cancel(bool isInner = false) { AcioningItems.Remove(this); if (_targetModel.ResourceModel.Progress > 0.98) { AppData.MainMV.TipMessage = "视频消息已发出无法取消!"; return; } if (ChatViewModel != null) { if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; if (isInner) { return; } } string tip = _targetModel.Sender.ID == AppData.Current.LoginUser.ID ? "发送" : "接收"; string size = Helper.FileHelper.FileSizeToString(_targetModel.ResourceModel.Length); string msg = $"您取消了\"{_targetModel.ResourceModel.FileName}\"({size})的{tip},文件传输失败。"; _targetModel.MsgType = MessageType.notification; _targetModel.MessageState = MessageStates.Fail; _targetModel.Content = msg; ChatViewModel.UpdateMsg(_targetModel); int roomID = ChatViewModel.Model.ID; var chatType = ChatViewModel.IsGroup ? SDKClient.SDKProperty.chatType.groupChat : SDKClient.SDKProperty.chatType.chat; Task.Run(async() => await SDKClient.SDKClient.Instance.AppendLocalData_NotifyMessage(AppData.Current.LoginUser.ID.ToString(), roomID.ToString(), msg, roomID, SDKClient.SDKProperty.MessageType.notification, chatType: chatType)); if (this.FileState == FileStates.SendOnline || _targetModel.ResourceModel.RefInfo is SDKClient.Model.OnlineFileBody body) //在线消息会发送取消给对方 { SDKClient.SDKProperty.chatType messageType = ChatViewModel.IsGroup ? SDKClient.SDKProperty.chatType.groupChat : SDKClient.SDKProperty.chatType.chat; SDKClient.SDKClient.Instance.SendRetractMessage(_targetModel.MsgKey, ChatViewModel.ID.ToString(), messageType, ChatViewModel.IsGroup ? ChatViewModel.ID : 0, SDKClient.SDKProperty.RetractType.SourceEndOnlineRetract); } else //离线消息不发送 { _targetModel.Sender = null; SDKClient.SDKClient.Instance.CancelOfflineFileRecv(_targetModel.MsgKey); } _targetModel.ResourceModel.FileState = FileStates.Fail; } }
/// <summary> /// 在线发送 /// </summary> private void OnlineSend() { if (ChatViewModel != null) { _targetModel.CanOperate = false; AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } if (FileExists()) { AcioningItems.Add(this); _operateTask = new System.Threading.CancellationTokenSource(); if (ChatViewModel.IsGroup) { this.tbConnecting.Visibility = Visibility.Visible; //群聊只能离线发送 ChatViewModel.SendOfflineFile(_targetModel, _operateTask, (result) => { AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } }); } else { ChatViewModel.SendOnlineFile(ChatViewModel, _targetModel, _operateTask, (result) => { AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } }); } } else { ChatViewModel.Chat.Messages.Remove(_targetModel); } } }
/// <summary> /// 开始接收 /// </summary> private void Receive() { if (ChatViewModel != null && AppData.CanInternetAction()) { _targetModel.MessageState = MessageStates.None; this.FileState = FileStates.Receiving; _operateTask = new System.Threading.CancellationTokenSource(); AcioningItems.Add(this); CSClient.Helper.MessageHelper.LoadFileContent(_targetModel, _operateTask, ChatViewModel, (result) => { AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } }); } }
private void SendVideo() { if (ChatViewModel != null) { _targetModel.CanOperate = false; if (FileExists()) { AcioningItems.Add(this); _operateTask = new System.Threading.CancellationTokenSource(); _targetModel.ResourceModel.FileState = FileStates.SendOffline; if (AppData.CanInternetAction("")) { ChatViewModel.SendSmallVideoFile(_targetModel, _operateTask, async(result) => { if (SDKClient.SDKClient.Instance.property.CurrentAccount.Isforbidden) { _targetModel.MessageState = MessageStates.Warn; _targetModel.ResourceModel.FileState = FileStates.Fail; } AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } }); } else { ChatViewModel.SaveSendingMessages(_targetModel); } } else { ChatViewModel.Chat.Messages.Remove(_targetModel); } } }
/// <summary> /// 另存为 /// </summary> private void SaveAs() { if (_targetModel.ResourceModel.FileState == FileStates.WaitForReceieve) { if (AppData.CanInternetAction()) { Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog(); sfd.FileName = _targetModel.ResourceModel.FileName; //sfd.Filter = System.IO.Path.GetExtension(_targetModel.ResourceModel.FileName); if (sfd.ShowDialog() == true) { try { File.Delete(sfd.FileName); } catch { AppData.MainMV.TipMessage = "文件已被占用,替换失败!"; return; } this.FileState = FileStates.Receiving; _operateTask = new System.Threading.CancellationTokenSource(); AcioningItems.Add(this); ChatViewModel chatVM = AppData.MainMV.ChatListVM.SelectedItem; CSClient.Helper.MessageHelper.LoadFileContent(_targetModel, _operateTask, chatVM, (result) => { AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } }, sfd.FileName); } } } else { if (FileExists()) { Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = System.IO.Path.GetFileName(this.FullName); // Default file name dlg.DefaultExt = System.IO.Path.GetExtension(this.FullName); // fileName.Split('.').LastOrDefault(); // Default file extension dlg.Filter = string.Format("文件 (.{0})|*.{0}", dlg.DefaultExt); // // Filter files by extension dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); if (dlg.ShowDialog() == true) { try { File.Copy(this.FullName, dlg.FileName, true); } catch { AppData.MainMV.TipMessage = "文件已被占用,替换失败!"; } } } } }
private void SetFileStatesView() { this.pbProcess.Visibility = Visibility.Collapsed; this.BdIcon.Visibility = Visibility.Collapsed; foreach (UIElement ui in this.ugState.Children) { ui.Visibility = Visibility.Collapsed; } switch (this.FileState) { case FileStates.Fail: if (ChatViewModel != null) { ChatViewModel.UpdateMsg(_targetModel); AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } } return; default: case FileStates.None: break; case FileStates.SendOnline: this.btnOfflineSend.Visibility = this.btnCancel.Visibility = Visibility.Visible; this.pbProcess.Visibility = Visibility.Visible; break; case FileStates.SendOffline: this.btnOnlineSend.Visibility = this.btnCancel.Visibility = Visibility.Visible; this.pbProcess.Visibility = Visibility.Visible; if (AppData.MainMV.ChatListVM.SelectedItem.IsGroup) //群聊不能 在线发送 { this.btnOnlineSend.Visibility = Visibility.Collapsed; this.tbConnecting.Visibility = Visibility.Visible; } break; case FileStates.WaitForReceieve: this.btnReceive.Visibility = this.btnSaveAs.Visibility = this.btnCancel.Visibility = Visibility.Visible; break; case FileStates.Receiving: this.btnCancel.Visibility = Visibility.Visible; this.pbProcess.Visibility = Visibility.Visible; break; case FileStates.Completed: this.btnOpen.Visibility = this.btnSaveAs.Visibility = this.btnDelete.Visibility = Visibility.Visible; _completeTime = DateTime.Now; this.BdIcon.Visibility = Visibility.Visible; _operateTask = null; AcioningItems.Remove(this); if (File.Exists(this.FullName)) { _targetModel.ResourceModel.FileImg = Helper.FileHelper.GetFileImage(this.FullName, false); // Helper.WindowsThumbnailProvider.GetFileThumbnail(this.FullName); } this.pbProcess.Visibility = Visibility.Collapsed; break; } if (this.IsInDocument) { this.btnDelete.Visibility = Visibility.Collapsed; this.btnCancel.Visibility = Visibility.Collapsed; } this.menu.Items.Clear(); foreach (UIElement ui in this.ugState.Children) { if (ui.Visibility == Visibility.Visible && ui is ButtonBase btn) { this.menu.Items.Add(new MenuItem() { Uid = btn.Uid, Header = btn.Content }); } } }
private void CalFileState() { if (this.FileState == FileStates.Receiving) { return; } try { this.tbName.ToolTip = System.IO.Path.GetFileName(this.FullName);//?.Split('\\').LastOrDefault(); } catch { return; } if (ChatViewModel == null) { ChatViewModel = AppData.MainMV.ChatListVM.SelectedItem; } else if (_targetModel.MessageState == MessageStates.Fail) { if (_operateTask != null) { _operateTask.Cancel(); } AcioningItems.Remove(this); } if (_targetModel == null && this.DataContext is MessageModel item) { _targetModel = item; var target = FileItems.FirstOrDefault(info => info.MsgKey == item.MsgKey); if (target == null) { target = item; FileItems.Add(item); } if (string.IsNullOrEmpty(target.MsgKey)) { target.MsgKey = Guid.NewGuid().ToString(); } this.DataContext = _targetModel = target; } if (this.IsMainView == false) { if (_targetModel.ResourceModel.FileState != FileStates.None) { return; } } //是否发送者 bool isSender = _targetModel.Sender.ID == AppData.Current.LoginUser.ID; _targetModel.ResourceModel.FileImg = Helper.FileHelper.GetFileImage(this.FullName, false); this.gridLayout.ToolTip = System.IO.Path.GetFileName(this.FullName); if (File.Exists(this.FullName)) { if (isSender && _targetModel.IsSending) { if (this._targetModel.ResourceModel.FileState == FileStates.SendOffline || this._targetModel.ResourceModel.FileState == FileStates.SendOnline) { } else { this.FileState = _targetModel.ResourceModel.FileState = FileStates.SendOnline; this.OnlineSend(); } } else { this.FileState = _targetModel.ResourceModel.FileState = FileStates.Completed; _completeTime = DateTime.Now.AddMinutes(-5); } } else { if (isSender) //发送者 { this.FileState = _targetModel.ResourceModel.FileState = FileStates.Completed; _completeTime = DateTime.Now.AddMinutes(-5); } else { this.FileState = _targetModel.ResourceModel.FileState = FileStates.WaitForReceieve; } } }
private void SetFileStatesView() { switch (this.FileState) { case FileStates.Fail: if (ChatViewModel != null) { AcioningItems.Remove(this); if (_operateTask != null) { _operateTask.Cancel(); _operateTask = null; } } App.Current.Dispatcher.Invoke(() => { this.gridProgress.Visibility = Visibility.Collapsed; this.borderPlay.Visibility = Visibility.Collapsed; this.imgReset.Visibility = Visibility.Visible; this.imgDownload.Visibility = Visibility.Collapsed; }); if (this.DataContext is MessageModel _dataContextMsg) { bool isSender = _dataContextMsg.Sender.ID == AppData.Current.LoginUser.ID; if (isSender) { App.Current.Dispatcher.Invoke(() => { this.gridProgress.Visibility = Visibility.Collapsed; this.borderPlay.Visibility = Visibility.Visible; this.imgReset.Visibility = Visibility.Collapsed; this.imgDownload.Visibility = Visibility.Collapsed; }); } } //this.pathResend.Visibility = Visibility.Visible; //if (this.DataContext is MessageModel msg && msg.IsMine) //{ // Grid.SetColumn(this.pathResend, 0); // this.grid.ColumnDefinitions[0].Width = new GridLength(20, GridUnitType.Pixel); // this.Width = this.Width + 20; // this.pathResend.Margin = new Thickness(); // this.pathResend.ToolTip = "重新发送"; //} //else //{ // Grid.SetColumn(this.pathResend, 2); // this.grid.ColumnDefinitions[2].Width = new GridLength(20, GridUnitType.Pixel); // this.Width = this.Width + 20; // this.pathResend.Margin = new Thickness(); // this.pathResend.ToolTip = "重新接收"; //} break; case FileStates.WaitForReceieve: //App.Current.Dispatcher.Invoke(() => //{ // this.gridProgress.Visibility = Visibility.Collapsed; // this.borderPlay.Visibility = Visibility.Collapsed; // this.imgReset.Visibility = Visibility.Collapsed; // this.imgDownload.Visibility = Visibility.Visible; //}); break; case FileStates.Completed: if (_targetModel == null && this.DataContext is MessageModel item) { _targetModel = item; } App.Current.Dispatcher.BeginInvoke(new Action(() => { this.VideoPath = _targetModel?.Content; })); if (string.IsNullOrEmpty(_targetModel.ResourceModel.PreviewImagePath) || !System.IO.File.Exists(_targetModel.ResourceModel.PreviewImagePath)) { string targetKey = _targetModel.ResourceModel.PreviewKey; if (!string.IsNullOrEmpty(targetKey)) { _targetModel.ResourceModel.PreviewImagePath = _thumbnailPath = System.IO.Path.Combine(SDKClient.SDKClient.Instance.property.CurrentAccount.imgPath, targetKey); } else { _targetModel.ResourceModel.PreviewImagePath = _thumbnailPath = System.IO.Path.Combine(SDKClient.SDKClient.Instance.property.CurrentAccount.imgPath, Guid.NewGuid() + ".jpg"); } GetOneFrameImageFromVideo(); } App.Current.Dispatcher.Invoke(() => { this.gridProgress.Visibility = Visibility.Collapsed; this.borderPlay.Visibility = Visibility.Visible; this.imgReset.Visibility = Visibility.Collapsed; this.imgDownload.Visibility = Visibility.Collapsed; }); //HasContexMenu = true; _operateTask = null; AcioningItems.Remove(this); break; default: break; } }
private void CalFileState() { if (ChatViewModel == null) { ChatViewModel = AppData.MainMV.ChatListVM.SelectedItem; } else if (_targetModel?.MessageState == MessageStates.Fail) { if (_operateTask != null) { _operateTask.Cancel(); } AcioningItems.Remove(this); } if (_targetModel == null && this.DataContext is MessageModel item) { this.PreviewMouseRightButtonUp -= SmallVideo_PreviewMouseRightButtonUp; _targetModel = item; var target = FileItems.FirstOrDefault(info => info.MsgKey == item.MsgKey); if (target == null) { target = item; FileItems.Add(item); } if (string.IsNullOrEmpty(target.MsgKey)) { target.MsgKey = Guid.NewGuid().ToString(); } _completeTime = _targetModel.SendTime; _isMine = _targetModel.IsMine; _isSync = _targetModel.IsSync; this.DataContext = _targetModel = target; if (_completeTime != null) { _targetModel.SendTime = _completeTime.Value; } _targetModel.IsMine = _isMine; _targetModel.IsSync = _isSync; //this.gridProgress.Visibility = Visibility.Visible; //this.borderPlay.Visibility = Visibility.Collapsed; } else { if (_targetModel != null) { return; } this.gridProgress.Visibility = Visibility.Collapsed; this.borderPlay.Visibility = Visibility.Visible; this.imgReset.Visibility = Visibility.Collapsed; this.imgDownload.Visibility = Visibility.Collapsed; this.PreviewMouseRightButtonUp += SmallVideo_PreviewMouseRightButtonUp; return; } if (SDKClient.SDKClient.Instance.property.CurrentAccount.Isforbidden) { _targetModel.MessageState = MessageStates.Warn; } //是否发送者 bool isSender = _targetModel.Sender.ID == AppData.Current.LoginUser.ID; string filePath = System.IO.Path.Combine(SDKClient.SDKClient.Instance.property.CurrentAccount.filePath, this.VideoPath); if (System.IO.File.Exists(filePath) || System.IO.File.Exists(this.VideoPath)) { if (isSender) //当前作为发送方 { if (_targetModel.IsSending) //若是正在发送中 { this.FileState = _targetModel.ResourceModel.FileState = FileStates.SendOffline; this.gridProgress.Visibility = Visibility.Visible; this.borderPlay.Visibility = Visibility.Collapsed; this.imgReset.Visibility = Visibility.Collapsed; this.imgDownload.Visibility = Visibility.Collapsed; SendVideo(); } else if (_targetModel.ResourceModel.DBState == 0) { this.gridProgress.Visibility = Visibility.Collapsed; this.borderPlay.Visibility = Visibility.Visible; this.imgReset.Visibility = Visibility.Collapsed; this.imgDownload.Visibility = Visibility.Collapsed; //HasContexMenu = true; } else { this.FileState = _targetModel.ResourceModel.FileState = FileStates.Completed; if (_targetModel.IsSync) { _completeTime = _targetModel.SendTime; } else if (_completeTime == null) { _completeTime = DateTime.Now.AddMinutes(-5); } } } else //当前用户作为接收方,若文件已有则认为已经成功 { this.FileState = _targetModel.ResourceModel.FileState = FileStates.Completed; if (_targetModel.IsSync) { _completeTime = _targetModel.SendTime; } else if (_completeTime == null) { _completeTime = DateTime.Now.AddMinutes(-5); } } } else { if (isSender) //发送者 { this.FileState = _targetModel.ResourceModel.FileState = FileStates.WaitForReceieve; if (_targetModel.IsSync) { _completeTime = _targetModel.SendTime; } else if (_completeTime == null) { _completeTime = DateTime.Now.AddMinutes(-5); } } else { this.FileState = _targetModel.ResourceModel.FileState = FileStates.WaitForReceieve; } } }