2017-09-12 14:32:34 +02:00
using System ;
using System.Collections.Generic ;
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 ;
2017-09-14 12:36:48 +02:00
using System.Net ;
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 ;
using System.Windows.Navigation ;
using System.Windows.Threading ;
2019-04-02 13:43:30 +02:00
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 ;
2019-05-15 14:57:16 +02:00
2019-04-04 13:00:10 +02:00
using Button = System . Windows . Controls . Button ;
using Cursors = System . Windows . Input . Cursors ;
using DataFormats = System . Windows . DataFormats ;
using KeyEventArgs = System . Windows . Input . KeyEventArgs ;
using MenuItem = System . Windows . Controls . MenuItem ;
using MessageBox = System . Windows . MessageBox ;
using Orientation = System . Windows . Controls . Orientation ;
using Panel = System . Windows . Controls . Panel ;
2017-09-14 12:36:48 +02:00
using Path = System . IO . Path ;
2019-04-04 13:00:10 +02:00
using ScrollBar = System . Windows . Controls . Primitives . ScrollBar ;
2019-05-14 13:37:20 +02:00
using Timer = System . Windows . Forms . Timer ;
2017-09-12 14:32:34 +02:00
namespace ChatController
{
2019-04-02 13:43:30 +02:00
public partial class ChatMainControl
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 > ( ) ;
2019-09-02 19:04:55 +02:00
private Window _ContainingWindow ;
public Window ContainingWindow
{
get = > _ContainingWindow ;
set
{
_ContainingWindow = value ;
if ( value ! = null )
{
_ContainingWindow . Deactivated + = ContainingWindowOnDeactivated ;
_ContainingWindow . Activated + = ContainingWindowOnActivated ;
}
}
}
2019-05-14 13:37:20 +02:00
2019-04-02 13:43:30 +02:00
private Chat _Chat ;
2019-05-14 13:37:20 +02:00
private CurrentContact _CurrentContact ;
2017-09-12 14:32:34 +02:00
2019-04-02 13:43:30 +02:00
private bool _IsChatBoxOnFocus ;
2017-09-12 14:32:34 +02:00
2019-04-04 13:00:10 +02:00
private IOrderedEnumerable < 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 ( ) ;
2019-05-14 13:37:20 +02:00
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 ;
}
2017-09-12 14:32:34 +02:00
public void InitMitChatdaten ( ChatDatenUebergabe cdu )
{
2019-04-02 13:43:30 +02:00
_Chat = new Chat ( cdu ) ;
2019-05-15 14:57:16 +02:00
2019-05-14 13:37:20 +02:00
_ContactList = _Chat . AddContacts ( ) ;
2017-09-12 14:32:34 +02:00
2019-04-02 13:43:30 +02:00
Clientlist . ItemsSource = _ContactList ;
2019-05-14 13:37:20 +02:00
ReadNewSyncFile ( ) ;
foreach ( var contact in _ContactList )
{
if ( _GroupId2DateTime . ContainsKey ( contact . GroupId ) )
{
var dateTimeFromFile = _GroupId2DateTime [ contact . GroupId ] . TimeStamp ;
var isUnread = _GroupId2DateTime [ contact . GroupId ] . IsUnread ;
var isYounger = contact . TimeStamp > dateTimeFromFile ;
2019-05-15 14:57:16 +02:00
contact . IsNewMessage = isUnread ;
2019-05-14 13:37:20 +02:00
contact . HasUnreadMessages = contact . IsNewMessage ;
}
}
2019-04-02 13:43:30 +02:00
var view = ( CollectionView ) CollectionViewSource . GetDefaultView ( Clientlist . ItemsSource ) ;
2019-05-14 13:37:20 +02:00
view . Filter = UserFilter ;
2017-09-12 14:32:34 +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
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
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
2019-09-03 15:15:12 +02:00
SelectContact ( _ContactList . FirstOrDefault ( f = > f . GroupId . Equals ( pGroupId ) ) ) ;
} ;
2019-05-14 13:37:20 +02:00
2019-09-03 15:15:12 +02:00
notifyIcon . BalloonTipClosed + = ( sender , args ) = >
{
DisposeAndRemoveNotification ( ( NotifyIcon ) sender , pGroupId ) ;
} ;
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 )
{
notifyIcon . Dispose ( ) ;
}
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 ) ;
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
}
2017-11-16 15:38:30 +01:00
2017-09-12 14:32:34 +02:00
private void AktChatUser_OnMouseDoubleClick ( object sender , MouseButtonEventArgs e )
{
2019-05-14 13:37:20 +02:00
_Chat . ShowProfilePicture ( _CurrentContact ) ;
2017-09-12 14:32:34 +02:00
}
private void Clientlist_OnSelectionChanged ( object sender , SelectionChangedEventArgs e )
{
2017-09-15 09:49:32 +02:00
if ( Clientlist . SelectedItem ! = null )
{
2019-05-14 13:37:20 +02:00
SelectContact ( ( Contact ) Clientlist . SelectedItem ) ;
}
}
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
private void SelectContact ( Contact pContact )
{
2019-05-15 14:57:16 +02:00
if ( pContact = = null )
{
return ;
}
ShouldInterruptContactsThread = true ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
Cursor = Cursors . Wait ;
_ScrollPrueferAktivieren = false ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
AktChatUserList . Items . Clear ( ) ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
if ( pContact . HasUnreadMessages )
{
pContact . HasUnreadMessages = false ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
Clientlist . Items . Refresh ( ) ;
}
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
var currentContact = new CurrentContact ( pContact ) ;
pContact . IsNewMessage = false ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
var shouldChangeIcon = _ContactList . Any ( contact = > contact . IsNewMessage ) ;
if ( shouldChangeIcon )
{
ContainingWindow . Icon = Utils . GetImageSourceFromIcon ( Resource . ownchat_favicon ) ;
}
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
AktChatUserList . Items . Add ( currentContact ) ;
_CurrentContact = currentContact ;
2019-09-02 19:04:55 +02:00
if ( Clientlist . Items . Contains ( pContact ) )
{
Clientlist . SelectedItem = pContact ;
}
2019-05-14 13:37:20 +02:00
var chatMessages = _Chat . LoadChatMessagesForContact ( currentContact ) ;
ChatListBox . ItemsSource = chatMessages ;
2017-12-18 18:22:13 +01:00
2019-05-14 13:37:20 +02:00
if ( VisualTreeHelper . GetChildrenCount ( ChatListBox ) > 0 )
{
var border = ( Border ) VisualTreeHelper . GetChild ( ChatListBox , 0 ) ;
var scrollViewer = ( ScrollViewer ) VisualTreeHelper . GetChild ( border , 0 ) ;
scrollViewer . ScrollToBottom ( ) ;
}
2017-12-18 18:22:13 +01:00
2019-05-14 13:37:20 +02:00
ChatListBox . ContextMenu = _Chat . ErstelleKontextMenue ( ) ;
SetContextHandler ( ) ;
_ScrollPrueferAktivieren = true ;
2017-11-10 11:41:09 +01:00
2019-05-14 13:37:20 +02:00
ListenForMessages ( ) ;
Cursor = Cursors . Arrow ;
2017-12-18 18:22:13 +01:00
2019-05-14 13:37:20 +02:00
WriteToNewSyncFile ( ) ;
2019-05-15 14:57:16 +02:00
ShouldInterruptContactsThread = false ;
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
2019-04-02 13:43:30 +02:00
foreach ( var menuItemText in _MenuItemName2Callback . Keys )
2017-09-12 14:32:34 +02:00
{
2019-04-02 13:43:30 +02:00
var cb = _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
{
cb ( menuItemText ) ;
} ;
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
}
var item2 = ( MenuItem ) contextItems [ 0 ] ;
2017-09-18 13:07:37 +02:00
item2 . Click + = Item2OnClick ; // umbenenen in Speichern unter
2017-09-12 14:32:34 +02:00
var item3 = ( MenuItem ) contextItems [ 1 ] ;
2017-09-18 13:07:37 +02:00
item3 . Click + = Item3OnClick ; //Umbenenen in Einfügen
// neu und vor Einfügen setzen und anpassen
var item4 = ( MenuItem ) contextItems [ 2 ] ;
item4 . Click + = Item4OnClick ; //Umbenenen in Kopieren
2017-09-12 14:32:34 +02:00
}
2017-09-14 12:36:48 +02:00
private void Item2OnClick ( 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
{
2017-12-11 14:32:56 +01:00
var item = ( ChatMessage ) items ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
_Chat . SaveFileAs ( item ) ;
2017-09-14 12:36:48 +02:00
}
}
2017-09-12 14:32:34 +02:00
2017-09-14 12:36:48 +02:00
private void Item3OnClick ( object sender , RoutedEventArgs routedEventArgs )
{
2019-05-14 13:37:20 +02:00
var kontakt = ( CurrentContact ) AktChatUserList . Items [ 0 ] ;
_Chat . Paste ( kontakt . GroupId , this ) ;
2017-09-18 13:07:37 +02:00
}
private void Item4OnClick ( object sender , RoutedEventArgs routedEventArgs )
{
string chatmessages = "" ;
2017-12-11 14:32:56 +01:00
foreach ( var items in ChatListBox . SelectedItems )
2017-09-18 13:07:37 +02:00
{
2017-12-11 14:32:56 +01:00
var item = ( ChatMessage ) items ;
2017-09-18 13:07:37 +02:00
chatmessages + = item . UserMessage + "\n" ;
}
if ( ! chatmessages . Equals ( "" ) )
2019-05-14 13:37:20 +02:00
_Chat . Copy ( chatmessages , 1 ) ;
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 )
{
2017-12-11 14:32:56 +01:00
var chatItems = ChatListBox . SelectedItems ;
2017-09-14 12:36:48 +02:00
if ( chatItems . Count > 0 )
{
foreach ( var items in chatItems )
{
2017-12-11 14:32:56 +01:00
var item = items as ChatMessage ;
2017-09-14 12:36:48 +02:00
if ( item ! = null & & item . ImageSources ! = null )
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-14 12:36:48 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 0 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Visible ;
}
else
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-14 12:36:48 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 0 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
}
2017-09-18 13:07:37 +02:00
2019-05-14 13:37:20 +02:00
if ( item . ImageSources = = null & & item . FilePath = = null )
2017-09-18 13:07:37 +02:00
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-18 13:07:37 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 2 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Visible ;
}
else
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-18 13:07:37 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 2 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
}
2017-09-14 12:36:48 +02:00
}
}
else
{
//Schalte speichern unter Aus
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-14 12:36:48 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 0 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
}
2017-09-12 14:32:34 +02:00
2017-09-14 12:36:48 +02:00
//prüfe ob was in dem Speicher vorhanden ist //Einfügen
var d = System . Windows . Forms . Clipboard . GetDataObject ( ) ;
2017-09-18 13:07:37 +02:00
if ( d . GetDataPresent ( DataFormats . FileDrop ) )
2017-09-14 12:36:48 +02:00
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-14 12:36:48 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Visible ;
}
else if ( d . GetDataPresent ( DataFormats . Text ) )
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-14 12:36:48 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Visible ;
}
else if ( d . GetDataPresent ( DataFormats . Bitmap ) )
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-14 12:36:48 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Visible ;
}
else
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-14 12:36:48 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 1 ] ;
ContextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
2017-09-18 13:07:37 +02:00
}
2017-09-12 14:32:34 +02:00
2017-09-15 09:49:32 +02:00
//prüfe ob dokumentation erlaubt
2019-05-14 13:37:20 +02:00
var kontakt = ( CurrentContact ) AktChatUserList . Items [ 0 ] ;
if ( kontakt . UserIdManage = = null & & ChatListBox . ContextMenu . Items . Count > 3 )
2017-09-12 14:32:34 +02:00
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-18 13:07:37 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 3 ] ;
2017-09-14 12:36:48 +02:00
ContextItemSpeichernUnter . Visibility = Visibility . Collapsed ;
}
2017-12-11 14:32:56 +01:00
else if ( ChatListBox . ContextMenu . Items . Count > 3 )
2017-09-15 09:49:32 +02:00
{
2017-12-11 14:32:56 +01:00
var contextItems = ChatListBox . ContextMenu . Items ;
2017-09-18 13:07:37 +02:00
var ContextItemSpeichernUnter = ( MenuItem ) contextItems [ 3 ] ;
2017-09-15 09:49:32 +02:00
ContextItemSpeichernUnter . Visibility = Visibility . Visible ;
}
2017-09-18 13:07:37 +02:00
2017-09-15 09:49:32 +02:00
2017-09-12 14:32:34 +02:00
2017-12-11 14:32:56 +01:00
if ( ChatListBox . ContextMenu . Items . Count = = 0 )
2017-09-14 12:36:48 +02:00
{
2017-12-11 14:32:56 +01:00
ChatListBox . ContextMenu . Visibility = Visibility . Collapsed ;
2017-09-12 14:32:34 +02:00
}
2017-09-14 12:36:48 +02:00
2017-09-12 14:32:34 +02:00
}
private void MediaButton_OnClick ( object sender , RoutedEventArgs e )
{
2017-12-11 14:32:56 +01:00
if ( AktChatUserList . Items . Count > 0 )
2017-09-14 12:36:48 +02:00
{
2019-05-14 13:37:20 +02:00
var kontakt = ( CurrentContact ) AktChatUserList . Items [ 0 ] ;
2017-10-20 14:41:03 +02:00
2019-05-14 13:37:20 +02:00
var medium = _Chat . OpenFile ( ) ;
2017-09-12 14:32:34 +02:00
2017-10-20 14:41:03 +02:00
if ( medium ! = null ) {
2017-09-12 14:32:34 +02:00
2019-04-02 13:43:30 +02:00
string convertetMedia = _Chat . ScaleImage ( medium , Path . GetExtension ( medium . ToUpperInvariant ( ) ) ) ;
2017-11-17 15:40:11 +01:00
if ( ! convertetMedia . Equals ( "" ) )
{
2019-05-14 13:37:20 +02:00
_Chat . AddNewFile ( convertetMedia , medium , kontakt . GroupId ) ;
2017-11-17 15:40:11 +01:00
2017-12-11 14:32:56 +01:00
CollectionViewSource . GetDefaultView ( ChatListBox . ItemsSource ) . Refresh ( ) ;
ChatListBox . Items . MoveCurrentToLast ( ) ;
ChatListBox . ScrollIntoView ( ChatListBox . Items . CurrentItem ) ;
2017-11-17 15:40:11 +01:00
2019-05-14 13:37:20 +02:00
_Chat . SendFileToContact ( kontakt , convertetMedia , medium ) ;
2017-11-17 15:40:11 +01:00
2017-12-11 14:32:56 +01:00
if ( ! convertetMedia . Equals ( "" ) & & ! convertetMedia . Equals ( medium ) & & File . Exists ( convertetMedia ) )
2017-11-17 15:40:11 +01:00
{
File . Delete ( convertetMedia ) ;
}
}
else
{
2017-12-11 14:32:56 +01:00
MessageBox . Show ( "Die gewählte Datei ist zu groß!" , "Fehler" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
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 )
{
2019-04-02 13:43:30 +02:00
if ( ! AktChatUserList . Items . IsEmpty & & ! Chatbox . Text . Equals ( "" ) & & _IsChatBoxOnFocus )
2017-09-12 14:32:34 +02:00
{
if ( ! Chatbox . Text . Equals ( "" ) )
{
2019-05-14 13:37:20 +02:00
var kontakt = ( CurrentContact ) AktChatUserList . Items [ 0 ] ;
2017-10-20 14:41:03 +02:00
2019-05-14 13:37:20 +02:00
_Chat . AddNewMessage ( Chatbox . Text , kontakt . GroupId ) ;
2017-10-20 14:41:03 +02:00
2017-12-11 14:32:56 +01:00
CollectionViewSource . GetDefaultView ( ChatListBox . ItemsSource ) . Refresh ( ) ;
ChatListBox . Items . MoveCurrentToLast ( ) ;
ChatListBox . ScrollIntoView ( ChatListBox . Items . CurrentItem ) ;
2017-10-20 14:41:03 +02:00
2019-04-02 13:43:30 +02:00
_Chat . SendMessage ( Chatbox . Text , kontakt . GroupId ) ;
2017-10-24 11:39:59 +02:00
2017-10-20 14:41:03 +02:00
Chatbox . Text = "" ;
2017-09-12 14:32:34 +02:00
if ( Chatbox . Text . Equals ( "" ) )
{
Chatbox . Text = "Nachricht schreiben" ;
Chatbox . Foreground = new SolidColorBrush ( Colors . DarkGray ) ;
2019-04-02 13:43:30 +02:00
_IsChatBoxOnFocus = false ;
2017-09-12 14:32:34 +02:00
}
}
}
else
{
2017-12-11 14:32:56 +01:00
if ( AktChatUserList . Items . IsEmpty )
2017-09-12 14:32:34 +02:00
{
MessageBox . Show ( "Bitte wählen Sie einen Chatpartner aus." , "Fehler" , MessageBoxButton . OK ) ;
2019-05-14 13:37:20 +02:00
if ( string . IsNullOrEmpty ( Chatbox . Text ) )
2017-09-12 14:32:34 +02:00
{
Chatbox . Text = "Nachricht schreiben" ;
Chatbox . Foreground = new SolidColorBrush ( Colors . DarkGray ) ;
2019-04-02 13:43:30 +02:00
_IsChatBoxOnFocus = false ;
2017-09-12 14:32:34 +02:00
}
}
2019-05-14 13:37:20 +02:00
else if ( string . IsNullOrEmpty ( Chatbox . Text ) )
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
MessageBox . Show ( "Sie können keine leere Nachricht verschicken." , "Fehler" , MessageBoxButton . OK ) ;
Chatbox . Text = "Nachricht schreiben" ;
Chatbox . Foreground = new SolidColorBrush ( Colors . DarkGray ) ;
_IsChatBoxOnFocus = false ;
2017-09-12 14:32:34 +02:00
}
}
}
private void Chatbox_OnGotFocus ( object sender , RoutedEventArgs e )
{
if ( Chatbox . Text . Equals ( "Nachricht schreiben" ) )
{
Chatbox . Text = "" ;
Chatbox . Foreground = new SolidColorBrush ( Colors . Black ) ;
2019-04-02 13:43:30 +02:00
_IsChatBoxOnFocus = true ;
2017-09-12 14:32:34 +02:00
}
}
private void Chatbox_OnKeyDownHandler ( object sender , KeyEventArgs e )
{
try
{
if ( e . Key = = Key . Return )
{
2017-12-11 14:32:56 +01:00
if ( ! AktChatUserList . Items . IsEmpty & & ! Chatbox . Text . Equals ( "" ) )
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
var kontakt = ( CurrentContact ) AktChatUserList . Items [ 0 ] ;
2017-10-20 14:41:03 +02:00
2019-05-14 13:37:20 +02:00
_Chat . AddNewMessage ( Chatbox . Text , kontakt . GroupId ) ;
2017-09-12 14:32:34 +02:00
2017-12-11 14:32:56 +01:00
CollectionViewSource . GetDefaultView ( ChatListBox . ItemsSource ) . Refresh ( ) ;
ChatListBox . Items . MoveCurrentToLast ( ) ;
ChatListBox . ScrollIntoView ( ChatListBox . Items . CurrentItem ) ;
2017-10-20 14:41:03 +02:00
2019-04-02 13:43:30 +02:00
_Chat . SendMessage ( Chatbox . Text , kontakt . GroupId ) ;
2017-10-24 11:39:59 +02:00
2017-11-09 12:20:03 +01:00
2017-09-12 14:32:34 +02:00
Chatbox . Text = "" ;
2017-10-20 14:41:03 +02:00
2017-09-12 14:32:34 +02:00
}
else
{
2017-12-11 14:32:56 +01:00
if ( AktChatUserList . Items . IsEmpty )
2017-09-12 14:32:34 +02:00
{
MessageBox . Show ( "Bitte wählen Sie einen Chatpartner aus." , "Fehler" , MessageBoxButton . OK ) ;
}
else if ( Chatbox . Text . Equals ( "" ) )
{
2020-02-26 17:31:34 +01:00
MessageBox . Show ( "Sie können keine leere Nachricht verschicken." , "Fehler" , MessageBoxButton . OK ) ;
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 )
{
//hier wie bei login
if ( OnEmojii ! = null )
{
OnEmojii ( EmojiButton ) ;
}
}
2019-05-14 13:37:20 +02:00
2017-09-12 14:32:34 +02:00
private void Chat_OnScrollChanged ( object sender , ScrollChangedEventArgs e )
{
2019-05-14 13:37:20 +02:00
var scrollBarList = GetVisualChildCollection < ScrollBar > ( ChatListBox ) ;
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 )
{
2019-04-02 13:43:30 +02:00
if ( _ScrollPrueferAktivieren = = true )
2017-09-12 14:32:34 +02:00
{
var x = ( ScrollBar ) sender ;
if ( x . Value > 0 )
{
//nix
}
else
2017-09-15 09:49:32 +02:00
{
Cursor = Cursors . Wait ;
//StartWaiting();
2019-04-02 13:43:30 +02:00
_ScrollPrueferAktivieren = false ;
2017-12-11 14:32:56 +01:00
ChatListBox . Items . MoveCurrentToFirst ( ) ;
ChatMessage item = ChatListBox . Items . CurrentItem as ChatMessage ;
ChatListBox . ItemsSource = null ;
2017-09-12 14:32:34 +02:00
2019-04-04 13:00:10 +02:00
var kontakt = ( Contact ) Clientlist . SelectedItem ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
var xy = _Chat . LoadMoreMessages ( kontakt ) ;
2017-09-12 14:32:34 +02:00
2017-12-11 14:32:56 +01:00
ChatListBox . ItemsSource = xy ;
ChatListBox . ScrollIntoView ( item ) ;
2017-09-15 09:49:32 +02:00
//EndWaiting();
Cursor = Cursors . Arrow ;
2017-09-12 14:32:34 +02:00
}
}
}
private static List < T > GetVisualChildCollection < T > ( object parent ) where T : Visual
{
2019-05-14 13:37:20 +02:00
var visualCollection = new List < T > ( ) ;
2017-09-12 14:32:34 +02:00
GetVisualChildCollection ( parent as DependencyObject , visualCollection ) ;
2019-05-14 13:37:20 +02:00
2017-09-12 14:32:34 +02:00
return visualCollection ;
}
2019-05-14 13:37:20 +02:00
private static void GetVisualChildCollection < T > ( DependencyObject parent , ICollection < T > visualCollection ) where T : Visual
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
var count = VisualTreeHelper . GetChildrenCount ( parent ) ;
for ( var i = 0 ; i < count ; i + + )
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
var child = VisualTreeHelper . GetChild ( parent , i ) ;
if ( child is T item )
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
visualCollection . Add ( item ) ;
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
else
2017-09-12 14:32:34 +02:00
{
GetVisualChildCollection ( child , visualCollection ) ;
}
}
}
2019-05-14 13:37:20 +02:00
private void ListenForMessages ( )
{
_ListeningThreadTimer ? . Stop ( ) ;
_ListeningThreadTimer = null ;
InitListeningThread ( ( CurrentContact ) AktChatUserList . Items [ 0 ] ) ;
}
2017-09-12 14:32:34 +02:00
2019-05-15 14:57:16 +02:00
public Timer _ListeningThreadTimer ;
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 ;
GlobalListeningThreadtimer . Interval = 2000 ;
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
{
2019-05-14 13:37:20 +02:00
var numberOfNewMessages = _Chat . GetNumberOfAllNewMessages ( ) ;
2017-12-18 18:22:13 +01:00
2019-05-14 13:37:20 +02:00
if ( numberOfNewMessages > 0 )
{
Dispatcher . BeginInvoke (
DispatcherPriority . Normal ,
( Action ) delegate
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
var contacts = _Chat . NeueGroupklassenKontakte ( ) . ToList ( ) ;
CurrentContact currentContact = null ;
2017-12-11 14:32:56 +01:00
2019-05-14 13:37:20 +02:00
if ( AktChatUserList . HasItems )
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
currentContact = ( CurrentContact ) AktChatUserList . Items [ 0 ] ;
}
foreach ( var contact in _ContactList )
{
foreach ( var newContact in contacts )
{
if ( contact . GroupId = = newContact . GroupId )
2017-12-11 14:32:56 +01:00
{
2019-09-03 15:15:12 +02:00
// ReceivedMessage kann null sein!
if ( contact . ReceivedMessage ! = null & & ! contact . ReceivedMessage . Id . Equals ( newContact . ReceivedMessage ? . Id ) )
2019-05-14 13:37:20 +02:00
{
contact . ReceivedMessage = newContact . ReceivedMessage ;
contact . TimeStamp = newContact . TimeStamp ;
2017-12-11 14:32:56 +01:00
2019-09-02 19:04:55 +02:00
// TODO: Prüfen, ob die App im Hintergrund oder minimiert ist
if ( ( Clientlist . SelectedItem ! = null & & ! ( ( Contact ) Clientlist . SelectedItem ) . GroupId . Equals ( newContact . GroupId ) | | Clientlist . SelectedItem = = null ) | | _IsInBackground )
2019-05-14 13:37:20 +02:00
{
ContainingWindow . Icon = Utils . GetImageSourceFromIcon ( Resource . oC_favico_NewMessage ) ;
2019-09-03 15:15:12 +02:00
ShowNotification ( newContact . Name , newContact . ReceivedMessage , newContact . GroupId ) ;
2019-05-14 13:37:20 +02:00
}
2017-12-11 14:32:56 +01:00
2019-05-14 13:37:20 +02: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 ! = null & & newContact . GroupId = = currentContact . GroupId )
{
contact . HasUnreadMessages = false ;
}
else
{
contact . HasUnreadMessages = true ;
contact . IsNewMessage = true ;
}
}
}
if ( ! _ContactList . Contains ( newContact ) )
{
_ContactList . ToList ( ) . Add ( newContact ) ;
}
}
if ( ! contacts . Contains ( contact ) )
{
_ContactList . ToList ( ) . Remove ( contact ) ;
}
2017-12-11 14:32:56 +01:00
}
2019-05-14 13:37:20 +02:00
Clientlist . Items . Refresh ( ) ;
} ) ;
2017-09-12 14:32:34 +02:00
}
}
2019-05-14 13:37:20 +02:00
private void InitListeningThread ( CurrentContact pCurrentContacObject )
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
_ListeningThreadTimer = new Timer { Tag = pCurrentContacObject } ;
_ListeningThreadTimer . Tick + = ListeningThreadTimerTickEvent ;
_ListeningThreadTimer . Interval = 2000 ;
_ListeningThreadTimer . Start ( ) ;
2017-09-12 14:32:34 +02:00
}
2019-05-14 13:37:20 +02:00
private void ListeningThreadTimerTickEvent ( object sender , EventArgs e )
{
ListenForMessagesForCurrentContact ( ( CurrentContact ) ( ( Timer ) sender ) . Tag ) ;
}
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
private void ListenForMessagesForCurrentContact ( CurrentContact pCurrentContact )
{
2019-05-15 14:57:16 +02:00
if ( ! ShouldInterruptContactsThread )
2019-05-14 13:37:20 +02:00
{
var hasNewMessages = _Chat . CheckIfNewMessagesExist ( pCurrentContact ) ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
if ( hasNewMessages )
{
Dispatcher . BeginInvoke ( DispatcherPriority . Background ,
( Action ) delegate
2017-09-12 14:32:34 +02:00
{
2019-05-14 13:37:20 +02:00
_Chat . LoadChatMessagesForContact ( pCurrentContact ) ;
2017-10-24 11:39:59 +02:00
2019-05-14 13:37:20 +02:00
CollectionViewSource . GetDefaultView ( ChatListBox . ItemsSource ) . Refresh ( ) ;
2017-09-12 14:32:34 +02:00
2019-05-14 13:37:20 +02:00
ChatListBox . Items . MoveCurrentToLast ( ) ;
ChatListBox . ScrollIntoView ( ChatListBox . Items . CurrentItem ) ;
} ) ;
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 )
{
if ( String . IsNullOrEmpty ( Suche . Text ) )
return true ;
else
2019-04-04 13:00:10 +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 )
{
2017-10-20 07:34:26 +02:00
Cursor = Cursors . Wait ;
2017-12-18 18:22:13 +01:00
2017-12-11 14:32:56 +01:00
var chatItems = ChatListBox . SelectedItems ;
2017-09-14 12:36:48 +02:00
2017-10-20 07:34:26 +02:00
if ( chatItems . Count > 0 )
{
2017-09-14 12:36:48 +02:00
foreach ( var items in chatItems )
{
2017-12-11 14:32:56 +01:00
var item = items as ChatMessage ;
2017-09-14 12:36:48 +02:00
if ( item ! = null & & item . ImageSources ! = null )
2017-11-06 07:16:43 +01:00
{
2017-12-18 18:22:13 +01:00
StartWaiting ( ) ;
2019-04-02 13:43:30 +02:00
_Chat . ShowPicture ( item , EndWaiting ) ;
2017-09-14 12:36:48 +02:00
}
2019-05-14 13:37:20 +02:00
else if ( item ! = null & & item . FilePath ! = null )
2017-11-06 07:16:43 +01:00
{
2019-05-14 13:37:20 +02:00
var filename = Path . GetFileName ( item . FilePath . ToString ( ) ) ;
LoadDocument ( item . FilePath . ToString ( ) , filename ) ;
2017-11-06 07:16:43 +01:00
}
2017-09-14 12:36:48 +02:00
}
2017-11-06 07:16:43 +01:00
Cursor = Cursors . Arrow ;
2017-09-14 12:36:48 +02:00
}
2017-10-20 07:34:26 +02:00
else
{
Cursor = Cursors . Arrow ;
2017-11-06 07:16:43 +01:00
}
2017-09-14 12:36:48 +02:00
}
2019-05-14 13:37:20 +02:00
private void LoadDocument ( string link , string filename )
2017-12-18 18:22:13 +01:00
{
try
2017-09-14 12:36:48 +02:00
{
2017-12-18 18:22:13 +01:00
StartWaiting ( ) ;
2019-05-14 13:37:20 +02:00
var path = Path . Combine ( Path . GetTempPath ( ) , filename ) ;
2019-04-02 13:43:30 +02:00
_CreatedTempFiles . Add ( path ) ;
2017-12-18 18:22:13 +01:00
if ( ! File . Exists ( path ) )
2017-09-14 12:36:48 +02:00
{
2019-05-14 13:37:20 +02:00
using ( var webClient = new WebClient ( ) )
2017-12-18 18:22:13 +01:00
{
webClient . DownloadFile ( link , path ) ;
}
2017-09-14 12:36:48 +02:00
2017-12-18 18:22:13 +01:00
if ( File . Exists ( path ) )
{
Process . Start ( path ) ;
}
}
else
2017-09-14 12:36:48 +02:00
{
Process . Start ( path ) ;
}
}
2017-12-18 18:22:13 +01:00
catch ( Exception e )
2017-09-14 12:36:48 +02:00
{
2017-12-18 18:22:13 +01:00
}
finally
{
EndWaiting ( ) ;
}
2017-09-14 12:36:48 +02:00
}
2019-05-14 13:37:20 +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 ( )
{
if ( _waitLayer = = null )
{
_waitLayer = new ChatControlWaitLayer ( ) ;
//Grid.SetColumnSpan(_waitLayer, 3);
Grid . SetRowSpan ( _waitLayer , 3 ) ;
GridRechts . Children . Add ( _waitLayer ) ;
Panel . SetZIndex ( _waitLayer , int . MaxValue ) ;
_waitLayer . RefreshChatUI ( ) ;
}
}
public void EndWaiting ( )
{
Dispatcher . BeginInvoke (
DispatcherPriority . Background ,
( Action ) delegate
{
if ( _waitLayer ! = null )
{
GridRechts . Children . Remove ( _waitLayer ) ;
_waitLayer = null ;
}
} ) ;
}
2017-09-20 11:46:00 +02:00
private void Hyperlink_OnRequestNavigate ( object sender , RequestNavigateEventArgs e )
{
Process . Start ( e . Uri . ToString ( ) ) ;
}
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
{
Cursor = Cursors . Wait ;
2019-05-14 13:37:20 +02:00
WriteToNewSyncFile ( ) ;
2017-10-24 11:39:59 +02:00
2019-05-14 13:37:20 +02:00
var aktuelleGruppen = _Chat . UpdateGroups ( ) ;
2017-10-18 10:09:20 +02:00
2019-05-14 13:37:20 +02:00
_ContactList = _Chat . GroupklassenAktuallisieren ( aktuelleGruppen ) ;
2017-10-18 10:09:20 +02:00
2019-05-14 13:37:20 +02:00
ReadNewSyncFile ( ) ;
2017-10-24 11:39:59 +02:00
Clientlist . Items . Refresh ( ) ;
2017-10-20 07:34:26 +02:00
Cursor = Cursors . Arrow ;
2017-10-06 12:08:05 +02:00
}
catch ( Exception e )
{
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
_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
}
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
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
}
}
public List < ChatMessage > GetSelectedMessages ( )
{
var messages = ( from object selectedItem in ChatListBox . SelectedItems select selectedItem as ChatMessage ) . ToList ( ) ;
return messages . Count > 0 ? messages : null ;
}
public CurrentContact GetCurrentContact ( )
{
if ( AktChatUserList ! = null & & AktChatUserList . Items . Count > 0 )
{
var contact = ( CurrentContact ) AktChatUserList . Items [ 0 ] ;
return contact . UserIdManage = = null ? null : contact ;
}
return null ;
}
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
public void DeleteTempFiles ( )
{
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
}
}
}
2017-09-12 14:32:34 +02:00
}
}