2017-09-12 14:32:34 +02:00
using System ;
using System.Collections.Generic ;
2020-06-17 19:36:52 +02:00
using System.Collections.ObjectModel ;
using System.ComponentModel ;
2017-09-14 12:36:48 +02:00
using System.Diagnostics ;
using System.IO ;
2017-09-12 14:32:34 +02:00
using System.Linq ;
2020-06-17 19:36:52 +02:00
using System.Runtime.CompilerServices ;
2019-05-14 13:37:20 +02:00
using System.Text ;
2022-06-03 16:43:03 +02:00
using System.Threading ;
2022-07-11 16:01:14 +02:00
using System.Threading.Tasks ;
2017-09-12 14:32:34 +02:00
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Data ;
2019-04-04 13:00:10 +02:00
using System.Windows.Forms ;
2017-09-12 14:32:34 +02:00
using System.Windows.Input ;
using System.Windows.Media ;
2020-08-06 16:05:23 +02:00
using System.Windows.Media.Imaging ;
2017-09-12 14:32:34 +02:00
using System.Windows.Threading ;
2020-06-17 19:36:52 +02:00
using ChatController.Annotations ;
2017-09-12 14:32:34 +02:00
using ChatController.ChatKlassen ;
2019-09-03 15:15:12 +02:00
using ChatController.Extensions ;
2017-09-12 14:32:34 +02:00
using ChatController.HauptKlassen ;
2019-04-04 13:00:10 +02:00
using ChatController.LoginKlassen ;
using ChatController.Utilities ;
2019-05-14 13:37:20 +02:00
using ChatController.Utilities.Extensions ;
2021-07-14 19:33:50 +02:00
using Application = System . Windows . Forms . Application ;
using Button = System . Windows . Controls . Button ;
2022-07-11 16:01:14 +02:00
using CheckBox = System . Windows . Controls . CheckBox ;
2021-07-14 19:33:50 +02:00
using Cursors = System . Windows . Input . Cursors ;
using DataFormats = System . Windows . DataFormats ;
2019-04-04 13:00:10 +02:00
using KeyEventArgs = System . Windows . Input . KeyEventArgs ;
2021-07-14 19:33:50 +02:00
using MenuItem = System . Windows . Controls . MenuItem ;
using MessageBox = System . Windows . MessageBox ;
using Orientation = System . Windows . Controls . Orientation ;
using Panel = System . Windows . Controls . Panel ;
using Path = System . IO . Path ;
using ScrollBar = System . Windows . Controls . Primitives . ScrollBar ;
using Timer = System . Windows . Forms . Timer ;
2017-09-12 14:32:34 +02:00
namespace ChatController
{
2020-06-17 19:36:52 +02:00
public partial class ChatMainControl : INotifyPropertyChanged
2017-09-12 14:32:34 +02:00
{
2019-09-03 15:15:12 +02:00
private readonly Dictionary < long , List < NotifyIcon > > _Group2NotifyIcons = new Dictionary < long , List < NotifyIcon > > ( ) ;
2019-05-14 13:37:20 +02:00
private readonly Dictionary < int , SyncFileInfoStruct > _GroupId2DateTime = new Dictionary < int , SyncFileInfoStruct > ( ) ;
2020-08-06 16:05:23 +02:00
private string _ThreadExceptionMessage ;
public string ThreadExceptionMessage
{
get = > _ThreadExceptionMessage ;
set
{
2023-03-03 21:23:48 +01:00
if ( Equals ( _ThreadExceptionMessage , value ) )
2020-08-06 16:05:23 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
2020-08-06 16:05:23 +02:00
}
2023-03-03 21:23:48 +01:00
_ThreadExceptionMessage = value ;
OnPropertyChanged ( nameof ( ThreadExceptionMessage ) ) ;
OnPropertyChanged ( nameof ( CurrentContactInformationString ) ) ;
OnPropertyChanged ( nameof ( CurrencContactInfoForeground ) ) ;
OnPropertyChanged ( nameof ( ThreadExceptionImageSource ) ) ;
2020-08-06 16:05:23 +02:00
}
}
private readonly ImageSource _ThreadExceptionImageSource ;
2022-07-28 19:18:15 +02:00
public ImageSource ThreadExceptionImageSource = > ! ( ThreadExceptionMessage is null ) ? _ThreadExceptionImageSource : CurrentContact ? . Image ;
2020-08-06 16:05:23 +02:00
2022-07-28 19:18:15 +02:00
public SolidColorBrush CurrencContactInfoForeground = > ! ( ThreadExceptionMessage is null ) ? new SolidColorBrush ( Color . FromRgb ( 185 , 65 , 0 ) ) : CurrentContact ? . AccentColorBrush ? ? new SolidColorBrush ( Colors . Transparent ) ;
2020-08-06 16:05:23 +02:00
public string CurrentContactInformationString = > ThreadExceptionMessage ? ? CurrentContact ? . Name ;
2019-09-02 19:04:55 +02:00
private Window _ContainingWindow ;
public Window ContainingWindow
{
get = > _ContainingWindow ;
set
{
_ContainingWindow = value ;
2020-06-17 19:36:52 +02:00
2023-03-03 21:23:48 +01:00
if ( value is null )
2019-09-02 19:04:55 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
2019-09-02 19:04:55 +02:00
}
2023-03-03 21:23:48 +01:00
_ContainingWindow . Deactivated + = ContainingWindowOnDeactivated ;
_ContainingWindow . Activated + = ContainingWindowOnActivated ;
2019-09-02 19:04:55 +02:00
}
}
2019-05-14 13:37:20 +02:00
2022-07-28 19:18:15 +02:00
private Chat _Chat ;
public Chat Chat
{
get = > _Chat ;
set
{
_Chat = value ;
OnPropertyChanged ( nameof ( Chat ) ) ;
OnPropertyChanged ( nameof ( ChatMessageInputGridVisibility ) ) ;
OnPropertyChanged ( nameof ( ChatListBoxVisibility ) ) ;
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
OnPropertyChanged ( nameof ( StapelmodusChatGridVisibility ) ) ;
}
}
2020-06-17 19:36:52 +02:00
private Contact _CurrentContact ;
public Contact CurrentContact
{
get = > _CurrentContact ;
set
{
2023-03-03 21:23:48 +01:00
if ( Equals ( _CurrentContact , value ) )
2020-06-17 19:36:52 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
2020-06-17 19:36:52 +02:00
}
2023-03-03 21:23:48 +01:00
_CurrentContact = value ;
OnPropertyChanged ( nameof ( CurrentContact ) ) ;
OnPropertyChanged ( nameof ( ChatMessageInputGridVisibility ) ) ;
OnPropertyChanged ( nameof ( Chat ) ) ;
OnPropertyChanged ( nameof ( CurrentContactInformationString ) ) ;
OnPropertyChanged ( nameof ( CurrencContactInfoForeground ) ) ;
OnPropertyChanged ( nameof ( ThreadExceptionImageSource ) ) ;
2020-06-17 19:36:52 +02:00
}
}
2022-07-11 16:01:14 +02:00
public ObservableSortCollection < ContactDependencyObject > ContactList
2020-06-17 19:36:52 +02:00
{
2022-07-11 16:01:14 +02:00
get = > _ContactList ? ? ( _ContactList = new ObservableSortCollection < ContactDependencyObject > ( ) ) ;
set
2020-06-17 19:36:52 +02:00
{
2022-07-11 16:01:14 +02:00
_ContactList = new ObservableSortCollection < ContactDependencyObject > ( value ) ;
_ContactList . Sort ( ( x , y ) = > DateTime . Compare ( y . Contact . TimeStamp , x . Contact . TimeStamp ) ) ;
OnPropertyChanged ( nameof ( ContactList ) ) ;
2020-06-17 19:36:52 +02:00
}
}
public Visibility ChatMessageInputGridVisibility
{
get
{
2022-07-11 16:01:14 +02:00
if ( _CurrentContact is null & & Chat ? . IsInBroadcastMode = = false )
2020-06-17 19:36:52 +02:00
{
return Visibility . Collapsed ;
}
2022-07-28 19:18:15 +02:00
return false = = ( Chat ? . IsInBroadcastMode ? ? false ) & & ! ( _CurrentContact is null ) ? _CurrentContact . IsChatMessageInputGridVisible ? Visibility . Visible : Visibility . Collapsed : Chat ? . IsInBroadcastMode ? ? false ? Visibility . Visible : Visibility . Collapsed ;
2020-06-17 19:36:52 +02:00
}
}
2017-09-12 14:32:34 +02:00
2022-07-11 16:01:14 +02:00
private ObservableSortCollection < ContactDependencyObject > _ContactList ;
2017-11-09 12:20:03 +01:00
2019-04-02 13:43:30 +02:00
private bool _ScrollPrueferAktivieren ;
2017-09-12 14:32:34 +02:00
2019-05-15 14:57:16 +02:00
public bool ShouldInterruptContactsThread = true ;
2017-09-12 14:32:34 +02:00
2019-04-02 13:43:30 +02:00
private readonly List < string > _CreatedTempFiles = new List < string > ( ) ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
private readonly Dictionary < string , Action < string > > _MenuItemName2Callback = new Dictionary < string , Action < string > > ( ) ;
2017-10-18 10:09:20 +02:00
2019-04-02 13:43:30 +02:00
private bool _IsDesktopVersion = true ;
2017-10-06 12:08:05 +02:00
2019-09-02 19:04:55 +02:00
private bool _IsInBackground ;
2017-09-12 14:32:34 +02:00
public ChatMainControl ( )
{
2017-10-18 14:32:00 +02:00
InitializeComponent ( ) ;
2022-07-11 16:01:14 +02:00
2020-08-06 16:05:23 +02:00
_ThreadExceptionImageSource = new BitmapImage ( new Uri ( "pack://application:,,,/ChatController;component/Ressourcen/warning-exclamation-mark.png" , UriKind . Absolute ) ) ;
2022-07-28 19:18:15 +02:00
BroadcastButtonImageSource = new BitmapImage ( new Uri ( "pack://application:,,,/ChatController;component/Ressourcen/tasks-solid.png" , UriKind . Absolute ) ) ;
InvertedBroadcastButtonImageSource = new BitmapImage ( new Uri ( "pack://application:,,,/ChatController;component/Ressourcen/tasks-solid-inverted.png" , UriKind . Absolute ) ) ;
2019-05-14 13:37:20 +02:00
2021-07-14 19:33:50 +02:00
_ChatMessages = new ObservableCollection < ChatMessage > ( ) ;
ChatMessages = CollectionViewSource . GetDefaultView ( _ChatMessages ) as ListCollectionView ;
2022-07-11 16:01:14 +02:00
2022-07-28 19:18:15 +02:00
if ( ! ( ChatMessages is null ) )
2021-07-14 19:33:50 +02:00
{
ChatMessages . CustomSort = new ChatMessageComparer ( ) ;
}
2020-06-17 19:36:52 +02:00
DataContext = this ;
2022-07-28 19:18:15 +02:00
ProgressOverlayVisibility = Visibility . Collapsed ;
StartWaitingImmediately ( ) ;
var deleteImagesTask = new Task ( ( ) = >
{
try
{
var files = Directory . GetFiles ( Path . GetTempPath ( ) , "*_tmp_img_owch.*" , SearchOption . TopDirectoryOnly ) ;
foreach ( var file in files )
{
2023-03-03 21:23:48 +01:00
if ( ! File . Exists ( file ) )
2022-07-28 19:18:15 +02:00
{
2023-03-03 21:23:48 +01:00
continue ;
2022-07-28 19:18:15 +02:00
}
2023-03-03 21:23:48 +01:00
File . Delete ( file ) ;
2022-07-28 19:18:15 +02:00
}
}
finally
{
2023-03-03 21:23:48 +01:00
this . Dispatch ( EndWaiting ) ;
2022-07-28 19:18:15 +02:00
}
} ) ;
deleteImagesTask . Start ( ) ;
2017-09-12 14:32:34 +02:00
}
2019-09-02 19:04:55 +02:00
private void ContainingWindowOnDeactivated ( object sender , EventArgs e )
{
_IsInBackground = true ;
}
private void ContainingWindowOnActivated ( object sender , EventArgs e )
{
_IsInBackground = false ;
}
2021-07-14 19:33:50 +02:00
private readonly ObservableCollection < ChatMessage > _ChatMessages ;
public ListCollectionView ChatMessages { get ; set ; }
2017-09-12 14:32:34 +02:00
public void InitMitChatdaten ( ChatDatenUebergabe cdu )
{
2021-07-14 19:33:50 +02:00
Chat = new Chat ( cdu , exception = > {
2020-08-06 16:05:23 +02:00
this . Dispatch (
( ) = > {
EndWaiting ( ) ;
2021-07-14 19:33:50 +02:00
2020-08-06 16:05:23 +02:00
MessageBox . Show ( $"Fehler: {exception.Message}" , "Fehler" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
} ) ;
} ,
exception = > {
this . Dispatch (
( ) = > {
2022-05-27 11:50:37 +02:00
if ( exception is null )
2020-08-06 16:05:23 +02:00
{
ThreadExceptionMessage = null ;
}
ThreadExceptionMessage = exception ? . Message ? . Contains ( "500" ) ? ? false ? "ownChat ist vorrübergehend nicht verfügbar. Bitte versuchen Sie es später noch einmal." : null ;
} ) ;
2020-06-17 19:36:52 +02:00
} ) ;
2019-05-15 14:57:16 +02:00
2021-07-14 19:33:50 +02:00
OnPropertyChanged ( nameof ( Chat ) ) ;
2022-07-11 16:01:14 +02:00
_ContactList = new ObservableSortCollection < ContactDependencyObject > ( ) ;
foreach ( var contact in Chat . AddContacts ( ) )
{
ContactList . Add ( new ContactDependencyObject ( contact ) ) ;
}
ContactList . Sort ( ( x , y ) = > DateTime . Compare ( y . Contact . TimeStamp , x . Contact . TimeStamp ) ) ;
2021-07-14 19:33:50 +02:00
2020-06-17 19:36:52 +02:00
OnPropertyChanged ( nameof ( ContactList ) ) ;
2019-05-14 13:37:20 +02:00
ReadNewSyncFile ( ) ;
2022-07-28 19:18:15 +02:00
foreach ( var contactDependencyObject in _ContactList )
2019-05-14 13:37:20 +02:00
{
2022-07-28 19:18:15 +02:00
if ( _GroupId2DateTime . ContainsKey ( contactDependencyObject . Contact . GroupId ) )
2019-05-14 13:37:20 +02:00
{
2022-07-11 16:01:14 +02:00
var isUnread = _GroupId2DateTime [ contactDependencyObject . Contact . GroupId ] . IsUnread ;
2019-05-14 13:37:20 +02:00
2022-07-11 16:01:14 +02:00
contactDependencyObject . Contact . IsNewMessage = isUnread ;
contactDependencyObject . Contact . HasUnreadMessages = contactDependencyObject . Contact . IsNewMessage ;
2019-05-14 13:37:20 +02:00
}
}
2019-05-15 14:57:16 +02:00
GlobalListeningThreadtimer ? . Stop ( ) ;
GlobalListeningThreadtimer = null ;
2019-05-14 13:37:20 +02:00
InitGlobalListeningThread ( ) ;
2017-11-07 11:56:29 +01:00
Clientlist . Items . Refresh ( ) ;
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
2019-09-03 15:15:12 +02:00
public void ShowNotification ( string pTitle , GroupLatestMessage pMessage , int pGroupId )
2017-09-12 14:32:34 +02:00
{
2019-09-03 15:15:12 +02:00
var notificationIcon = Resource . ownchat_favicon ;
2019-05-14 13:37:20 +02:00
2022-07-11 16:01:14 +02:00
var contact = _ContactList . FirstOrDefault ( a = > a . Contact . GroupId = = pGroupId ) ;
2019-05-14 13:37:20 +02:00
2023-03-03 21:23:48 +01:00
if ( ! ( contact is null ) )
2019-09-03 15:15:12 +02:00
{
2022-07-11 16:01:14 +02:00
notificationIcon = Utils . ImageSourceToIcon ( contact . Contact . Image ) ;
2019-09-03 15:15:12 +02:00
}
2019-05-14 13:37:20 +02:00
2019-09-03 15:15:12 +02:00
var notifyIcon = new NotifyIcon { Icon = notificationIcon , Visible = true , Tag = pMessage } ;
2019-05-14 13:37:20 +02:00
2019-09-03 15:15:12 +02:00
notifyIcon . BalloonTipClicked + = ( sender , args ) = >
{
DisposeAndRemoveNotification ( ( NotifyIcon ) sender , pGroupId ) ;
2019-05-14 13:37:20 +02:00
2022-07-28 19:18:15 +02:00
if ( _ContainingWindow is null )
2021-02-25 12:33:00 +01:00
{
return ;
}
2019-09-03 15:15:12 +02:00
if ( _ContainingWindow . WindowState = = WindowState . Minimized )
{
_ContainingWindow . WindowState = WindowState . Normal ;
}
2019-09-02 19:04:55 +02:00
2019-09-03 15:15:12 +02:00
_ContainingWindow . Activate ( ) ;
2019-09-02 19:04:55 +02:00
2022-07-11 16:01:14 +02:00
var selectedContact = _ContactList ? . FirstOrDefault ( f = > f ? . Contact . GroupId = = pGroupId ) ;
2023-06-28 19:04:03 +02:00
if ( ! ( selectedContact ? . Contact is null ) )
2021-02-25 12:33:00 +01:00
{
2022-07-11 16:01:14 +02:00
SelectContact ( selectedContact . Contact ) ;
2021-02-25 12:33:00 +01:00
}
2019-09-03 15:15:12 +02:00
} ;
2020-09-15 13:42:46 +02:00
2019-09-03 15:15:12 +02:00
notifyIcon . BalloonTipClosed + = ( sender , args ) = >
{
2023-03-03 21:23:48 +01:00
// Wird beim ausblenden bzw. automatischem Schließen des "Balloons" ausgelöst
2020-09-25 14:55:40 +02:00
DisposeAndRemoveNotification ( ( NotifyIcon ) sender , pGroupId ) ;
2019-09-03 15:15:12 +02:00
} ;
2019-05-14 13:37:20 +02:00
2019-09-03 15:15:12 +02:00
_Group2NotifyIcons . AddOrUpdateValueInDictionary ( pGroupId , notifyIcon ) ;
2017-09-12 14:32:34 +02:00
2019-09-03 15:15:12 +02:00
_Group2NotifyIcons [ pGroupId ] . Find ( f = > f . Tag is GroupLatestMessage message & & message . Id . Equals ( pMessage . Id ) ) ? . ShowBalloonTip ( Constants . NotificationTimeout , pTitle , "Sie haben eine neue Nachricht" , ToolTipIcon . None ) ;
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
public void ClearNotifications ( )
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
foreach ( var group2NotifyIcon in _Group2NotifyIcons )
2017-09-12 14:32:34 +02:00
{
2019-09-03 15:15:12 +02:00
foreach ( var notifyIcon in group2NotifyIcon . Value )
{
2020-09-15 13:42:46 +02:00
notifyIcon . Icon = null ;
2019-09-03 15:15:12 +02:00
notifyIcon . Dispose ( ) ;
2020-09-15 13:42:46 +02:00
Application . DoEvents ( ) ;
2019-09-03 15:15:12 +02:00
}
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
_Group2NotifyIcons . Clear ( ) ;
}
private void DisposeAndRemoveNotification ( IDisposable pNotification , long pGroupId )
{
pNotification . Dispose ( ) ;
_Group2NotifyIcons . Remove ( pGroupId ) ;
2020-09-25 14:55:40 +02:00
2020-09-15 13:42:46 +02:00
Application . DoEvents ( ) ;
2017-09-12 14:32:34 +02:00
}
2020-08-06 16:05:23 +02:00
private void CurrentContactImage_OnMouseDoubleClick ( object sender , MouseButtonEventArgs e )
2017-09-12 14:32:34 +02:00
{
2020-08-06 16:05:23 +02:00
if ( e . ClickCount = = 2 )
{
2021-07-14 19:33:50 +02:00
Chat . ShowProfilePicture ( _CurrentContact ) ;
2020-08-06 16:05:23 +02:00
}
2017-09-12 14:32:34 +02:00
}
private void Clientlist_OnSelectionChanged ( object sender , SelectionChangedEventArgs e )
{
2022-07-11 16:01:14 +02:00
if ( Chat . IsInBroadcastMode )
2017-09-15 09:49:32 +02:00
{
2022-07-11 16:01:14 +02:00
return ;
}
2023-06-28 19:04:03 +02:00
if ( Clientlist ? . SelectedItem is ContactDependencyObject selectedContactDependencyObject & & ! ( selectedContactDependencyObject . Contact is null ) )
2022-07-11 16:01:14 +02:00
{
SelectContact ( selectedContactDependencyObject . Contact ) ;
2019-05-14 13:37:20 +02:00
}
}
2017-09-12 14:32:34 +02:00
2023-06-28 19:04:03 +02:00
// ToDo: Hier kommt es immer wieder zu einer NullPointerException
2022-07-28 19:18:15 +02:00
private void SelectContact ( Contact contact )
2019-05-14 13:37:20 +02:00
{
2023-07-24 18:04:56 +02:00
if ( contact is null )
2019-05-15 14:57:16 +02:00
{
2023-07-24 18:04:56 +02:00
return ;
}
2019-05-15 14:57:16 +02:00
2023-07-24 18:04:56 +02:00
ChatMessageFileWrapper = null ;
2022-07-28 19:18:15 +02:00
2023-07-24 18:04:56 +02:00
ShouldInterruptContactsThread = true ;
2017-09-12 14:32:34 +02:00
2023-07-24 18:04:56 +02:00
StartWaitingImmediately ( ) ;
2021-07-14 19:33:50 +02:00
2023-07-24 18:04:56 +02:00
_ScrollPrueferAktivieren = false ;
2017-09-12 14:32:34 +02:00
2024-02-23 17:25:25 +01:00
if ( contact ? . HasUnreadMessages ? ? false )
2023-07-24 18:04:56 +02:00
{
contact . HasUnreadMessages = false ;
2017-09-12 14:32:34 +02:00
2023-07-24 18:04:56 +02:00
Clientlist ? . Items . Refresh ( ) ;
OnPropertyChanged ( nameof ( ContactList ) ) ;
}
2017-09-12 14:32:34 +02:00
2023-07-24 18:04:56 +02:00
var currentContact = contact ;
2017-09-12 14:32:34 +02:00
2023-07-24 18:04:56 +02:00
contact . IsNewMessage = false ;
2017-09-12 14:32:34 +02:00
2023-07-31 17:28:05 +02:00
var shouldChangeIcon = _ContactList . Any ( contactDependencyObject = > contactDependencyObject ? . Contact ? . IsNewMessage ? ? false ) ;
2023-07-24 18:04:56 +02:00
if ( shouldChangeIcon )
{
ContainingWindow . Icon = Utils . ConvertIconToImageSource ( Resource . ownchat_favicon ) ;
}
2019-09-02 19:04:55 +02:00
2023-07-24 18:04:56 +02:00
CurrentContact = currentContact ;
2022-07-28 19:18:15 +02:00
2023-07-24 18:04:56 +02:00
OnPropertyChanged ( nameof ( IsSendButtonEnabled ) ) ;
2023-06-28 19:04:03 +02:00
2023-07-24 18:04:56 +02:00
if ( ! ( Clientlist is null ) )
{
foreach ( var obj in Clientlist . Items )
2021-11-18 12:33:13 +01:00
{
2024-02-23 17:25:25 +01:00
if ( obj is ContactDependencyObject contactDependencyObject & & contact ! = null & & contactDependencyObject . Contact ! = null & & Equals ( contactDependencyObject . Contact , contact ) )
2023-06-28 19:04:03 +02:00
{
2023-07-24 18:04:56 +02:00
Clientlist . SelectedItem = obj ;
2023-06-28 19:04:03 +02:00
}
2021-11-18 12:33:13 +01:00
}
2023-07-24 18:04:56 +02:00
}
2019-09-02 19:04:55 +02:00
2024-02-23 17:25:25 +01:00
_ChatMessages ? . Clear ( ) ;
2023-07-24 18:04:56 +02:00
OnPropertyChanged ( nameof ( _ChatMessages ) ) ;
2017-12-18 18:22:13 +01:00
2024-02-23 17:25:25 +01:00
Chat ? . LoadChatMessagesForContactAsync ( currentContact , GetFirstMessage ( ) , chatMessages = >
2023-07-24 18:04:56 +02:00
{
this . Dispatch ( ( ) = >
2021-07-14 19:33:50 +02:00
{
2023-07-24 18:04:56 +02:00
_ChatMessages . Clear ( ) ;
_ChatMessages . AddRangeIfElementsNotIn ( chatMessages ) ;
OnPropertyChanged ( nameof ( _ChatMessages ) ) ;
2021-07-14 19:33:50 +02:00
2023-07-24 18:04:56 +02:00
WpfUtils . ScrollToBottomOfListBox ( ChatListBox ) ;
2017-12-18 18:22:13 +01:00
2023-07-24 18:04:56 +02:00
ChatListBox . ContextMenu = Chat . ErstelleKontextMenue ( ) ;
SetContextHandler ( ) ;
_ScrollPrueferAktivieren = true ;
2021-07-14 19:33:50 +02:00
2023-07-24 18:04:56 +02:00
ListenForMessages ( ) ;
Cursor = Cursors . Arrow ;
2017-11-10 11:41:09 +01:00
2023-07-24 18:04:56 +02:00
WriteToNewSyncFile ( ) ;
2017-12-18 18:22:13 +01:00
2023-07-24 18:04:56 +02:00
ShouldInterruptContactsThread = false ;
2019-05-14 13:37:20 +02:00
2023-07-24 18:04:56 +02:00
EndWaiting ( ) ;
2021-07-14 19:33:50 +02:00
} ) ;
2023-07-24 18:04:56 +02:00
} ) ;
2017-09-12 14:32:34 +02:00
}
private void SetContextHandler ( )
{
2019-05-14 13:37:20 +02:00
var contextItems = ChatListBox ? . ContextMenu ? . Items ;
2017-09-18 13:07:37 +02:00
2022-07-28 19:18:15 +02:00
if ( contextItems is null )
2020-06-17 19:36:52 +02:00
{
return ;
}
2022-07-28 19:18:15 +02:00
foreach ( var menuItemText in _MenuItemName2Callback . Keys )
2017-09-12 14:32:34 +02:00
{
2020-06-17 19:36:52 +02:00
var callback = _MenuItemName2Callback [ menuItemText ] ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
var menuItem = new MenuItem ( ) ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
menuItem . Click + = ( s , e2 ) = >
2017-09-12 14:32:34 +02:00
{
2020-06-17 19:36:52 +02:00
callback ( menuItemText ) ;
2017-09-12 14:32:34 +02:00
} ;
2020-06-17 19:36:52 +02:00
2019-05-14 13:37:20 +02:00
menuItem . Header = menuItemText ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
contextItems . Add ( menuItem ) ;
2017-09-12 14:32:34 +02:00
}
2020-06-17 19:36:52 +02:00
if ( contextItems . Count > 0 )
{
var saveAsMenuItem = ( MenuItem ) contextItems [ 0 ] ;
saveAsMenuItem . Click + = SaveAsOnClick ;
}
if ( contextItems . Count > 1 )
{
var pasteMenuItem = ( MenuItem ) contextItems [ 1 ] ;
pasteMenuItem . Click + = PasteOnClick ;
}
2017-09-18 13:07:37 +02:00
// neu und vor Einfügen setzen und anpassen
2023-03-03 21:23:48 +01:00
if ( contextItems . Count < = 2 )
2020-06-17 19:36:52 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
2020-06-17 19:36:52 +02:00
}
2023-03-03 21:23:48 +01:00
var copyMenuItem = ( MenuItem ) contextItems [ 2 ] ;
copyMenuItem . Click + = CopyOnClick ;
2017-09-12 14:32:34 +02:00
}
2020-06-17 19:36:52 +02:00
private void SaveAsOnClick ( object sender , RoutedEventArgs routedEventArgs )
2017-09-12 14:32:34 +02:00
{
2023-07-24 18:04:56 +02:00
var selectedChatMessages = new List < ChatMessage > ( ) ;
foreach ( var selectedItem in ChatListBox . SelectedItems )
2017-09-14 12:36:48 +02:00
{
2023-07-24 18:04:56 +02:00
if ( selectedItem is ChatMessage message )
{
selectedChatMessages . AddIfNotIn ( message ) ;
}
}
2017-09-12 14:32:34 +02:00
2023-07-24 18:04:56 +02:00
foreach ( var message in selectedChatMessages )
{
Chat . SaveFileAs ( message ) ;
2017-09-14 12:36:48 +02:00
}
}
2017-09-12 14:32:34 +02:00
2022-07-11 16:01:14 +02:00
private async void PasteOnClick ( object sender , RoutedEventArgs routedEventArgs )
2017-09-14 12:36:48 +02:00
{
2022-07-11 16:01:14 +02:00
await Chat . Paste ( CurrentContact . GroupId , this ) ;
2017-09-18 13:07:37 +02:00
}
2020-06-17 19:36:52 +02:00
private void CopyOnClick ( object sender , RoutedEventArgs routedEventArgs )
2017-09-18 13:07:37 +02:00
{
2022-07-28 19:18:15 +02:00
var textToCopy = string . Empty ;
2020-06-17 19:36:52 +02:00
2017-12-11 14:32:56 +01:00
foreach ( var items in ChatListBox . SelectedItems )
2017-09-18 13:07:37 +02:00
{
2020-06-17 19:36:52 +02:00
var item = ( ChatMessage ) items ;
2017-09-18 13:07:37 +02:00
2022-07-28 19:18:15 +02:00
textToCopy + = item . UserMessage + Environment . NewLine ;
2017-09-18 13:07:37 +02:00
}
2022-07-28 19:18:15 +02:00
if ( ! string . IsNullOrWhiteSpace ( textToCopy ) )
2020-06-17 19:36:52 +02:00
{
2022-07-28 19:18:15 +02:00
Chat . Copy ( textToCopy , 1 ) ;
2020-06-17 19:36:52 +02:00
}
2017-09-14 12:36:48 +02:00
}
2017-09-18 13:07:37 +02:00
2017-09-14 12:36:48 +02:00
private void Chat_OnContextMenuOpening ( object sender , ContextMenuEventArgs e )
{
2022-07-28 19:18:15 +02:00
if ( ChatListBox ? . ContextMenu is null )
2020-06-17 19:36:52 +02:00
{
return ;
}
2017-09-14 12:36:48 +02:00
2020-06-17 19:36:52 +02:00
if ( ChatListBox . SelectedItems is List < ChatMessage > chatMessages )
2017-09-14 12:36:48 +02:00
{
2020-06-17 19:36:52 +02:00
if ( chatMessages . Count > 0 )
2017-09-14 12:36:48 +02:00
{
2022-07-28 19:18:15 +02:00
foreach ( var chatMessage in chatMessages )
2017-09-14 12:36:48 +02:00
{
2022-07-28 19:18:15 +02:00
if ( ! ( chatMessage ? . PictureSource is null ) )
2020-06-17 19:36:52 +02:00
{
2022-07-28 19:18:15 +02:00
if ( ! ( ChatListBox . ContextMenu is null ) )
2020-06-17 19:36:52 +02:00
{
var contextItems = ChatListBox . ContextMenu . Items ;
( ( MenuItem ) contextItems [ 0 ] ) . Visibility = Visibility . Visible ;
}
}
else
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 0 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
2020-06-17 19:36:52 +02:00
}
2017-09-18 13:07:37 +02:00
2022-07-28 19:18:15 +02:00
if ( chatMessage ? . PictureSource is null & & chatMessage ? . FilePath is null )
2020-06-17 19:36:52 +02:00
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 2 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Visible ;
2020-06-17 19:36:52 +02:00
}
else
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 2 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
2020-06-17 19:36:52 +02:00
}
2017-09-18 13:07:37 +02:00
}
2017-09-14 12:36:48 +02:00
}
2020-06-17 19:36:52 +02:00
else
{
//Schalte speichern unter Aus
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 0 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
2020-06-17 19:36:52 +02:00
}
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
//prüfe ob was in dem Speicher vorhanden ist //Einfügen
var dataObject = System . Windows . Forms . Clipboard . GetDataObject ( ) ;
2017-09-18 13:07:37 +02:00
2022-07-28 19:18:15 +02:00
if ( ! ( dataObject is null ) & & dataObject . GetDataPresent ( DataFormats . FileDrop ) )
2020-06-17 19:36:52 +02:00
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Visible ;
2020-06-17 19:36:52 +02:00
}
2022-07-28 19:18:15 +02:00
else if ( ! ( dataObject is null ) & & dataObject . GetDataPresent ( DataFormats . Text ) )
2020-06-17 19:36:52 +02:00
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Visible ;
2020-06-17 19:36:52 +02:00
}
2022-07-28 19:18:15 +02:00
else if ( ! ( dataObject is null ) & & dataObject . GetDataPresent ( DataFormats . Bitmap ) )
2020-06-17 19:36:52 +02:00
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Visible ;
2020-06-17 19:36:52 +02:00
}
else
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
2020-06-17 19:36:52 +02:00
}
2017-09-15 09:49:32 +02:00
2020-08-06 16:05:23 +02:00
// Prüfe, ob Dokumentation erlaubt
2022-07-28 19:18:15 +02:00
if ( CurrentContact . UserIdManage is null & & ChatListBox . ContextMenu . Items . Count > 3 )
2020-06-17 19:36:52 +02:00
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 3 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
2020-06-17 19:36:52 +02:00
}
else if ( ChatListBox . ContextMenu . Items . Count > 3 )
{
var contextItems = ChatListBox . ContextMenu . Items ;
2021-09-07 19:32:10 +02:00
var contextItemSpeichernUnter = ( MenuItem ) contextItems [ 3 ] ;
contextItemSpeichernUnter . Visibility = Visibility . Visible ;
2020-06-17 19:36:52 +02:00
}
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
if ( ChatListBox . ContextMenu . Items . Count = = 0 )
{
ChatListBox . ContextMenu . Visibility = Visibility . Collapsed ;
}
2017-09-12 14:32:34 +02:00
}
}
2022-07-28 19:18:15 +02:00
private void MediaButton_OnClick ( object sender , RoutedEventArgs e )
2017-09-12 14:32:34 +02:00
{
2022-07-28 19:18:15 +02:00
if ( ! ( CurrentContact is null ) )
2017-09-14 12:36:48 +02:00
{
2021-09-07 19:32:10 +02:00
var pathToOriginalImage = Chat . OpenFile ( ) ;
2017-09-12 14:32:34 +02:00
2023-03-03 21:23:48 +01:00
if ( ! File . Exists ( pathToOriginalImage ) )
2020-06-17 19:36:52 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
}
2017-11-17 15:40:11 +01:00
2023-03-03 21:23:48 +01:00
StartWaitingImmediately ( "Skaliere Bild ..." ) ;
var scalingTask = new Task ( ( ) = >
{
try
2017-11-17 15:40:11 +01:00
{
2023-03-03 21:23:48 +01:00
var pathToScaledImage = FileUtils . ScaleImage ( pathToOriginalImage , Path . GetExtension ( pathToOriginalImage . ToUpperInvariant ( ) ) , Chat . ChatDaten . MaxUploadSize ) ;
2021-07-14 19:33:50 +02:00
2023-03-03 21:23:48 +01:00
var isFileSizeTooLarge = FileUtils . CheckFileSize ( pathToScaledImage , Chat . ChatDaten . MaxUploadSize ) ;
2021-07-14 19:33:50 +02:00
2023-03-03 21:23:48 +01:00
if ( isFileSizeTooLarge )
{
this . Dispatch ( ( ) = >
2022-07-28 19:18:15 +02:00
{
2023-03-03 21:23:48 +01:00
EndWaiting ( ) ;
MessageBox . Show ( $"Die ausgewählte Datei ist zu groß. Die maximale Größe beträgt {Chat.ChatDaten.MaxUploadSize / 1000 / 1000} MB" , "Senden nicht möglich" , MessageBoxButton . OK , MessageBoxImage . Warning ) ;
} ) ;
2017-11-17 15:40:11 +01:00
2023-03-03 21:23:48 +01:00
return ;
2022-07-28 19:18:15 +02:00
}
2023-03-03 21:23:48 +01:00
if ( File . Exists ( pathToScaledImage ) )
2017-11-17 15:40:11 +01:00
{
2023-03-03 21:23:48 +01:00
ChatMessageFileWrapper = new ChatMessageFileWrapper ( pathToOriginalImage , pathToScaledImage , $"{Chat.ChatDaten.ServerUrl}/document.png" ) ;
2017-11-17 15:40:11 +01:00
}
2023-03-03 21:23:48 +01:00
}
finally
{
this . Dispatch ( EndWaiting ) ;
}
} ) ;
2022-07-28 19:18:15 +02:00
2023-03-03 21:23:48 +01:00
scalingTask . Start ( ) ;
2017-09-14 12:36:48 +02:00
}
else
{
2017-12-11 14:32:56 +01:00
MessageBox . Show ( "Bitte wählen Sie einen Kontakt aus." , "ownChat" , MessageBoxButton . OK , MessageBoxImage . Information ) ;
2017-09-14 12:36:48 +02:00
}
2017-09-12 14:32:34 +02:00
}
2022-07-28 19:18:15 +02:00
private async Task SendMessage ( )
2017-09-12 14:32:34 +02:00
{
2022-07-28 19:18:15 +02:00
if ( CurrentContact is null | | ( string . IsNullOrEmpty ( Chatbox . Text ) & & string . IsNullOrWhiteSpace ( ChatMessageFileWrapper ? . ScaledFilePath ) ) )
2022-07-11 16:01:14 +02:00
{
return ;
}
2022-07-28 19:18:15 +02:00
var isFileInMessage = ! ( ChatMessageFileWrapper is null ) & & File . Exists ( ChatMessageFileWrapper . ScaledFilePath ) ;
var messageText = Chatbox . Text ;
2017-12-11 14:32:56 +01:00
2022-07-28 19:18:15 +02:00
var newMessages = isFileInMessage ?
Chat . AddNewFile ( ChatMessageFileWrapper . ScaledFilePath , ChatMessageFileWrapper . OriginalFilePath , CurrentContact . GroupId , GetFirstMessage ( ) , messageText ) :
Chat . AddNewMessage ( Chatbox . Text , CurrentContact . GroupId , GetFirstMessage ( ) ) ;
2017-10-24 11:39:59 +02:00
2022-07-28 19:18:15 +02:00
_ChatMessages . AddRangeIfElementsNotIn ( newMessages ) ;
2020-06-17 19:36:52 +02:00
2022-07-28 19:18:15 +02:00
OnPropertyChanged ( nameof ( ChatMessages ) ) ;
2021-07-14 19:33:50 +02:00
2022-07-28 19:18:15 +02:00
ChatMessages . MoveCurrentToLast ( ) ;
ChatListBox . ScrollIntoView ( ChatMessages . CurrentItem ) ;
WpfUtils . ScrollToBottomOfListBox ( ChatListBox ) ;
if ( isFileInMessage )
{
await Chat . SendFileToContact ( CurrentContact , ChatMessageFileWrapper . ScaledFilePath , ChatMessageFileWrapper . OriginalFilePath , Chatbox . Text ) ;
}
else
{
2022-07-11 16:01:14 +02:00
await Chat . SendMessage ( Chatbox . Text , CurrentContact . GroupId ) ;
2022-07-28 19:18:15 +02:00
}
2020-06-17 19:36:52 +02:00
2022-07-28 19:18:15 +02:00
ChatMessageFileWrapper = null ;
Chatbox . Text = string . Empty ;
}
public Visibility ChatListBoxVisibility = > ( Chat ? . IsInBroadcastMode ? ? false ) | | ! ( ChatMessageFileWrapper is null ) ? Visibility . Collapsed : Visibility . Visible ;
public Visibility FilePreviewGridVisibility = > string . IsNullOrWhiteSpace ( ChatMessageFileWrapper ? . OriginalFilePath ) ? Visibility . Collapsed : Visibility . Visible ;
private ChatMessageFileWrapper _ChatMessageFileWrapper ;
public ChatMessageFileWrapper ChatMessageFileWrapper
{
get = > _ChatMessageFileWrapper ;
set
{
_ChatMessageFileWrapper = value ;
2022-05-30 20:48:14 +02:00
2022-07-28 19:18:15 +02:00
OnPropertyChanged ( nameof ( ChatMessageFileWrapper ) ) ;
OnPropertyChanged ( nameof ( IsSendButtonEnabled ) ) ;
OnPropertyChanged ( nameof ( FilePreviewGridVisibility ) ) ;
OnPropertyChanged ( nameof ( ChatListBoxVisibility ) ) ;
2017-09-12 14:32:34 +02:00
}
}
2022-07-28 19:18:15 +02:00
private async void SendButton_OnClick ( object sender , RoutedEventArgs e )
{
if ( Chat . IsInBroadcastMode )
{
SendBroadcastMessage ( ) ;
return ;
}
2023-07-24 18:04:56 +02:00
StartWaitingImmediately ( ) ;
2022-07-28 19:18:15 +02:00
await SendMessage ( ) ;
2023-07-24 18:04:56 +02:00
EndWaiting ( ) ;
CheckForNewMessagesForContact ( CurrentContact ) ;
ListenGloballyForMessages ( ) ;
2022-07-28 19:18:15 +02:00
}
2017-09-12 14:32:34 +02:00
private void Chatbox_OnGotFocus ( object sender , RoutedEventArgs e )
{
2023-03-03 21:23:48 +01:00
if ( ! Chatbox . Text . Equals ( "Nachricht schreiben" ) )
2017-09-12 14:32:34 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
2017-09-12 14:32:34 +02:00
}
2023-03-03 21:23:48 +01:00
Chatbox . Text = string . Empty ;
Chatbox . Foreground = new SolidColorBrush ( Colors . Black ) ;
2017-09-12 14:32:34 +02:00
}
private void Chatbox_OnKeyDownHandler ( object sender , KeyEventArgs e )
{
2023-03-03 21:23:48 +01:00
//if (e.Key == Key.Return)
//{
// if(CurrentContact != null && !string.IsNullOrEmpty(Chatbox.Text))
// {
// Chat.AddNewMessage(Chatbox.Text, CurrentContact.GroupId);
2020-06-17 19:36:52 +02:00
2023-03-03 21:23:48 +01:00
// OnPropertyChanged(nameof(CurrentChatMessages));
2020-06-17 19:36:52 +02:00
2023-03-03 21:23:48 +01:00
// ChatListBox.Items.MoveCurrentToLast();
// ChatListBox.ScrollIntoView(ChatListBox.Items.CurrentItem);
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
// Chat.SendMessage(Chatbox.Text, CurrentContact.GroupId);
2017-10-24 11:39:59 +02:00
2023-03-03 21:23:48 +01:00
// Chatbox.Text = string.Empty;
// }
//}
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
OnPropertyChanged ( nameof ( IsSendButtonEnabled ) ) ;
2017-09-12 14:32:34 +02:00
}
2017-10-24 11:39:59 +02:00
2022-07-11 16:01:14 +02:00
private void Chatbox_OnKeyUpHandler ( object sender , KeyEventArgs e )
{
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
OnPropertyChanged ( nameof ( IsSendButtonEnabled ) ) ;
}
2019-05-14 13:37:20 +02:00
public delegate void EmojiiDelegate ( Button button ) ;
2017-09-12 14:32:34 +02:00
public event EmojiiDelegate OnEmojii ;
private void EmojiButton_OnClick ( object sender , RoutedEventArgs e )
2020-06-17 19:36:52 +02:00
{
OnEmojii ? . Invoke ( EmojiButton ) ;
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
2017-09-12 14:32:34 +02:00
private void Chat_OnScrollChanged ( object sender , ScrollChangedEventArgs e )
{
2020-06-17 19:36:52 +02:00
var scrollBarList = Utils . GetVisualChildCollection < ScrollBar > ( ChatListBox ) ;
2023-03-03 21:23:48 +01:00
foreach ( var scrollBar in scrollBarList )
2017-09-12 14:32:34 +02:00
{
2023-03-03 21:23:48 +01:00
if ( scrollBar . Orientation = = Orientation . Horizontal )
2017-09-12 14:32:34 +02:00
{
2019-04-02 13:43:30 +02:00
_ScrollPrueferAktivieren = true ;
2017-09-12 14:32:34 +02:00
}
else
{
scrollBar . ValueChanged + = VerticalScrollbarChanged ;
}
}
}
private void VerticalScrollbarChanged ( object sender , RoutedPropertyChangedEventArgs < double > routedPropertyChangedEventArgs )
{
2023-03-03 21:23:48 +01:00
if ( ! _ScrollPrueferAktivieren )
2017-09-12 14:32:34 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
}
2017-09-12 14:32:34 +02:00
2023-03-03 21:23:48 +01:00
var scrollBar = ( ScrollBar ) sender ;
2021-07-14 19:33:50 +02:00
2023-03-03 21:23:48 +01:00
if ( scrollBar . Value > 0 )
{
return ;
}
2017-12-11 14:32:56 +01:00
2023-03-03 21:23:48 +01:00
StartWaitingImmediately ( ) ;
2017-09-12 14:32:34 +02:00
2023-03-03 21:23:48 +01:00
_ScrollPrueferAktivieren = false ;
2017-09-12 14:32:34 +02:00
2023-03-03 21:23:48 +01:00
ChatMessages . MoveCurrentToFirst ( ) ;
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
var currentChatMessage = ChatMessages . CurrentItem ;
if ( Clientlist . SelectedItem is null )
{
return ;
}
2017-09-12 14:32:34 +02:00
2023-03-03 21:23:48 +01:00
var selectedContact = ( ( ContactDependencyObject ) Clientlist . SelectedItem ) . Contact ;
2017-12-11 14:32:56 +01:00
2023-03-03 21:23:48 +01:00
Chat . LoadMoreMessagesAsync ( selectedContact , GetFirstMessage ( ) , chatMessages = >
{
this . Dispatch ( ( ) = >
{
EndWaiting ( ) ;
_ChatMessages . AddRangeIfElementsNotIn ( chatMessages ) ;
ChatListBox . ScrollIntoView ( currentChatMessage ) ;
} ) ;
} ) ;
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
private void ListenForMessages ( )
{
2020-06-17 19:36:52 +02:00
EmojisListeningThreadTimer ? . Stop ( ) ;
EmojisListeningThreadTimer = null ;
2020-08-06 16:05:23 +02:00
InitListeningThread ( CurrentContact ) ;
2019-05-14 13:37:20 +02:00
}
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
public Timer EmojisListeningThreadTimer ;
2019-05-15 14:57:16 +02:00
public Timer GlobalListeningThreadtimer { get ; set ; }
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
private void InitGlobalListeningThread ( )
2017-09-12 14:32:34 +02:00
{
2019-05-15 14:57:16 +02:00
GlobalListeningThreadtimer = new Timer ( ) ;
2022-07-28 19:18:15 +02:00
GlobalListeningThreadtimer . Tick + = ( s , e ) = > { ListenGloballyForMessages ( ) ; } ;
2020-08-06 16:05:23 +02:00
GlobalListeningThreadtimer . Interval = 10000 ;
2019-05-15 14:57:16 +02:00
GlobalListeningThreadtimer . Start ( ) ;
2019-05-14 13:37:20 +02:00
}
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
private void ListenGloballyForMessages ( )
2017-09-12 14:32:34 +02:00
{
2021-07-14 19:33:50 +02:00
Chat . GetNumberOfAllNewMessagesAsync ( newMessagesCount = >
2019-05-14 13:37:20 +02:00
{
2021-07-14 19:33:50 +02:00
if ( newMessagesCount > 0 )
2017-09-12 14:32:34 +02:00
{
2021-07-14 19:33:50 +02:00
Chat . UpdateContactList ( updatedContactList = >
2019-05-14 13:37:20 +02:00
{
2021-07-14 19:33:50 +02:00
var updatedContacts = updatedContactList . ToList ( ) ;
2020-09-25 14:55:40 +02:00
2021-07-14 19:33:50 +02:00
this . Dispatch ( ( ) = >
{
2023-03-03 21:23:48 +01:00
foreach ( var contactDependencyObject in _ContactList )
2017-12-11 14:32:56 +01:00
{
2023-03-03 21:23:48 +01:00
foreach ( var newContact in updatedContacts )
2019-05-14 13:37:20 +02:00
{
2023-03-03 21:23:48 +01:00
if ( contactDependencyObject . Contact . GroupId ! = newContact . GroupId )
{
continue ;
}
// ReceivedMessage kann null sein!
if ( contactDependencyObject . Contact . ReceivedMessage is null | | contactDependencyObject . Contact . ReceivedMessage . Id . Equals ( newContact . ReceivedMessage ? . Id ) )
{
continue ;
}
contactDependencyObject . Contact . ReceivedMessage = newContact . ReceivedMessage ;
contactDependencyObject . Contact . TimeStamp = newContact . TimeStamp ;
Contact selectedContact = null ;
if ( Clientlist . SelectedItem is ContactDependencyObject selectedContactDependencyObject )
{
selectedContact = selectedContactDependencyObject . Contact ;
}
// Angemeldeter Benutzer
var userOid = Chat . ChatDaten . LoggedInUser . Response . User . Oid ;
var senderId = contactDependencyObject . Contact . ReceivedMessage ? . UserId ;
var receiverId = userOid ;
var receiverGroupId = newContact . GroupId ;
var selectedGroupId = selectedContact ? . GroupId ;
if ( senderId ! = receiverId & & ( _IsInBackground | | receiverGroupId ! = selectedGroupId ) )
2019-05-14 13:37:20 +02:00
{
2023-03-03 21:23:48 +01:00
if ( ! ( ContainingWindow is null ) )
2020-11-11 15:08:31 +01:00
{
2023-03-03 21:23:48 +01:00
ContainingWindow . Icon = Utils . ConvertIconToImageSource ( Resource . oC_favico_NewMessage ) ;
2020-11-11 15:08:31 +01:00
}
2023-03-03 21:23:48 +01:00
ShowNotification ( newContact . Name , newContact . ReceivedMessage , newContact . GroupId ) ;
2019-05-14 13:37:20 +02:00
}
2023-03-03 21:23:48 +01:00
// Wenn der momentan ausgewählte Kontakt der aktuelle Kontakt in der Schleife ist, werden die Nachrichten nicht als ungelesen angezeigt, sonst schon.
if ( ! ( CurrentContact is null ) & & newContact . GroupId = = CurrentContact . GroupId )
{
contactDependencyObject . Contact . HasUnreadMessages = false ;
}
else
2023-02-15 18:43:15 +01:00
{
2023-03-03 21:23:48 +01:00
contactDependencyObject . Contact . HasUnreadMessages = true ;
contactDependencyObject . Contact . IsNewMessage = true ;
2023-02-15 18:43:15 +01:00
}
2021-07-14 19:33:50 +02:00
}
2019-05-14 13:37:20 +02:00
2023-03-03 21:23:48 +01:00
if ( ! updatedContacts . Contains ( contactDependencyObject . Contact ) )
{
_ContactList . ToList ( ) . Remove ( contactDependencyObject ) ;
}
2023-02-15 18:43:15 +01:00
}
2023-03-03 21:23:48 +01:00
ContactList . Sort ( ( x , y ) = > DateTime . Compare ( y . Contact . TimeStamp , x . Contact . TimeStamp ) ) ;
OnPropertyChanged ( nameof ( ChatMessages ) ) ;
OnPropertyChanged ( nameof ( ContactList ) ) ;
2021-07-14 19:33:50 +02:00
} ) ;
} ) ;
}
} ) ;
2017-09-12 14:32:34 +02:00
}
2020-06-17 19:36:52 +02:00
private void InitListeningThread ( Contact currentContact )
2017-09-12 14:32:34 +02:00
{
2020-06-17 19:36:52 +02:00
EmojisListeningThreadTimer = new Timer { Tag = currentContact } ;
EmojisListeningThreadTimer . Tick + = ListeningThreadTimerTickEvent ;
2020-08-06 16:05:23 +02:00
EmojisListeningThreadTimer . Interval = 10000 ;
2020-06-17 19:36:52 +02:00
EmojisListeningThreadTimer . Start ( ) ;
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
private void ListeningThreadTimerTickEvent ( object sender , EventArgs e )
{
2020-06-17 19:36:52 +02:00
ListenForMessagesForCurrentContact ( ( Contact ) ( ( Timer ) sender ) . Tag ) ;
2019-05-14 13:37:20 +02:00
}
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
private void ListenForMessagesForCurrentContact ( Contact pCurrentContact )
2019-05-14 13:37:20 +02:00
{
2022-07-28 19:18:15 +02:00
if ( ! ShouldInterruptContactsThread & & ! ( CurrentContact is null ) )
2019-05-14 13:37:20 +02:00
{
2023-07-24 18:04:56 +02:00
CheckForNewMessagesForContact ( pCurrentContact ) ;
}
}
private void CheckForNewMessagesForContact ( Contact contact )
{
Chat . CheckIfNewMessagesExistAsync ( CurrentContact . GroupId , hasNewMessages = >
{
if ( ! hasNewMessages )
2019-05-14 13:37:20 +02:00
{
2023-07-24 18:04:56 +02:00
return ;
}
2017-10-24 11:39:59 +02:00
2023-07-24 18:04:56 +02:00
Chat . LoadChatMessagesForContactAsync ( contact , GetFirstMessage ( ) , chatMessages = >
{
this . Dispatch ( ( ) = >
2021-07-14 19:33:50 +02:00
{
2023-07-24 18:04:56 +02:00
_ChatMessages . Clear ( ) ;
_ChatMessages . AddRangeIfElementsNotIn ( chatMessages ) ;
2017-09-12 14:32:34 +02:00
2023-07-24 18:04:56 +02:00
WpfUtils . ScrollToBottomOfListBox ( ChatListBox ) ;
2021-07-14 19:33:50 +02:00
} ) ;
} ) ;
2023-07-24 18:04:56 +02:00
} ) ;
2017-09-12 14:32:34 +02:00
}
private void Suche_OnTextChanged ( object sender , TextChangedEventArgs e )
{
CollectionViewSource . GetDefaultView ( Clientlist . ItemsSource ) . Refresh ( ) ;
}
private bool UserFilter ( object item )
{
2023-03-03 21:23:48 +01:00
if ( string . IsNullOrEmpty ( Suche . Text ) )
2020-06-17 19:36:52 +02:00
{
2017-09-12 14:32:34 +02:00
return true ;
2020-06-17 19:36:52 +02:00
}
2022-07-11 16:01:14 +02:00
if ( item is ContactDependencyObject contactDependencyObject )
{
return contactDependencyObject . Contact . Name . IndexOf ( Suche . Text , StringComparison . OrdinalIgnoreCase ) > = 0 ;
}
return false ;
2017-09-12 14:32:34 +02:00
}
2017-09-14 12:36:48 +02:00
private void Chat_OnMouseDoubleClick ( object sender , MouseButtonEventArgs e )
{
2023-03-03 21:23:48 +01:00
if ( ChatListBox . SelectedItems . Count < = 0 )
2017-10-20 07:34:26 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
}
2017-09-14 12:36:48 +02:00
2023-03-03 21:23:48 +01:00
var selectedItem = ChatListBox . SelectedItems [ 0 ] ;
2021-09-07 19:32:10 +02:00
2023-03-03 21:23:48 +01:00
if ( ! ( selectedItem is ChatMessage selectedMessage ) )
{
return ;
}
2022-07-28 19:18:15 +02:00
2023-03-03 21:23:48 +01:00
if ( ! string . IsNullOrEmpty ( selectedMessage . OriginalImage ) )
{
var uri = new Uri ( selectedMessage . OriginalImage ) ;
2021-09-07 19:32:10 +02:00
2023-03-03 21:23:48 +01:00
if ( uri . IsFile )
{
var bitmapImage = new BitmapImage ( uri ) ;
bitmapImage . Freeze ( ) ;
2021-09-07 19:32:10 +02:00
2023-03-03 21:23:48 +01:00
Chat . ShowPictureWindow ( bitmapImage , "Test" ) ;
2020-06-17 19:36:52 +02:00
2023-03-03 21:23:48 +01:00
return ;
}
StartWaitingImmediately ( ) ;
Chat . ShowPicture ( selectedMessage . OriginalImage , CurrentContact . GroupId , ( imageSource , windowTitle ) = >
{
this . Dispatch ( ( ) = >
2017-11-06 07:16:43 +01:00
{
2023-03-03 21:23:48 +01:00
EndWaiting ( ) ;
Chat . ShowPictureWindow ( imageSource , windowTitle ) ;
} ) ;
} ) ;
}
else if ( ! ( selectedMessage . FilePath is null ) )
{
StartWaitingImmediately ( ) ;
2017-11-06 07:16:43 +01:00
2023-03-03 21:23:48 +01:00
LoadDocumentAsync ( selectedMessage . FilePath , Path . GetFileName ( selectedMessage . FilePath ) , EndWaiting ) ;
2017-09-14 12:36:48 +02:00
}
}
2021-07-14 19:33:50 +02:00
private void LoadDocumentAsync ( string uri , string fileName , Action callback )
2017-12-18 18:22:13 +01:00
{
try
2017-09-14 12:36:48 +02:00
{
2021-07-14 19:33:50 +02:00
var path = Path . Combine ( Path . GetTempPath ( ) , fileName ) ;
2017-12-18 18:22:13 +01:00
2019-04-02 13:43:30 +02:00
_CreatedTempFiles . Add ( path ) ;
2021-07-14 19:33:50 +02:00
if ( ! File . Exists ( path ) )
2017-09-14 12:36:48 +02:00
{
2021-07-14 19:33:50 +02:00
Chat . DownloadFileAsync ( uri , path , ( ) = >
2017-12-18 18:22:13 +01:00
{
2021-07-14 19:33:50 +02:00
this . Dispatch ( ( ) = >
{
if ( File . Exists ( path ) )
{
Process . Start ( path ) ;
}
2017-09-14 12:36:48 +02:00
2021-07-14 19:33:50 +02:00
callback ? . Invoke ( ) ;
} ) ;
} ) ;
2017-12-18 18:22:13 +01:00
}
else
2017-09-14 12:36:48 +02:00
{
Process . Start ( path ) ;
2017-12-18 18:22:13 +01:00
2021-07-14 19:33:50 +02:00
callback ? . Invoke ( ) ;
}
2017-12-18 18:22:13 +01:00
}
2023-03-03 21:23:48 +01:00
finally
2017-12-18 18:22:13 +01:00
{
EndWaiting ( ) ;
2021-07-14 19:33:50 +02:00
2017-12-18 18:22:13 +01:00
}
2017-09-14 12:36:48 +02:00
}
2020-06-17 19:36:52 +02:00
private ChatControlWaitLayer _WaitLayer ;
2017-09-15 09:49:32 +02:00
2022-07-28 19:18:15 +02:00
public void StartWaitingImmediately ( string dialogText = null )
2017-09-15 09:49:32 +02:00
{
2023-03-03 21:23:48 +01:00
if ( ! ( _WaitLayer is null ) )
2017-09-15 09:49:32 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
2017-09-15 09:49:32 +02:00
}
2023-03-03 21:23:48 +01:00
_WaitLayer = new ChatControlWaitLayer ( dialogText ) ;
Grid . SetRowSpan ( _WaitLayer , 3 ) ;
RootGrid . Children . Add ( _WaitLayer ) ;
Panel . SetZIndex ( _WaitLayer , int . MaxValue ) ;
_WaitLayer . RefreshChatUI ( ) ;
2017-09-15 09:49:32 +02:00
}
public void EndWaiting ( )
{
Dispatcher . BeginInvoke (
2023-07-24 18:04:56 +02:00
DispatcherPriority . Normal ,
2020-06-17 19:36:52 +02:00
( Action ) delegate
2017-09-15 09:49:32 +02:00
{
2023-03-03 21:23:48 +01:00
if ( _WaitLayer is null )
2017-09-15 09:49:32 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
2017-09-15 09:49:32 +02:00
}
2023-03-03 21:23:48 +01:00
RootGrid . Children . Remove ( _WaitLayer ) ;
_WaitLayer = null ;
2017-09-15 09:49:32 +02:00
} ) ;
}
2017-10-06 12:08:05 +02:00
private void ButtonReloadGruppen_OnClick ( object sender , RoutedEventArgs e )
2017-10-24 11:39:59 +02:00
{
2017-10-06 12:08:05 +02:00
DoSynchro ( ) ;
}
private void DoSynchro ( )
{
try
{
2020-06-17 19:36:52 +02:00
StartWaitingImmediately ( ) ;
2017-10-06 12:08:05 +02:00
2019-05-14 13:37:20 +02:00
WriteToNewSyncFile ( ) ;
2017-10-24 11:39:59 +02:00
2021-07-14 19:33:50 +02:00
Chat . ReloadGroupsAsync ( data = >
2020-06-17 19:36:52 +02:00
{
this . Dispatch ( ( ) = >
{
2022-07-11 16:01:14 +02:00
ContactList = new ObservableSortCollection < ContactDependencyObject > ( ) ;
2022-07-28 19:18:15 +02:00
foreach ( var contact in Chat . UpdateContacts ( data ) )
2022-07-11 16:01:14 +02:00
{
ContactList . Add ( new ContactDependencyObject ( contact ) ) ;
}
2022-07-28 19:18:15 +02:00
2022-07-11 16:01:14 +02:00
ContactList . Sort ( ( x , y ) = > DateTime . Compare ( y . Contact . TimeStamp , x . Contact . TimeStamp ) ) ;
2022-07-28 19:18:15 +02:00
if ( ! ( CurrentContact is null ) )
{
var selectedElement = ContactList . FirstOrDefault ( f = > f . Contact . GroupId = = CurrentContact . GroupId ) ;
if ( ! ( selectedElement is null ) )
{
Clientlist . SelectedItem = selectedElement ;
}
}
2020-06-17 19:36:52 +02:00
OnPropertyChanged ( nameof ( ContactList ) ) ;
2017-10-18 10:09:20 +02:00
2020-06-17 19:36:52 +02:00
ReadNewSyncFile ( ) ;
2017-10-18 10:09:20 +02:00
2020-06-19 18:34:30 +02:00
AddViewFilterToClientlist ( ) ;
2020-06-17 19:36:52 +02:00
EndWaiting ( ) ;
} ) ;
} ) ;
2017-10-06 12:08:05 +02:00
}
2023-03-03 21:23:48 +01:00
finally
2017-10-06 12:08:05 +02:00
{
2020-06-17 19:36:52 +02:00
EndWaiting ( ) ;
2017-10-06 12:08:05 +02:00
}
}
2017-12-11 14:32:56 +01:00
2019-05-14 13:37:20 +02:00
// Speichert die TimeStamps der zuletzt empfangenen Nachrichten
public void WriteToNewSyncFile ( )
2017-10-18 14:32:00 +02:00
{
2019-05-14 13:37:20 +02:00
try
2017-10-18 14:32:00 +02:00
{
2023-03-03 21:23:48 +01:00
if ( File . Exists ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) ) )
2019-05-14 13:37:20 +02:00
{
File . SetAttributes ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) , FileAttributes . Normal ) ;
File . Delete ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) ) ;
}
using ( var streamWriter = new StreamWriter ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) ) )
{
Encoding enc = new UTF8Encoding ( ) ;
2022-07-28 19:18:15 +02:00
foreach ( var contact in _ContactList . ToList ( ) . Where ( contact = > ! ( contact . Contact . ReceivedMessage is null ) ) )
2019-05-14 13:37:20 +02:00
{
2022-07-11 16:01:14 +02:00
var text = contact . Contact . GroupId + ";" + contact . Contact . TimeStamp . ToUniversalTime ( ) + ";" + contact . Contact . IsNewMessage + ";" ;
2019-05-14 13:37:20 +02:00
var bytes = enc . GetBytes ( text ) ;
var base64String = Convert . ToBase64String ( bytes ) ;
streamWriter . WriteLine ( base64String ) ;
}
File . SetAttributes ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) , FileAttributes . ReadOnly ) ;
2017-10-18 14:32:00 +02:00
}
}
2022-07-11 16:01:14 +02:00
catch ( Exception )
2019-05-14 13:37:20 +02:00
{
2017-10-18 14:32:00 +02:00
2019-05-14 13:37:20 +02:00
}
2017-10-18 14:32:00 +02:00
}
2019-05-14 13:37:20 +02:00
public void ReadNewSyncFile ( )
2017-10-18 14:32:00 +02:00
{
2019-05-14 13:37:20 +02:00
try
2017-10-18 14:32:00 +02:00
{
2019-05-14 13:37:20 +02:00
if ( File . Exists ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) ) )
{
using ( var streamReader = new StreamReader ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) , true ) )
{
string line ;
2017-10-18 14:32:00 +02:00
2019-05-14 13:37:20 +02:00
_GroupId2DateTime . Clear ( ) ;
2017-10-18 14:32:00 +02:00
2022-07-28 19:18:15 +02:00
while ( ! ( ( line = streamReader . ReadLine ( ) ) is null ) )
2019-05-14 13:37:20 +02:00
{
var encodedTextBytes = Convert . FromBase64String ( line ) ;
var plainText = Encoding . UTF8 . GetString ( encodedTextBytes ) ;
var splitPlainText = plainText . Split ( ';' ) ;
// Ist kein Datum vorhanden -> 01.01.0001 00:00:00
var groupId = int . Parse ( splitPlainText [ 0 ] ) ;
var dateTime = DateTime . Parse ( splitPlainText [ 1 ] ) ;
var isUnread = splitPlainText . Length < = 2 | | string . IsNullOrEmpty ( splitPlainText [ 2 ] ) | | bool . Parse ( splitPlainText [ 2 ] ) ;
_GroupId2DateTime . Add ( groupId , new SyncFileInfoStruct ( isUnread , dateTime ) ) ;
}
}
var latestDateTime = Utils . DefaultDate ;
foreach ( var id2dateTime in _GroupId2DateTime )
2017-10-18 14:32:00 +02:00
{
2019-05-14 13:37:20 +02:00
if ( id2dateTime . Value . TimeStamp > latestDateTime )
2017-10-18 14:32:00 +02:00
{
2019-05-14 13:37:20 +02:00
latestDateTime = id2dateTime . Value . TimeStamp ;
2017-10-18 14:32:00 +02:00
}
2019-05-14 13:37:20 +02:00
}
2019-05-15 14:57:16 +02:00
2021-07-14 19:33:50 +02:00
Chat . LastTimeStamp = latestDateTime . GetUnixTimeStamp ( ) ;
2017-10-18 14:32:00 +02:00
}
2019-05-14 13:37:20 +02:00
}
2022-07-11 16:01:14 +02:00
catch ( Exception )
2019-05-14 13:37:20 +02:00
{
}
2017-11-09 12:20:03 +01:00
}
2020-06-17 19:36:52 +02:00
// Wird im BeWoPlaner benutzt
2019-05-14 13:37:20 +02:00
public List < Contact > GetContactsWithoutUnreadMessages ( )
2017-11-09 12:20:03 +01:00
{
2019-05-14 13:37:20 +02:00
var contacts = new List < Contact > ( ) ;
2017-11-07 11:56:29 +01:00
2017-11-09 12:20:03 +01:00
Clientlist . Items . Refresh ( ) ;
2017-11-07 11:56:29 +01:00
2019-05-14 13:37:20 +02:00
foreach ( var contactItem in Clientlist . Items )
2017-11-09 12:20:03 +01:00
{
2022-07-11 16:01:14 +02:00
if ( contactItem is ContactDependencyObject contactDependencyObject & & contactDependencyObject . Contact . HasUnreadMessages & & ! string . IsNullOrEmpty ( contactDependencyObject . Contact . ReceivedMessage ? . Text ) )
2017-11-09 12:20:03 +01:00
{
2022-07-11 16:01:14 +02:00
contacts . Add ( contactDependencyObject . Contact ) ;
2017-11-09 12:20:03 +01:00
}
}
2019-05-14 13:37:20 +02:00
return contacts ;
2017-10-18 14:32:00 +02:00
}
2017-11-09 12:20:03 +01:00
2020-06-17 19:36:52 +02:00
// Wird im BeWoPlaner benutzt
2019-05-14 13:37:20 +02:00
public void ResetNumberOfUnreadMessages ( Dictionary < long , DateTime > pGroupId2DateTime )
2017-11-09 12:20:03 +01:00
{
2023-03-03 21:23:48 +01:00
if ( pGroupId2DateTime is null )
2017-11-09 12:20:03 +01:00
{
2023-03-03 21:23:48 +01:00
return ;
}
foreach ( var contactDependencyObject in _ContactList )
{
foreach ( var groupId2DateTime in pGroupId2DateTime )
2017-11-09 12:20:03 +01:00
{
2023-03-03 21:23:48 +01:00
if ( contactDependencyObject . Contact . GroupId = = groupId2DateTime . Key )
2017-11-09 12:20:03 +01:00
{
2023-03-03 21:23:48 +01:00
contactDependencyObject . Contact . HasUnreadMessages = false ;
2017-11-09 12:20:03 +01:00
}
}
2019-05-14 13:37:20 +02:00
}
}
2020-06-17 19:36:52 +02:00
// Wird im BeWoPlaner benutzt
2019-05-14 13:37:20 +02:00
public List < ChatMessage > GetSelectedMessages ( )
{
var messages = ( from object selectedItem in ChatListBox . SelectedItems select selectedItem as ChatMessage ) . ToList ( ) ;
return messages . Count > 0 ? messages : null ;
}
2020-06-17 19:36:52 +02:00
// Wird im BeWoPlaner benutzt
2021-02-25 12:33:00 +01:00
public Contact GetCurrentContactWithUserIdManage ( )
2019-05-14 13:37:20 +02:00
{
2022-07-28 19:18:15 +02:00
return ! ( CurrentContact ? . UserIdManage is null ) ? CurrentContact : null ;
2019-05-14 13:37:20 +02:00
}
2021-02-25 12:33:00 +01:00
// Wird im BeWoPlaner benutzt
public Contact GetCurrentContact ( )
{
return CurrentContact ;
}
2020-06-17 19:36:52 +02:00
// Wird im BeWoPlaner benutzt
2019-05-14 13:37:20 +02:00
public void AddContextMenu ( string menuItemText , Action < string > callBackAction )
{
if ( ! _MenuItemName2Callback . ContainsKey ( menuItemText ) )
{
_MenuItemName2Callback . Add ( menuItemText , callBackAction ) ;
}
2017-11-09 12:20:03 +01:00
}
2017-12-18 18:22:13 +01:00
2020-06-17 19:36:52 +02:00
public void DeleteTempFiles ( )
2017-12-18 18:22:13 +01:00
{
2023-03-03 21:23:48 +01:00
foreach ( var file in _CreatedTempFiles )
2017-12-18 18:22:13 +01:00
{
try
{
2023-03-03 21:23:48 +01:00
if ( File . Exists ( file ) )
2019-04-04 13:00:10 +02:00
{
2017-12-18 18:22:13 +01:00
File . Delete ( file ) ;
2019-04-04 13:00:10 +02:00
}
2017-12-18 18:22:13 +01:00
}
2023-03-03 21:23:48 +01:00
catch ( Exception )
2017-12-18 18:22:13 +01:00
{
2022-07-11 16:01:14 +02:00
//ignore okeee
2017-12-18 18:22:13 +01:00
}
}
}
2020-06-17 19:36:52 +02:00
public event PropertyChangedEventHandler PropertyChanged ;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
{
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
}
private void ChatMainControl_OnLoaded ( object sender , RoutedEventArgs e )
2020-06-19 18:34:30 +02:00
{
AddViewFilterToClientlist ( ) ;
}
private void AddViewFilterToClientlist ( )
2020-06-17 19:36:52 +02:00
{
var view = ( CollectionView ) CollectionViewSource . GetDefaultView ( Clientlist . ItemsSource ) ;
view . Filter = UserFilter ;
}
2020-08-06 16:05:23 +02:00
private void Chatbox_OnTextChanged ( object sender , TextChangedEventArgs e )
{
if ( string . IsNullOrWhiteSpace ( Chatbox . Text ) )
{
Chatbox . Height = 25 ;
}
2022-07-11 16:01:14 +02:00
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
OnPropertyChanged ( nameof ( IsSendButtonEnabled ) ) ;
2020-08-06 16:05:23 +02:00
}
2021-07-14 19:33:50 +02:00
public void AddMessages ( List < ChatMessage > newMessages )
{
_ChatMessages . AddRangeIfElementsNotIn ( newMessages ) ;
}
public ChatMessage GetFirstMessage ( )
{
return _ChatMessages . FirstOrDefault ( ) ;
}
2022-07-11 16:01:14 +02:00
2022-07-28 19:18:15 +02:00
#region Stapelnachricht
2022-07-11 16:01:14 +02:00
2022-07-28 19:18:15 +02:00
public ImageSource BroadcastButtonImageSource { get ; }
public ImageSource InvertedBroadcastButtonImageSource { get ; }
private Visibility _ProgressOverlayVisibility ;
public Visibility ProgressOverlayVisibility
{
get = > _ProgressOverlayVisibility ;
set
{
_ProgressOverlayVisibility = value ;
OnPropertyChanged ( nameof ( ProgressOverlayVisibility ) ) ;
OnPropertyChanged ( nameof ( NormalViewVisibility ) ) ;
OnPropertyChanged ( nameof ( StapelmodusChatGridVisibility ) ) ;
OnPropertyChanged ( nameof ( ChatListBoxVisibility ) ) ;
}
}
public Visibility NormalViewVisibility = > ProgressOverlayVisibility = = Visibility . Visible ? Visibility . Collapsed : Visibility . Visible ;
public Visibility StapelmodusChatGridVisibility = > ( Chat ? . IsInBroadcastMode ? ? false ) & & ProgressOverlayVisibility = = Visibility . Collapsed ? Visibility . Visible : Visibility . Collapsed ;
public bool IsSendButtonEnabled = > ( ! string . IsNullOrEmpty ( Chatbox . Text ) | | File . Exists ( ChatMessageFileWrapper ? . ScaledFilePath ) ) & & ! ( CurrentContact is null ) ;
2022-07-11 16:01:14 +02:00
public bool IsBroadcastButtonEnabled
{
get
{
2022-07-28 19:18:15 +02:00
var isInBroadcastMode = Chat ? . IsInBroadcastMode ? ? false ;
2022-07-11 16:01:14 +02:00
var hasSelectedContacts = GetSelectedContacts ( ) . Any ( ) ;
2022-07-28 19:18:15 +02:00
var hasTextOrFile = ! string . IsNullOrEmpty ( Chatbox . Text ) | | File . Exists ( BroadcastMessageFileWrapper ? . ScaledFilePath ) ;
2022-07-11 16:01:14 +02:00
return isInBroadcastMode & & hasSelectedContacts & & hasTextOrFile ;
}
}
private Contact _PreviousContact ;
private CancellationTokenSource _CancellationTokenSource ;
2023-03-03 21:23:48 +01:00
private const string DefaultBroadcastInfoText = "Sie befinden Sich im Stapel-Modus. In diesem Modus können Sie eine Nachricht in einem Schritt an mehrere Empfänger & Gruppen senden. Bitte setzen Sie dazu in der Kontaktliste bei den gewünschten Empfängern & Gruppen ein Häkchen und schreiben Sie Ihre Nachricht wie gewohnt. Wenn Sie auf senden klicken, wird die Nachricht an alle ausgewählten Empfänger & Gruppen gesendet." ;
2022-07-11 16:01:14 +02:00
private string _BroadcastInfoText ;
public string BroadcastInfoText
{
2023-03-03 21:23:48 +01:00
get = > _BroadcastInfoText ? ? ( _BroadcastInfoText = DefaultBroadcastInfoText ) ;
2022-07-11 16:01:14 +02:00
set
{
2023-03-03 21:23:48 +01:00
_BroadcastInfoText = $"{DefaultBroadcastInfoText}{Environment.NewLine}{value}" ;
2022-07-11 16:01:14 +02:00
OnPropertyChanged ( BroadcastInfoText ) ;
}
}
private double _BroadcastProgressValue ;
public double BroadcastProgressValue
{
get = > _BroadcastProgressValue ;
set
{
_BroadcastProgressValue = value ;
OnPropertyChanged ( nameof ( BroadcastProgressValue ) ) ;
}
}
2022-07-28 19:18:15 +02:00
public Visibility RemoveFileFromBroadcastMessageVisibility = > BroadcastMessageFileWrapper is null ? Visibility . Collapsed : Visibility ;
private ChatMessageFileWrapper _BroadcastMessageFileWrapper ;
public ChatMessageFileWrapper BroadcastMessageFileWrapper
2022-07-11 16:01:14 +02:00
{
2022-07-28 19:18:15 +02:00
get = > _BroadcastMessageFileWrapper ;
2022-07-11 16:01:14 +02:00
set
{
2022-07-28 19:18:15 +02:00
_BroadcastMessageFileWrapper = value ;
2022-07-11 16:01:14 +02:00
2022-07-28 19:18:15 +02:00
OnPropertyChanged ( nameof ( BroadcastMessageFileWrapper ) ) ;
2022-07-11 16:01:14 +02:00
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
2022-07-28 19:18:15 +02:00
OnPropertyChanged ( nameof ( RemoveFileFromBroadcastMessageVisibility ) ) ;
2022-07-11 16:01:14 +02:00
}
}
2022-07-28 19:18:15 +02:00
private void BroadcastToggleButton_OnClick ( object sender , RoutedEventArgs e )
2022-07-11 16:01:14 +02:00
{
Chat . IsInBroadcastMode = ! Chat . IsInBroadcastMode ;
BroadcastProgressValue = 0 ;
DeselectAllContacts ( ) ;
2023-03-03 21:23:48 +01:00
if ( Chat . IsInBroadcastMode )
2022-07-11 16:01:14 +02:00
{
_CancellationTokenSource = new CancellationTokenSource ( ) ;
_PreviousContact = CurrentContact ;
CurrentContact = null ;
2022-07-28 19:18:15 +02:00
ToggleButtonImage . Source = InvertedBroadcastButtonImageSource ;
2022-07-11 16:01:14 +02:00
}
else
{
CloseBroadcastMode ( ) ;
}
2022-07-28 19:18:15 +02:00
OnPropertyChanged ( nameof ( StapelmodusChatGridVisibility ) ) ;
OnPropertyChanged ( nameof ( ChatListBoxVisibility ) ) ;
2022-07-11 16:01:14 +02:00
OnPropertyChanged ( nameof ( IsSendButtonEnabled ) ) ;
OnPropertyChanged ( nameof ( ChatMessageInputGridVisibility ) ) ;
2022-07-28 19:18:15 +02:00
OnPropertyChanged ( nameof ( BroadcastMessageFileWrapper ) ) ;
2022-07-11 16:01:14 +02:00
}
private async void SendBroadcastMessage ( )
{
var selectedContacts = GetSelectedContacts ( ) ;
2022-07-28 19:18:15 +02:00
if ( selectedContacts . Count = = 0 | | string . IsNullOrEmpty ( Chatbox . Text ) & & BroadcastMessageFileWrapper . ChatMessageImageSource is null )
{
return ;
}
var result = new OwnChatMessageBox ( "JA" , "ABBRECHEN" , $"Sind Sie sicher? Ihre Nachricht wird an {selectedContacts.Count} Empfänger & Gruppen verschickt!" , "Stapelnachricht" ) { Owner = ContainingWindow } . ShowDialog ( ) ;
if ( result ! = true )
2022-07-11 16:01:14 +02:00
{
return ;
}
2022-07-28 19:18:15 +02:00
var wasCompleted = false ;
2022-07-11 16:01:14 +02:00
var progress = new Progress < BroadcastProgressReportModel > ( ) ;
progress . ProgressChanged + = ReportProgress ;
try
{
2022-07-28 19:18:15 +02:00
ProgressOverlayVisibility = Visibility . Visible ;
wasCompleted = await SendBroadcast ( progress , _CancellationTokenSource . Token ) ;
2022-07-11 16:01:14 +02:00
}
catch ( OperationCanceledException )
{
BroadcastInfoText = "Der Broadcast wurde abgebrochen." ;
}
2022-07-28 19:18:15 +02:00
finally
{
ProgressOverlayVisibility = Visibility . Collapsed ;
}
2022-07-11 16:01:14 +02:00
DoSynchro ( ) ;
2022-07-28 19:18:15 +02:00
if ( wasCompleted )
{
new OwnChatMessageBox ( null , "OK" , $"Die Nachricht wurde erfolgreich an {selectedContacts.Count} Empfänger & Gruppen versandt." , "Stapelnachricht" ) { Owner = ContainingWindow } . ShowDialog ( ) ;
}
2022-07-11 16:01:14 +02:00
CloseBroadcastMode ( ) ;
}
2022-07-28 19:18:15 +02:00
private async Task < bool > SendBroadcast ( IProgress < BroadcastProgressReportModel > progress , CancellationToken cancellationToken )
2022-07-11 16:01:14 +02:00
{
var count = 0 ;
var report = new BroadcastProgressReportModel ( ) ;
var selectedContacts = GetSelectedContacts ( ) ;
2022-07-28 19:18:15 +02:00
foreach ( var contactDependencyObject in selectedContacts . Where ( selectedContact = > selectedContact . Contact . IsChatMessageInputGridVisible ) )
2022-07-11 16:01:14 +02:00
{
2022-07-28 19:18:15 +02:00
if ( BroadcastMessageFileWrapper is null )
2022-07-11 16:01:14 +02:00
{
2022-07-28 19:18:15 +02:00
await Chat . SendMessage ( Chatbox . Text , contactDependencyObject . Contact . GroupId ) ;
2022-07-11 16:01:14 +02:00
}
else
{
2022-07-28 19:18:15 +02:00
await Chat . SendFileToContact ( contactDependencyObject . Contact , BroadcastMessageFileWrapper . ScaledFilePath , BroadcastMessageFileWrapper . OriginalFilePath , Chatbox . Text ) ;
2022-07-11 16:01:14 +02:00
}
2022-07-28 19:18:15 +02:00
contactDependencyObject . SetValue ( ListItemHelper . IsCheckedProperty , false ) ;
2022-07-11 16:01:14 +02:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2022-07-28 19:18:15 +02:00
2022-07-11 16:01:14 +02:00
count + + ;
report . PercentageComplete = count * 100 / selectedContacts . Count ;
progress . Report ( report ) ;
}
2022-07-28 19:18:15 +02:00
return count = = selectedContacts . Count ;
2022-07-11 16:01:14 +02:00
}
private void ReportProgress ( object sender , BroadcastProgressReportModel e )
{
BroadcastProgressValue = e . PercentageComplete ;
}
private void SelectAllContacts_OnClick ( object sender , RoutedEventArgs e )
{
2023-03-03 21:23:48 +01:00
if ( ! ( sender is CheckBox checkBox ) )
2022-07-11 16:01:14 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
}
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
var isChecked = checkBox . IsChecked ? ? false ;
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
foreach ( var contactDependencyObject in ContactList )
{
contactDependencyObject . SetValue ( ListItemHelper . IsCheckedProperty , isChecked ) ;
2022-07-11 16:01:14 +02:00
}
2023-03-03 21:23:48 +01:00
OnPropertyChanged ( nameof ( ContactList ) ) ;
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
2022-07-11 16:01:14 +02:00
}
private void SendBroatcastButton_OnClick ( object sender , RoutedEventArgs e )
{
SendBroadcastMessage ( ) ;
}
private void CancelBroadcastButton_OnClick ( object sender , RoutedEventArgs e )
{
_CancellationTokenSource ? . Cancel ( ) ;
2022-07-28 19:18:15 +02:00
BroadcastMessageFileWrapper = null ;
2022-07-11 16:01:14 +02:00
Chatbox . Text = string . Empty ;
}
private void AddMediaFileToBroadcastMessage_OnClick ( object sender , RoutedEventArgs e )
{
2023-03-03 21:23:48 +01:00
if ( ! Chat . IsInBroadcastMode )
2022-07-11 16:01:14 +02:00
{
2023-03-03 21:23:48 +01:00
return ;
}
var originalFilePath = Chat . OpenFile ( ) ;
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
if ( ! File . Exists ( originalFilePath ) )
{
return ;
}
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
StartWaitingImmediately ( "Skaliere Bild ..." ) ;
2022-07-11 16:01:14 +02:00
2023-03-03 21:23:48 +01:00
var scalingTask = new Task ( ( ) = >
{
try
{
var scaledImagePath = FileUtils . ScaleImage ( originalFilePath , Path . GetExtension ( originalFilePath . ToUpperInvariant ( ) ) , Chat . ChatDaten . MaxUploadSize ) ;
2022-07-28 19:18:15 +02:00
2023-03-03 21:23:48 +01:00
var isFileSizeTooLarge = FileUtils . CheckFileSize ( scaledImagePath , Chat . ChatDaten . MaxUploadSize ) ;
2022-07-28 19:18:15 +02:00
2023-03-03 21:23:48 +01:00
if ( isFileSizeTooLarge )
{
MessageBox . Show ( $"Die ausgewählte Datei ist zu groß. Die maximale Größe beträgt {Chat.ChatDaten.MaxUploadSize / 1000 / 1000} MB" , "Senden nicht möglich" , MessageBoxButton . OK , MessageBoxImage . Warning ) ;
return ;
}
2022-07-28 19:18:15 +02:00
2023-03-03 21:23:48 +01:00
if ( File . Exists ( scaledImagePath ) )
{
BroadcastMessageFileWrapper = new ChatMessageFileWrapper ( originalFilePath , scaledImagePath , $"{Chat.ChatDaten.ServerUrl}/document.png" ) ;
}
2022-07-11 16:01:14 +02:00
}
2023-03-03 21:23:48 +01:00
finally
{
this . Dispatch ( EndWaiting ) ;
}
} ) ;
scalingTask . Start ( ) ;
2022-07-11 16:01:14 +02:00
}
private void CloseBroadcastMode ( )
{
Chatbox . Text = string . Empty ;
2022-07-28 19:18:15 +02:00
BroadcastToggleButton . IsChecked = false ;
Chat . IsInBroadcastMode = false ;
2022-07-11 16:01:14 +02:00
2022-07-28 19:18:15 +02:00
_CancellationTokenSource = null ;
BroadcastMessageFileWrapper = null ;
BroadcastProgressValue = 0 ;
ProgressOverlayVisibility = Visibility . Collapsed ;
2022-07-11 16:01:14 +02:00
DeselectAllContacts ( ) ;
OnPropertyChanged ( nameof ( IsSendButtonEnabled ) ) ;
OnPropertyChanged ( nameof ( ChatMessageInputGridVisibility ) ) ;
2022-07-28 19:18:15 +02:00
OnPropertyChanged ( nameof ( BroadcastMessageFileWrapper ) ) ;
ToggleButtonImage . Source = BroadcastButtonImageSource ;
OnPropertyChanged ( nameof ( StapelmodusChatGridVisibility ) ) ;
2022-07-11 16:01:14 +02:00
2022-07-28 19:18:15 +02:00
if ( ! ( _PreviousContact is null ) )
{
SelectContact ( _PreviousContact ) ;
}
2022-07-11 16:01:14 +02:00
}
private void ContactCheckBox_OnClick ( object sender , RoutedEventArgs e )
{
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
}
private void ContactCheckBox_OnChecked ( object sender , RoutedEventArgs e )
{
UpdateContactSelection ( ) ;
}
private void UpdateContactSelection ( )
{
OnPropertyChanged ( nameof ( ContactList ) ) ;
OnPropertyChanged ( nameof ( IsBroadcastButtonEnabled ) ) ;
2022-07-28 19:18:15 +02:00
bool? newValue = null ;
var selectedContacts = GetSelectedContacts ( ) ;
var allSelectableContacts = ContactList . Where ( contactDependencyObject = > contactDependencyObject . Contact . IsChatMessageInputGridVisible ) . ToList ( ) ;
if ( selectedContacts . Count = = allSelectableContacts . Count )
{
newValue = true ;
}
if ( selectedContacts . Count = = 0 )
{
newValue = false ;
}
SelectAllContactsCheckBox . IsChecked = newValue ;
2022-07-11 16:01:14 +02:00
}
2022-07-28 19:18:15 +02:00
private List < ContactDependencyObject > GetSelectedContacts ( )
2022-07-11 16:01:14 +02:00
{
2022-07-28 19:18:15 +02:00
var result = new List < ContactDependencyObject > ( ) ;
2022-07-11 16:01:14 +02:00
foreach ( var contactDependencyObject in ContactList . Where ( contactDependencyObject = > contactDependencyObject . Contact . IsChatMessageInputGridVisible ) )
{
if ( contactDependencyObject . GetValue ( ListItemHelper . IsCheckedProperty ) is bool isChecked & & isChecked )
{
2022-07-28 19:18:15 +02:00
result . AddIfNotIn ( contactDependencyObject ) ;
2022-07-11 16:01:14 +02:00
}
}
return result ;
}
private void DeselectAllContacts ( )
{
2023-03-03 21:23:48 +01:00
foreach ( var contactDependencyObject in ContactList )
2022-07-11 16:01:14 +02:00
{
contactDependencyObject . SetValue ( ListItemHelper . IsCheckedProperty , false ) ;
}
}
2022-07-28 19:18:15 +02:00
private void RemoveBroadcastFileAttachmentButton_Click ( object sender , RoutedEventArgs e )
{
BroadcastMessageFileWrapper = null ;
}
#endregion
private void RemoveFileAttachmentButton_Click ( object sender , RoutedEventArgs e )
{
ChatMessageFileWrapper = null ;
}
2017-09-12 14:32:34 +02:00
}
}