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 ;
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 ;
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
{
if ( ! Equals ( _ThreadExceptionMessage , value ) )
{
_ThreadExceptionMessage = value ;
OnPropertyChanged ( nameof ( ThreadExceptionMessage ) ) ;
OnPropertyChanged ( nameof ( CurrentContactInformationString ) ) ;
OnPropertyChanged ( nameof ( CurrencContactInfoForeground ) ) ;
OnPropertyChanged ( nameof ( ThreadExceptionImageSource ) ) ;
}
}
}
private readonly ImageSource _ThreadExceptionImageSource ;
public ImageSource ThreadExceptionImageSource = > ThreadExceptionMessage ! = null ? _ThreadExceptionImageSource : CurrentContact ? . Image ;
public SolidColorBrush CurrencContactInfoForeground = > ThreadExceptionMessage ! = null ? new SolidColorBrush ( Color . FromRgb ( 185 , 65 , 0 ) ) : CurrentContact ? . AccentColorBrush ? ? new SolidColorBrush ( Colors . Transparent ) ;
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
2019-09-02 19:04:55 +02:00
if ( value ! = null )
{
_ContainingWindow . Deactivated + = ContainingWindowOnDeactivated ;
_ContainingWindow . Activated + = ContainingWindowOnActivated ;
}
}
}
2019-05-14 13:37:20 +02:00
2021-07-14 19:33:50 +02:00
public Chat Chat { get ; set ; }
2020-06-17 19:36:52 +02:00
private Contact _CurrentContact ;
public Contact CurrentContact
{
get = > _CurrentContact ;
set
{
if ( ! Equals ( _CurrentContact , value ) )
{
_CurrentContact = value ;
OnPropertyChanged ( nameof ( CurrentContact ) ) ;
OnPropertyChanged ( nameof ( ChatMessageInputGridVisibility ) ) ;
2021-07-14 19:33:50 +02:00
OnPropertyChanged ( nameof ( Chat ) ) ;
2020-08-06 16:05:23 +02:00
OnPropertyChanged ( nameof ( CurrentContactInformationString ) ) ;
OnPropertyChanged ( nameof ( CurrencContactInfoForeground ) ) ;
OnPropertyChanged ( nameof ( ThreadExceptionImageSource ) ) ;
2020-06-17 19:36:52 +02:00
}
}
}
public ObservableCollection < Contact > ContactList
{
get
{
return _ContactList = = null ? new ObservableCollection < Contact > ( ) : new ObservableCollection < Contact > ( _ContactList . OrderByDescending ( contact = > contact . TimeStamp ) ) ;
}
}
public Visibility ChatMessageInputGridVisibility
{
get
{
2022-05-27 11:50:37 +02:00
if ( _CurrentContact is null )
2020-06-17 19:36:52 +02:00
{
return Visibility . Collapsed ;
}
return _CurrentContact . IsChatMessageInputGridVisible ? Visibility . Visible : Visibility . Collapsed ;
}
}
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
private List < Contact > _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 ( ) ;
2020-08-06 16:05:23 +02:00
_ThreadExceptionImageSource = new BitmapImage ( new Uri ( "pack://application:,,,/ChatController;component/Ressourcen/warning-exclamation-mark.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-05-27 11:50:37 +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 ;
2017-12-11 14:32:56 +01:00
ReloadGruppen . Visibility = Visibility . Collapsed ;
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 ) ) ;
_ContactList = Chat . AddContacts ( ) ;
2020-06-17 19:36:52 +02:00
OnPropertyChanged ( nameof ( ContactList ) ) ;
2019-05-14 13:37:20 +02:00
ReadNewSyncFile ( ) ;
foreach ( var contact in _ContactList )
{
if ( _GroupId2DateTime . ContainsKey ( contact . GroupId ) )
{
var isUnread = _GroupId2DateTime [ contact . GroupId ] . IsUnread ;
2019-05-15 14:57:16 +02:00
contact . IsNewMessage = isUnread ;
2019-05-14 13:37:20 +02:00
contact . HasUnreadMessages = contact . IsNewMessage ;
}
}
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
2019-09-03 15:15:12 +02:00
var contact = _ContactList . FirstOrDefault ( a = > a . GroupId = = pGroupId ) ;
2019-05-14 13:37:20 +02:00
2019-09-03 15:15:12 +02:00
if ( contact ! = null )
{
notificationIcon = Utils . ImageSourceToIcon ( contact . Image ) ;
}
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
2021-02-25 12:33:00 +01:00
if ( _ContainingWindow = = null )
{
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
2021-02-25 12:33:00 +01:00
var selectedContact = _ContactList ? . FirstOrDefault ( f = > f ? . GroupId = = pGroupId ) ;
if ( selectedContact ! = null )
{
SelectContact ( selectedContact ) ;
}
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 ) = >
{
2020-09-15 13:42:46 +02:00
// TODO: 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
}
2019-04-02 13:43:30 +02:00
public void ResetStyle ( )
2017-10-06 12:08:05 +02:00
{
EmojiButton . Style = null ;
MediaButton . Style = null ;
ReloadGruppen . Visibility = Visibility . Visible ;
2019-04-02 13:43:30 +02:00
_IsDesktopVersion = false ;
2017-10-06 12:08:05 +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 )
{
2021-07-14 19:33:50 +02:00
if ( Clientlist ? . SelectedItem is Contact selectedContact )
2017-09-15 09:49:32 +02:00
{
2021-02-25 12:33:00 +01:00
SelectContact ( selectedContact ) ;
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 SelectContact ( Contact pContact )
{
2021-11-18 12:33:13 +01:00
try
2019-05-15 14:57:16 +02:00
{
2021-11-18 12:33:13 +01:00
if ( pContact = = null )
{
return ;
}
2019-05-15 14:57:16 +02:00
2021-11-18 12:33:13 +01:00
ShouldInterruptContactsThread = true ;
2017-09-12 14:32:34 +02:00
2021-11-18 12:33:13 +01:00
StartWaitingImmediately ( ) ;
2021-07-14 19:33:50 +02:00
2021-11-18 12:33:13 +01:00
_ScrollPrueferAktivieren = false ;
2017-09-12 14:32:34 +02:00
2021-11-18 12:33:13 +01:00
if ( pContact . HasUnreadMessages )
{
pContact . HasUnreadMessages = false ;
2017-09-12 14:32:34 +02:00
2021-11-18 12:33:13 +01:00
Clientlist . Items . Refresh ( ) ;
2022-05-27 11:50:37 +02:00
OnPropertyChanged ( nameof ( ContactList ) ) ;
2021-11-18 12:33:13 +01:00
}
2017-09-12 14:32:34 +02:00
2021-11-18 12:33:13 +01:00
var currentContact = pContact ;
pContact . IsNewMessage = false ;
2017-09-12 14:32:34 +02:00
2021-11-18 12:33:13 +01:00
var shouldChangeIcon = _ContactList . Any ( contact = > contact . IsNewMessage ) ;
if ( shouldChangeIcon )
{
ContainingWindow . Icon = Utils . ConvertIconToImageSource ( Resource . ownchat_favicon ) ;
}
2017-09-12 14:32:34 +02:00
2021-11-18 12:33:13 +01:00
CurrentContact = currentContact ;
2019-09-02 19:04:55 +02:00
2021-11-18 12:33:13 +01:00
if ( Clientlist . Items . Contains ( pContact ) )
{
Clientlist . SelectedItem = pContact ;
}
2019-09-02 19:04:55 +02:00
2021-11-18 12:33:13 +01:00
_ChatMessages . Clear ( ) ;
2022-05-27 11:50:37 +02:00
OnPropertyChanged ( nameof ( _ChatMessages ) ) ;
2017-12-18 18:22:13 +01:00
2021-11-18 12:33:13 +01:00
Chat . LoadChatMessagesForContactAsync ( currentContact , GetFirstMessage ( ) , chatMessages = >
2021-07-14 19:33:50 +02:00
{
2021-11-18 12:33:13 +01:00
this . Dispatch ( ( ) = >
{
2022-05-27 11:50:37 +02:00
_ChatMessages . Clear ( ) ;
2021-11-18 12:33:13 +01:00
_ChatMessages . AddRangeIfElementsNotIn ( chatMessages ) ;
2022-05-27 11:50:37 +02:00
OnPropertyChanged ( nameof ( _ChatMessages ) ) ;
2021-07-14 19:33:50 +02:00
2021-11-18 12:33:13 +01:00
WpfUtils . ScrollToBottomOfListBox ( ChatListBox ) ;
2017-12-18 18:22:13 +01:00
2021-11-18 12:33:13 +01:00
ChatListBox . ContextMenu = Chat . ErstelleKontextMenue ( ) ;
SetContextHandler ( ) ;
_ScrollPrueferAktivieren = true ;
2021-07-14 19:33:50 +02:00
2021-11-18 12:33:13 +01:00
ListenForMessages ( ) ;
Cursor = Cursors . Arrow ;
2017-11-10 11:41:09 +01:00
2021-11-18 12:33:13 +01:00
WriteToNewSyncFile ( ) ;
2017-12-18 18:22:13 +01:00
2021-11-18 12:33:13 +01:00
ShouldInterruptContactsThread = false ;
2019-05-14 13:37:20 +02:00
2021-11-18 12:33:13 +01:00
EndWaiting ( ) ;
} ) ;
2021-07-14 19:33:50 +02:00
} ) ;
2021-11-18 12:33:13 +01:00
}
catch ( Exception exception )
{
#if DEBUG
var desktop = Environment . GetFolderPath ( Environment . SpecialFolder . DesktopDirectory ) ;
using ( var fileStream = new FileStream ( Path . Combine ( desktop , "ownchat_log.txt" ) , FileMode . OpenOrCreate ) )
{
using ( var streamWriter = new StreamWriter ( fileStream ) )
{
streamWriter . WriteLine ( exception . ToString ( ) ) ;
}
}
#endif
MessageBox . Show ( exception . Message , "Fehler" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
}
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
2020-06-17 19:36:52 +02:00
if ( contextItems = = null )
{
return ;
}
2019-04-02 13:43:30 +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
2020-06-17 19:36:52 +02:00
if ( contextItems . Count > 2 )
{
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
{
2017-12-11 14:32:56 +01:00
foreach ( var items in ChatListBox . SelectedItems )
2017-09-14 12:36:48 +02:00
{
2020-06-17 19:36:52 +02:00
var chatMessage = ( ChatMessage ) items ;
2017-09-12 14:32:34 +02:00
2021-07-14 19:33:50 +02:00
Chat . SaveFileAs ( chatMessage ) ;
2017-09-14 12:36:48 +02:00
}
}
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
private void PasteOnClick ( object sender , RoutedEventArgs routedEventArgs )
2017-09-14 12:36:48 +02:00
{
2021-07-14 19:33:50 +02:00
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
{
2020-06-17 19:36:52 +02:00
var chatMessages = string . Empty ;
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
2020-06-17 19:36:52 +02:00
chatMessages + = item . UserMessage + "\n" ;
2017-09-18 13:07:37 +02:00
}
2020-06-17 19:36:52 +02:00
if ( ! string . IsNullOrWhiteSpace ( chatMessages ) )
{
2021-07-14 19:33:50 +02:00
Chat . Copy ( chatMessages , 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 )
{
2020-06-17 19:36:52 +02:00
if ( ChatListBox ? . ContextMenu = = null )
{
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
{
2020-06-17 19:36:52 +02:00
foreach ( var items in chatMessages )
2017-09-14 12:36:48 +02:00
{
2021-07-14 19:33:50 +02:00
var item = ( ChatMessage ) items ;
if ( item ? . PictureSource ! = null )
2020-06-17 19:36:52 +02:00
{
if ( ChatListBox . ContextMenu ! = null )
{
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
2021-07-14 19:33:50 +02:00
if ( item . PictureSource = = null & & item . FilePath = = 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
2020-06-17 19:36:52 +02:00
if ( dataObject ! = null & & dataObject . GetDataPresent ( DataFormats . FileDrop ) )
{
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 if ( dataObject ! = null & & dataObject . GetDataPresent ( DataFormats . Text ) )
{
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 if ( dataObject ! = null & & dataObject . GetDataPresent ( DataFormats . Bitmap ) )
{
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
if ( CurrentContact . UserIdManage = = 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
}
}
private void MediaButton_OnClick ( object sender , RoutedEventArgs e )
{
2020-08-06 16:05:23 +02:00
if ( CurrentContact ! = 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
2021-09-07 19:32:10 +02:00
if ( pathToOriginalImage ! = null )
2020-06-17 19:36:52 +02:00
{
2021-09-07 19:32:10 +02:00
var pathToScaledImage = FileUtils . ScaleImage ( pathToOriginalImage , Path . GetExtension ( pathToOriginalImage . ToUpperInvariant ( ) ) , Chat . ChatDaten . MaxUploadSize ) ;
2020-06-17 19:36:52 +02:00
2021-09-07 19:32:10 +02:00
var isFileSizeTooLarge = FileUtils . CheckFileSize ( pathToScaledImage , Chat . ChatDaten . MaxUploadSize ) ;
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
if ( isFileSizeTooLarge )
{
2021-07-14 19:33:50 +02:00
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 ) ;
2020-06-17 19:36:52 +02:00
return ;
}
2017-11-17 15:40:11 +01:00
2021-09-07 19:32:10 +02:00
if ( ! string . IsNullOrWhiteSpace ( pathToScaledImage ) )
2017-11-17 15:40:11 +01:00
{
2021-09-07 19:32:10 +02:00
_ChatMessages . AddRangeIfElementsNotIn ( Chat . AddNewFile ( pathToScaledImage , pathToOriginalImage , CurrentContact . GroupId , GetFirstMessage ( ) ) ) ;
2017-11-17 15:40:11 +01:00
2021-07-14 19:33:50 +02:00
ChatMessages . MoveCurrentToLast ( ) ;
var currentChatMessage = ChatMessages . CurrentItem ;
ChatListBox . ScrollIntoView ( currentChatMessage ) ;
2017-11-17 15:40:11 +01:00
2021-09-07 19:32:10 +02:00
Chat . SendFileToContact ( CurrentContact , pathToScaledImage , pathToOriginalImage ) ;
2017-11-17 15:40:11 +01:00
2021-09-07 19:32:10 +02:00
if ( ! string . IsNullOrWhiteSpace ( pathToScaledImage ) & & ! pathToScaledImage . Equals ( pathToOriginalImage ) & & File . Exists ( pathToScaledImage ) )
2017-11-17 15:40:11 +01:00
{
2021-09-07 19:32:10 +02:00
File . Delete ( pathToScaledImage ) ;
2017-11-17 15:40:11 +01:00
}
}
2017-10-20 14:41:03 +02:00
}
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
}
private void SendButton_OnClick ( object sender , RoutedEventArgs e )
{
2020-08-06 16:05:23 +02:00
if ( CurrentContact ! = null & & ! string . IsNullOrEmpty ( Chatbox . Text ) )
2017-09-12 14:32:34 +02:00
{
2021-09-07 19:32:10 +02:00
var messages2Add = Chat . AddNewMessage ( Chatbox . Text , CurrentContact . GroupId , GetFirstMessage ( ) ) ;
2017-12-11 14:32:56 +01:00
2021-09-07 19:32:10 +02:00
_ChatMessages . AddRangeIfElementsNotIn ( messages2Add ) ;
OnPropertyChanged ( nameof ( ChatMessages ) ) ;
2021-07-14 19:33:50 +02:00
ChatMessages . MoveCurrentToLast ( ) ;
2017-10-24 11:39:59 +02:00
2021-07-14 19:33:50 +02:00
ChatListBox . ScrollIntoView ( ChatMessages . CurrentItem ) ;
2020-06-17 19:36:52 +02:00
2021-07-14 19:33:50 +02:00
WpfUtils . ScrollToBottomOfListBox ( ChatListBox ) ;
Chat . SendMessage ( Chatbox . Text , CurrentContact . GroupId ) ;
2020-06-17 19:36:52 +02:00
Chatbox . Text = string . Empty ;
2022-05-30 20:48:14 +02:00
DoSynchro ( ) ;
2017-09-12 14:32:34 +02:00
}
}
private void Chatbox_OnGotFocus ( object sender , RoutedEventArgs e )
{
if ( Chatbox . Text . Equals ( "Nachricht schreiben" ) )
{
2020-06-17 19:36:52 +02:00
Chatbox . Text = string . Empty ;
2017-09-12 14:32:34 +02:00
Chatbox . Foreground = new SolidColorBrush ( Colors . Black ) ;
}
}
private void Chatbox_OnKeyDownHandler ( object sender , KeyEventArgs e )
{
try
{
2021-07-14 19:33:50 +02: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
2021-07-14 19:33:50 +02:00
// OnPropertyChanged(nameof(CurrentChatMessages));
2020-06-17 19:36:52 +02:00
2021-07-14 19:33:50 +02:00
// ChatListBox.Items.MoveCurrentToLast();
// ChatListBox.ScrollIntoView(ChatListBox.Items.CurrentItem);
2017-10-20 14:41:03 +02:00
2021-07-14 19:33:50 +02:00
// Chat.SendMessage(Chatbox.Text, CurrentContact.GroupId);
2017-10-24 11:39:59 +02:00
2021-07-14 19:33:50 +02:00
// Chatbox.Text = string.Empty;
// }
//}
2017-09-12 14:32:34 +02:00
}
catch ( Exception ed )
{
MessageBox . Show ( "Beim Senden einer Nachricht ist ein Fehler aufgetreten. " + ed . Message + "\n" + ed . StackTrace , "Fehler" , MessageBoxButton . OK ) ;
}
}
2017-10-24 11:39:59 +02:00
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 ) ;
2019-05-14 13:37:20 +02:00
foreach ( var scrollBar in scrollBarList )
2017-09-12 14:32:34 +02:00
{
if ( scrollBar . Orientation = = Orientation . Horizontal )
{
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 )
{
2020-06-17 19:36:52 +02:00
if ( _ScrollPrueferAktivieren )
2017-09-12 14:32:34 +02:00
{
2020-06-17 19:36:52 +02:00
var scrollBar = ( ScrollBar ) sender ;
2017-09-12 14:32:34 +02:00
2020-06-17 19:36:52 +02:00
if ( ! ( scrollBar . Value > 0 ) )
2017-09-15 09:49:32 +02:00
{
2021-07-14 19:33:50 +02:00
StartWaitingImmediately ( ) ;
2019-04-02 13:43:30 +02:00
_ScrollPrueferAktivieren = false ;
2017-12-11 14:32:56 +01:00
2021-07-14 19:33:50 +02:00
ChatMessages . MoveCurrentToFirst ( ) ;
2017-09-12 14:32:34 +02:00
2021-07-14 19:33:50 +02:00
var currentChatMessage = ChatMessages . CurrentItem ;
var selectedContact = ( Contact ) Clientlist . SelectedItem ;
2017-09-12 14:32:34 +02:00
2021-07-14 19:33:50 +02:00
Chat . LoadMoreMessagesAsync ( selectedContact , GetFirstMessage ( ) , chatMessages = >
{
this . Dispatch ( ( ) = >
{
EndWaiting ( ) ;
2017-09-12 14:32:34 +02:00
2021-07-14 19:33:50 +02:00
_ChatMessages . AddRangeIfElementsNotIn ( chatMessages ) ;
2017-12-11 14:32:56 +01:00
2021-07-14 19:33:50 +02:00
ChatListBox . ScrollIntoView ( currentChatMessage ) ;
} ) ;
} ) ;
2017-09-12 14:32:34 +02:00
}
2020-06-17 19:36:52 +02:00
}
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 ( ) ;
GlobalListeningThreadtimer . Tick + = GlobalListeningThreadTimerTickEvent ;
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 GlobalListeningThreadTimerTickEvent ( object sender , EventArgs e )
{
ListenGloballyForMessages ( ) ;
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 ( ( ) = >
{
foreach ( var contact in _ContactList )
2017-12-11 14:32:56 +01:00
{
2021-07-14 19:33:50 +02:00
foreach ( var newContact in updatedContacts )
2019-05-14 13:37:20 +02:00
{
2021-07-14 19:33:50 +02:00
if ( contact . GroupId = = newContact . GroupId )
2019-05-14 13:37:20 +02:00
{
2021-07-14 19:33:50 +02:00
// ReceivedMessage kann null sein!
if ( contact . ReceivedMessage ! = null & & ! contact . ReceivedMessage . Id . Equals ( newContact . ReceivedMessage ? . Id ) )
2020-11-11 15:08:31 +01:00
{
2021-07-14 19:33:50 +02:00
contact . ReceivedMessage = newContact . ReceivedMessage ;
contact . TimeStamp = newContact . TimeStamp ;
var selectedContact = ( Contact ) Clientlist . SelectedItem ;
// Angemeldeter Benutzer
var userOid = Chat . ChatDaten . LoggedInUser . Response . User . Oid ;
var senderId = contact . ReceivedMessage ? . UserId ;
var receiverId = userOid ;
var receiverGroupId = newContact . GroupId ;
var selectedGroupId = selectedContact ? . GroupId ;
if ( senderId ! = receiverId & & ( _IsInBackground | | receiverGroupId ! = selectedGroupId ) )
{
if ( ContainingWindow ! = null )
{
ContainingWindow . Icon = Utils . ConvertIconToImageSource ( Resource . oC_favico_NewMessage ) ;
}
ShowNotification ( newContact . Name , newContact . ReceivedMessage , newContact . GroupId ) ;
}
// Wenn der momentan ausgewählte Kontakt der aktuelle Kontakt in der Schleife ist, werden die Nachrichten nicht als ungelesen angezeigt, sonst schon.
if ( CurrentContact ! = null & & newContact . GroupId = = CurrentContact . GroupId )
{
contact . HasUnreadMessages = false ;
}
else
{
contact . HasUnreadMessages = true ;
contact . IsNewMessage = true ;
}
2020-11-11 15:08:31 +01:00
}
2019-05-14 13:37:20 +02:00
}
}
2021-07-14 19:33:50 +02:00
if ( ! updatedContacts . Contains ( contact ) )
{
_ContactList . ToList ( ) . Remove ( contact ) ;
}
2019-05-14 13:37:20 +02:00
}
2022-05-30 20:48:14 +02:00
Clientlist . Items . Refresh ( ) ;
2022-05-27 11:50:37 +02:00
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
{
2019-05-15 14:57:16 +02:00
if ( ! ShouldInterruptContactsThread )
2019-05-14 13:37:20 +02:00
{
2021-07-14 19:33:50 +02:00
Chat . CheckIfNewMessagesExistAsync ( CurrentContact . GroupId , hasNewMessages = >
2019-05-14 13:37:20 +02:00
{
2021-07-14 19:33:50 +02:00
if ( ! hasNewMessages )
{
return ;
}
2017-10-24 11:39:59 +02:00
2021-07-14 19:33:50 +02:00
Chat . LoadChatMessagesForContactAsync ( pCurrentContact , GetFirstMessage ( ) , chatMessages = >
{
this . Dispatch ( ( ) = >
{
_ChatMessages . Clear ( ) ;
_ChatMessages . AddRangeIfElementsNotIn ( chatMessages ) ;
2017-09-12 14:32:34 +02:00
2021-07-14 19:33:50 +02:00
WpfUtils . ScrollToBottomOfListBox ( ChatListBox ) ;
2019-05-14 13:37:20 +02:00
} ) ;
2021-07-14 19:33:50 +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 )
{
2020-06-17 19:36:52 +02:00
if ( string . IsNullOrEmpty ( Suche . Text ) )
{
2017-09-12 14:32:34 +02:00
return true ;
2020-06-17 19:36:52 +02:00
}
return ( item as Contact ) . Name . IndexOf ( Suche . Text , StringComparison . OrdinalIgnoreCase ) > = 0 ;
2017-09-12 14:32:34 +02:00
}
2017-09-14 12:36:48 +02:00
private void Chat_OnMouseDoubleClick ( object sender , MouseButtonEventArgs e )
{
2021-07-14 19:33:50 +02:00
if ( ChatListBox . SelectedItems . Count > 0 )
2017-10-20 07:34:26 +02:00
{
2021-07-14 19:33:50 +02:00
var selectedItem = ChatListBox . SelectedItems [ 0 ] ;
2017-09-14 12:36:48 +02:00
2021-07-14 19:33:50 +02:00
if ( selectedItem is ChatMessage selectedMessage )
{
2021-09-07 19:32:10 +02:00
if ( ! string . IsNullOrEmpty ( selectedMessage . OriginalImage ) )
2021-07-14 19:33:50 +02:00
{
2021-09-07 19:32:10 +02:00
var uri = new Uri ( selectedMessage . OriginalImage ) ;
if ( uri . IsFile )
{
var bitmapImage = new BitmapImage ( uri ) ;
bitmapImage . Freeze ( ) ;
Chat . ShowPictureWindow ( bitmapImage , "Test" ) ;
return ;
}
2020-06-17 19:36:52 +02:00
StartWaitingImmediately ( ) ;
2021-07-14 19:33:50 +02:00
Chat . ShowPicture ( selectedMessage . OriginalImage , CurrentContact . GroupId , ( imageSource , windowTitle ) = >
{
this . Dispatch ( ( ) = >
{
EndWaiting ( ) ;
Chat . ShowPictureWindow ( imageSource , windowTitle ) ;
} ) ;
} ) ;
2017-09-14 12:36:48 +02:00
}
2021-07-14 19:33:50 +02:00
else if ( selectedMessage . FilePath ! = null )
2017-11-06 07:16:43 +01:00
{
2021-07-14 19:33:50 +02:00
StartWaitingImmediately ( ) ;
2017-11-06 07:16:43 +01:00
2021-07-14 19:33:50 +02:00
LoadDocumentAsync ( selectedMessage . FilePath . ToString ( ) , Path . GetFileName ( selectedMessage . FilePath . ToString ( ) ) , 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
}
2021-07-14 19:33:50 +02:00
catch ( Exception exception )
2017-12-18 18:22:13 +01:00
{
EndWaiting ( ) ;
2021-07-14 19:33:50 +02:00
MessageBox . Show ( exception . Message , "Fehler" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
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
public void StartWaiting ( )
{
Dispatcher . BeginInvoke ( DispatcherPriority . Background , ( Action ) StartWaitingImmediately ) ;
}
public void StartWaitingImmediately ( )
{
2020-06-17 19:36:52 +02:00
if ( _WaitLayer = = null )
2017-09-15 09:49:32 +02:00
{
2020-06-17 19:36:52 +02:00
_WaitLayer = new ChatControlWaitLayer ( ) ;
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 (
DispatcherPriority . Background ,
2020-06-17 19:36:52 +02:00
( Action ) delegate
2017-09-15 09:49:32 +02:00
{
2020-06-17 19:36:52 +02:00
if ( _WaitLayer ! = null )
2017-09-15 09:49:32 +02:00
{
2020-06-17 19:36:52 +02: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 ( ( ) = >
{
2021-07-14 19:33:50 +02:00
_ContactList = Chat . UpdateContacts ( data ) ; // <- dauert eine Sekunde!
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
}
catch ( Exception e )
{
2020-06-17 19:36:52 +02:00
EndWaiting ( ) ;
2017-10-06 12:08:05 +02:00
MessageBox . Show ( "Ein Fehler bei der Synchronisation ist aufgetreten.\nFehler:\n" + e . Message , "Fehler" , MessageBoxButton . OK ) ;
}
}
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
{
2019-05-14 13:37:20 +02:00
if ( File . Exists ( Path . Combine ( Utils . GetAndCreateUserAppDataPath ( ) , Resource . SyncFile2Name ) ) )
{
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 ( ) ;
foreach ( var contact in _ContactList . ToList ( ) . Where ( contact = > contact . ReceivedMessage ! = null ) )
{
2019-05-15 14:57:16 +02:00
var text = contact . GroupId + ";" + contact . TimeStamp . ToUniversalTime ( ) + ";" + 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
}
}
2019-05-14 13:37:20 +02:00
catch ( Exception exception )
{
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
2019-05-14 13:37:20 +02:00
while ( ( line = streamReader . ReadLine ( ) ) ! = null )
{
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
}
catch ( Exception e )
{
}
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
{
2019-09-03 15:15:12 +02:00
if ( contactItem is Contact contact & & contact . HasUnreadMessages & & ! string . IsNullOrEmpty ( contact . ReceivedMessage ? . Text ) )
2017-11-09 12:20:03 +01:00
{
2019-05-14 13:37:20 +02:00
contacts . Add ( 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
{
2019-05-14 13:37:20 +02:00
if ( pGroupId2DateTime ! = null )
2017-11-09 12:20:03 +01:00
{
2019-05-14 13:37:20 +02:00
foreach ( var contact in _ContactList )
2017-11-09 12:20:03 +01:00
{
2019-05-14 13:37:20 +02:00
foreach ( var groupId2DateTime in pGroupId2DateTime )
2017-11-09 12:20:03 +01:00
{
2019-05-14 13:37:20 +02:00
if ( contact . GroupId = = groupId2DateTime . Key )
2017-11-09 12:20:03 +01:00
{
2019-05-14 13:37:20 +02:00
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
{
2020-08-06 16:05:23 +02:00
return CurrentContact ? . UserIdManage ! = 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
{
2019-04-02 13:43:30 +02:00
foreach ( var file in _CreatedTempFiles )
2017-12-18 18:22:13 +01:00
{
try
{
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
}
catch ( Exception e )
{
//ignore
}
}
}
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 ;
}
}
2021-07-14 19:33:50 +02:00
public void AddMessages ( List < ChatMessage > newMessages )
{
_ChatMessages . AddRangeIfElementsNotIn ( newMessages ) ;
}
public ChatMessage GetFirstMessage ( )
{
return _ChatMessages . FirstOrDefault ( ) ;
}
2017-09-12 14:32:34 +02:00
}
}