public void UploadFile(ConversationMessageModel message, FileModel file) { try { IsBusy = true; _writer.Write(MessageDictionary.UploadFile); if (_reader.ReadByte() == MessageDictionary.OK) { _writer.Write(file.Name); _writer.Write(file.ContentType.MediaType); _writer.Write(file.DateAdded.ToString("dd-MM-yyyy")); SendFile(new MemoryStream(file.Data)); string fileId = _reader.ReadString(); file.Id = fileId; FileUploaded?.Invoke(this, new FileUploadedEventArgs() { Message = message, File = file }); } IsBusy = false; } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); ErrorHelper.LogError(ex); Logout(); } }
public void UploadFiles(ConversationMessageModel message, List <FileModel> files) { foreach (FileModel file in files) { UploadFile(message, file); } }
public ConversationMessageDetails(ConversationMessageModel message) { this.Dispatcher.Invoke(() => { InitializeComponent(); DetailsTable.AddItem("Data: ", message.InitialDate.Value.ToString("dd.MM.yyyy HH:mm")); if (message.Attachments.Count == 0) { AttachmentListContainer.Visibility = Visibility.Collapsed; } else { foreach (FileModel attachment in message.Attachments) { FileListItem listItem = new FileListItem(attachment); listItem.AllowDelete = false; listItem.AllowDownload = false; listItem.AllowRename = false; listItem.AllowSelect = false; AttachmentList.Children.Add(listItem); } } if (message is ConversationEmailMessageModel) { ConversationEmailMessageModel emailMessage = (ConversationEmailMessageModel)message; EmailAddressModel email = message.Author.EmailAddresses.Find(obj => obj.Id == message.AuthorFrom); DetailsTable.AddItem("Od: ", message.Author.FullName + " <" + email.Address + ">"); SubjectLabel.Text = emailMessage.MessageSubject; MessageContainer.Text = emailMessage.MessageContent; } else if (message is ConversationPhoneMessageModel) { ConversationPhoneMessageModel phoneMessage = (ConversationPhoneMessageModel)message; DetailsTable.AddItem("Od: ", message.Author.FullName + " (" + phoneMessage.AuthorPhoneNumber.Number + ")"); DetailsTable.AddItem("Do: ", phoneMessage.Recipient.FullName + " (" + phoneMessage.RecipientPhoneNumber.Number + ")"); SubjectLabelContainer.Visibility = Visibility.Collapsed; if (!phoneMessage.CallAnswered) { CallUnansweredContainer.Visibility = Visibility.Visible; } MessageContainer.Margin = new Thickness(MessageContainer.Margin.Left, 10, MessageContainer.Margin.Right, MessageContainer.Margin.Bottom); MessageContainer.Text = phoneMessage.CallDescription; } CloseButton.PreviewMouseLeftButtonDown += CloseButton_PreviewMouseLeftButtonDown; CloseButton.MouseEnter += CloseButton_MouseEnter; CloseButton.MouseLeave += CloseButton_MouseLeave; }); }
internal ConversationMessage(Api api, ConversationMessageModel model) { _api = api; Id = model.Id; CreatedAt = model.CreatedAt; Body = model.Body; AuthorId = model.AuthorId; Generated = model.Generated; MediaComment = model.MediaComment.ConvertIfNotNull(mc => new MediaComment(api, mc)); ForwardedMessages = model.ForwardedMessages.SelectNotNull(fm => new ConversationMessage(api, fm)); Attachments = model.Attachments.SelectNotNull(a => new FileAttachment(api, a)); }
private void NewEmailButton_Click(object sender, EventArgs e) { ConversationModel conversation = ConversationList.SelectedConversation; if (conversation.Members.Where(obj => !obj.IsInternalUser).All(obj => obj.EmailAddresses.Count == 0)) { Dispatcher.Invoke(() => { _parent.Darkened = true; CustomMessageBox messageBox = new CustomMessageBox( "Nie można wysłać wiadomości. Żaden z członków konwersacji nie posiada adresu e-mail.", MessageBoxButton.OK); messageBox.OKButtonClicked += (s2, ea2) => { _parent.mainCanvas.Children.Remove(messageBox); _parent.Darkened = false; }; _parent.mainCanvas.Children.Add(messageBox); return; }); } if (!_parent.Persons.Find(obj => obj.Id == _parent.Client.UserInfo.PersonId).EmailAddresses.Any()) { Dispatcher.Invoke(() => { _parent.Darkened = true; CustomMessageBox messageBox = new CustomMessageBox( "Nie można utworzyć wiadomości. Twoje konto nie zawiera żadnego adresu e-mail", MessageBoxButton.OK); messageBox.OKButtonClicked += (s2, ea2) => { _parent.mainCanvas.Children.Remove(messageBox); _parent.Darkened = false; }; _parent.mainCanvas.Children.Add(messageBox); }); return; } _parent.Darkened = true; NewEmailMessage newMessage = new NewEmailMessage(conversation, _parent); newMessage.CancelButtonClicked += (s, ea) => { _parent.Darkened = false; _parent.mainCanvas.Children.Remove(newMessage); }; newMessage.ReadyButtonClicked += (s, ea) => { DoWorkEventHandler doWorkHandler = delegate(object se, DoWorkEventArgs ev) { try { this.Dispatcher.Invoke(() => { _parent.StatusBar.StatusText = "Wysyłanie wiadomości"; }); SmtpClient client = _parent.EmailClients.Find(obj => obj.Address == newMessage.SendingAddress) .SmtpClient; MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(newMessage.SendingAddress); mailMessage.Subject = newMessage.OutputSubject; mailMessage.Body = newMessage.Message; foreach (PersonModel recipient in conversation.Members) { if (!recipient.IsInternalUser) { recipient.EmailAddresses.ForEach(obj => mailMessage.To.Add(new MailAddress(obj.Address))); } } foreach (FileModel file in newMessage.Attachments) { mailMessage.Attachments.Add(new Attachment(new MemoryStream(file.Data), file.Name)); } client.Send(mailMessage); ConversationMessageModel message = new ConversationMessageModel( _parent.Persons.Find(obj => obj.Id == _parent.Client.UserInfo.PersonId), DateTime.Now); message.PreviewImage = ImageHelper.GetHtmlImagePreview( newMessage.Message.Replace("\r\n", "<br>"), new Size(600, 60), new Size(600, 250)); message.PreviewImage.Freeze(); ConversationEmailMessageModel emailMessage = new ConversationEmailMessageModel(message, _parent.Persons.Find(obj => obj.Id == _parent.Client.UserInfo.PersonId) .EmailAddresses.Find(obj => obj.Address == newMessage.SendingAddress), newMessage.OutputSubject, newMessage.Message); foreach (FileModel file in newMessage.Attachments) { FileModel newFile = new FileModel(file.Id, file.Name, file.ContentType, file.Size, file.DateAdded) { ConversationId = conversation.Id, Data = file.Data }; emailMessage.Attachments.Add(newFile); } EventHandler <FileUploadedEventArgs> fileUploadedEventHandler = null; fileUploadedEventHandler = delegate(object s2, FileUploadedEventArgs ea2) { if (emailMessage.Attachments.All(obj => obj.Id != null)) { _parent.Client.AddNewMessage(conversation.Id, emailMessage); _parent.UploadClient.FileUploaded -= fileUploadedEventHandler; } }; if (!emailMessage.Attachments.Any(obj => obj.Id == null)) { this.Dispatcher.Invoke(() => { _parent.Client.AddNewMessage(conversation.Id, emailMessage); }); } else { _parent.UploadClient.FileUploaded += fileUploadedEventHandler; _parent.UploadClient.UploadFiles(emailMessage, emailMessage.Attachments.Where(obj => obj.Id == null).ToList()); } this.Dispatcher.Invoke(() => { _parent.StatusBar.StatusText = "Wysyłano wiadomość"; }); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); ErrorHelper.LogError(ex); } }; _sendingWorker.DoWork += doWorkHandler; RunWorkerCompletedEventHandler runWorkerCompleted = null; runWorkerCompleted = delegate(object s2, RunWorkerCompletedEventArgs ev) { _sendingWorker.DoWork -= doWorkHandler; _sendingWorker.RunWorkerCompleted -= runWorkerCompleted; }; _sendingWorker.RunWorkerCompleted += runWorkerCompleted; _sendingWorker.RunWorkerAsync(); _parent.Darkened = false; _parent.mainCanvas.Children.Remove(newMessage); }; _parent.mainCanvas.Children.Add(newMessage); }
private void _newPhoneButton_Click(object sender, EventArgs e) { ConversationModel conversation = ConversationList.SelectedConversation; if (conversation.Members.Where(obj => !obj.IsInternalUser).All(obj => obj.PhoneNumbers.Count == 0)) { Dispatcher.Invoke(() => { _parent.Darkened = true; CustomMessageBox messageBox = new CustomMessageBox( "Nie można utworzyć wiadomości. Żaden z członków konwersacji nie posiada numeru telefonu.", MessageBoxButton.OK); messageBox.OKButtonClicked += (s2, ea2) => { _parent.mainCanvas.Children.Remove(messageBox); _parent.Darkened = false; }; _parent.mainCanvas.Children.Add(messageBox); }); return; } if (!_parent.Persons.Find(obj => obj.Id == _parent.Client.UserInfo.PersonId).PhoneNumbers.Any()) { Dispatcher.Invoke(() => { _parent.Darkened = true; CustomMessageBox messageBox = new CustomMessageBox( "Nie można utworzyć wiadomości. Twoje konto nie zawiera żadnego numeru telefonu", MessageBoxButton.OK); messageBox.OKButtonClicked += (s2, ea2) => { _parent.mainCanvas.Children.Remove(messageBox); _parent.Darkened = false; }; _parent.mainCanvas.Children.Add(messageBox); }); return; } _parent.Darkened = true; NewPhoneConversationMessage newMessage = new NewPhoneConversationMessage(_parent, conversation); _parent.mainCanvas.Children.Add(newMessage); newMessage.ReadyButtonClicked += (s, ea) => { DoWorkEventHandler doWorkHandler = delegate(object se, DoWorkEventArgs ev) { try { this.Dispatcher.Invoke(() => { _parent.StatusBar.StatusText = "Wysyłanie wiadomości"; }); ConversationMessageModel message = new ConversationMessageModel(newMessage.MessageType == PhoneMessageType.ReceivedCall ? newMessage.SelectedPerson : _parent.Persons.Find(obj => obj.Id == _parent.Client.UserInfo.PersonId), DateTime.Now); message.PreviewImage = ImageHelper.GetHtmlImagePreview( newMessage.Message.Replace("\r\n", "<br>"), new Size(600, 60), new Size(600, 250)); message.PreviewImage.Freeze(); ConversationPhoneMessageModel phoneMessage = new ConversationPhoneMessageModel(message, newMessage.MessageType == PhoneMessageType.ReceivedCall ? newMessage.SelectedPhoneNumber : _parent.Persons.Find(obj => obj.Id == _parent.Client.UserInfo.PersonId) .PhoneNumbers.First(), newMessage.MessageType != PhoneMessageType.ReceivedCall ? newMessage.SelectedPhoneNumber : _parent.Persons.Find(obj => obj.Id == _parent.Client.UserInfo.PersonId) .PhoneNumbers.First(), newMessage.Message, newMessage.CallAnswered); foreach (FileModel file in newMessage.Attachments) { FileModel newFile = new FileModel(file.Id, file.Name, file.ContentType, file.Size, file.DateAdded) { ConversationId = conversation.Id, Data = file.Data }; phoneMessage.Attachments.Add(newFile); } EventHandler <FileUploadedEventArgs> fileUploadedEventHandler = null; fileUploadedEventHandler = delegate(object s2, FileUploadedEventArgs ea2) { if (phoneMessage.Attachments.All(obj => obj.Id != null)) { _parent.Client.AddNewMessage(conversation.Id, phoneMessage); _parent.UploadClient.FileUploaded -= fileUploadedEventHandler; } }; if (!phoneMessage.Attachments.Any(obj => obj.Id == null)) { this.Dispatcher.Invoke(() => { _parent.Client.AddNewMessage(conversation.Id, phoneMessage); }); } else { _parent.UploadClient.FileUploaded += fileUploadedEventHandler; _parent.UploadClient.UploadFiles(phoneMessage, phoneMessage.Attachments.Where(obj => obj.Id == null).ToList()); } this.Dispatcher.Invoke(() => { _parent.StatusBar.StatusText = "Wysyłano wiadomość"; }); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); ErrorHelper.LogError(ex); } }; if (newMessage.SelectedPhoneNumber == null) { newMessage.ErrorTextBlock.Text = "Wybierz nadawcę/odbiorcę"; return; } _parent.Darkened = false; _parent.mainCanvas.Children.Remove(newMessage); _sendingWorker.DoWork += doWorkHandler; RunWorkerCompletedEventHandler runWorkerCompleted = null; runWorkerCompleted = delegate(object s2, RunWorkerCompletedEventArgs ev) { _sendingWorker.DoWork -= doWorkHandler; _sendingWorker.RunWorkerCompleted -= runWorkerCompleted; }; _sendingWorker.RunWorkerCompleted += runWorkerCompleted; _sendingWorker.RunWorkerAsync(); _parent.Darkened = false; _parent.mainCanvas.Children.Remove(newMessage); }; newMessage.CancelButtonClicked += (s, ea) => { _parent.Darkened = false; _parent.mainCanvas.Children.Remove(newMessage); }; }
public void Receiver() { try { string conversationId = ""; string id = ""; bool notifyContactPersons = false; string emailId = ""; string emailAddress = ""; while (_client.Connected) { byte response = _reader.ReadByte(); switch (response) { case MessageDictionary.NewNotification: _writer.Write(MessageDictionary.OK); List <string> referenceFields = new List <string>(); string notificationId = _reader.ReadString(); string notificationText = _reader.ReadString(); DateTime notificationDate = DateTime.ParseExact(_reader.ReadString(), "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); int referenceFieldCount = _reader.ReadInt32(); for (int i = 0; i < referenceFieldCount; i++) { referenceFields.Add(_reader.ReadString()); } if (_reader.Read() == MessageDictionary.EndOfMessage) { switch (_reader.ReadByte()) { #region NewMessage case MessageDictionary.NewMessage: id = _reader.ReadString(); conversationId = _reader.ReadString(); string authorId = _reader.ReadString(); string authorFrom = _reader.ReadString(); string initialDateString = _reader.ReadString(); byte[] previewData = ReceiveFile(); List <string> attachmentIds = new List <string>(); int attachmentCount = _reader.ReadInt32(); for (int j = 0; j < attachmentCount; j++) { attachmentIds.Add(_reader.ReadString()); } MemoryStream stream = new MemoryStream(previewData); BitmapImage previewImage = new BitmapImage(); previewImage.BeginInit(); previewImage.StreamSource = stream; previewImage.EndInit(); previewImage.Freeze(); byte type = _reader.ReadByte(); DateTime initialDate = DateTime.ParseExact(initialDateString, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); ConversationMessageModel message = new ConversationMessageModel(id, conversationId, authorId, authorFrom, initialDate, true, previewImage, false, attachmentIds); switch (type) { case MessageDictionary.MessageTypeEmail: string subject = _reader.ReadString(); string content = _reader.ReadString(); ConversationEmailMessageModel emailMessage = new ConversationEmailMessageModel(message, subject, content); message = emailMessage; break; case MessageDictionary.MessageTypePhoneCall: string recipientPhoneNumber = _reader.ReadString(); string description = _reader.ReadString(); bool answered = _reader.ReadBoolean(); ConversationPhoneMessageModel phoneMessage = new ConversationPhoneMessageModel(message, recipientPhoneNumber, description, answered); message = phoneMessage; break; } NewConversationMessageArrived?.Invoke(this, new NewConversationMessageArrivedEventArgs() { Message = message, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RenameConversation case MessageDictionary.RenameConversation: conversationId = _reader.ReadString(); string oldName = _reader.ReadString(); string newName = _reader.ReadString(); ConversationRenamed?.Invoke(this, new ConversationRenamedEventArgs() { ConversationId = conversationId, OldName = oldName, NewName = newName, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RenameFile case MessageDictionary.RenameFile: conversationId = _reader.ReadString(); oldName = _reader.ReadString(); newName = _reader.ReadString(); FileRenamed?.Invoke(this, new FileRenamedEventArgs() { FileId = conversationId, OldName = oldName, NewName = newName, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region AddConversationMembers case MessageDictionary.AddConversationMembers: conversationId = _reader.ReadString(); string personId = _reader.ReadString(); string personColor = _reader.ReadString(); ConversationMembersAdded?.Invoke(this, new ConversationMembersAddedEventArgs() { ConversationId = conversationId, PersonId = personId, PersonColor = personColor, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RemoveConversationMember case MessageDictionary.RemoveConversationMember: conversationId = _reader.ReadString(); personId = _reader.ReadString(); ConversationMemberRemoved?.Invoke(this, new ConversationMemberRemovedEventArgs() { ConversationId = conversationId, PersonId = personId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region ConversationSettingsChanged case MessageDictionary.ChangeConversationSettings: conversationId = _reader.ReadString(); notifyContactPersons = _reader.ReadBoolean(); ConversationSettingsChanged?.Invoke(this, new ConversationSettingsChangedEventArgs() { ConversationId = conversationId, NotifyContactPersons = notifyContactPersons, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region NewCompany case MessageDictionary.NewCompany: string companyId = _reader.ReadString(); string companyName = _reader.ReadString(); NewCompanyArrived?.Invoke(this, new NewCompanyEventArgs() { Company = new CompanyModel(companyId, companyName), Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RenameCompany case MessageDictionary.RenameCompany: companyId = _reader.ReadString(); newName = _reader.ReadString(); CompanyRenamed?.Invoke(this, new CompanyRenamedEventArgs() { CompanyId = companyId, NewName = newName, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RemoveCompanyEvent case MessageDictionary.RemoveCompany: companyId = _reader.ReadString(); CompanyRemoved?.Invoke(this, new CompanyRemovedEventArgs() { CompanyId = companyId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region UpdatePersonDetails case MessageDictionary.UpdatePersonDetails: string PersonId = _reader.ReadString(); string PersonFirstName = _reader.ReadString(); string PersonLastName = _reader.ReadString(); string PersonGenderCode = _reader.ReadString(); string PersonCompanyId = _reader.ReadString(); Gender PersonGender = (Gender)Convert.ToInt32(PersonGenderCode); PersonModel contactPerson = new PersonModel(PersonId, PersonFirstName, PersonLastName, PersonGender, PersonCompanyId, true); int emailAddressCount = _reader.ReadInt32(); for (int j = 0; j < emailAddressCount; j++) { emailId = _reader.ReadString(); string emailName = _reader.ReadString(); emailAddress = _reader.ReadString(); EmailAddressModel emailAddressModel = new EmailAddressModel(emailId, emailAddress, emailName); contactPerson.EmailAddresses.Add(emailAddressModel); } int phoneNumberCount = _reader.ReadInt32(); for (int j = 0; j < phoneNumberCount; j++) { string phoneNumberId = _reader.ReadString(); string phoneName = _reader.ReadString(); string phoneNumber = _reader.ReadString(); PhoneNumberModel phoneNumberModel = new PhoneNumberModel(phoneNumberId, phoneNumber, phoneName); contactPerson.PhoneNumbers.Add(phoneNumberModel); } ContactDetailsUpdated?.Invoke(this, new ContactDetailsUpdatedEventArgs() { NewData = contactPerson, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region NewExternalContact case MessageDictionary.NewExternalContact: _writer.Write(MessageDictionary.OK); PersonId = _reader.ReadString(); PersonFirstName = _reader.ReadString(); PersonLastName = _reader.ReadString(); PersonGenderCode = _reader.ReadString(); PersonCompanyId = _reader.ReadString(); PersonGender = (Gender)Convert.ToInt32(PersonGenderCode); contactPerson = new PersonModel(PersonId, PersonFirstName, PersonLastName, PersonGender, PersonCompanyId, true); emailAddressCount = _reader.ReadInt32(); for (int j = 0; j < emailAddressCount; j++) { emailId = _reader.ReadString(); string emailName = _reader.ReadString(); emailAddress = _reader.ReadString(); EmailAddressModel emailAddressModel = new EmailAddressModel(emailId, emailAddress, emailName); contactPerson.EmailAddresses.Add(emailAddressModel); } phoneNumberCount = _reader.ReadInt32(); for (int j = 0; j < phoneNumberCount; j++) { string phoneNumberId = _reader.ReadString(); string phoneName = _reader.ReadString(); string phoneNumber = _reader.ReadString(); PhoneNumberModel phoneNumberModel = new PhoneNumberModel(phoneNumberId, phoneNumber, phoneName); contactPerson.PhoneNumbers.Add(phoneNumberModel); } NewExternalContact?.Invoke(this, new NewExternalContactEventArgs() { NewData = contactPerson, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region NewExternalContact case MessageDictionary.RemoveExternalContact: personId = _reader.ReadString(); ExternalContactRemoved?.Invoke(this, new ExternalContactRemovedEventArgs() { PersonId = personId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RemoveConversation case MessageDictionary.RemoveConversation: _writer.Write(MessageDictionary.OK); conversationId = _reader.ReadString(); ConversationRemoved?.Invoke(this, new ConversationRemovedEventArgs() { ConversationId = conversationId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion } } break; #region NewFiles case MessageDictionary.NewFiles: List <FileModel> files = new List <FileModel>(); files.Clear(); _writer.Write(MessageDictionary.OK); int fileCount = _reader.ReadInt32(); for (int i = 0; i < fileCount; i++) { conversationId = _reader.ReadString(); id = _reader.ReadString(); string name = _reader.ReadString(); string contentType = _reader.ReadString(); long size = _reader.ReadInt64(); string dateAdded = _reader.ReadString(); FileModel file = new FileModel(id, name, new ContentType(contentType), size, DateTime.ParseExact(dateAdded, "dd-MM-yyyy", CultureInfo.InvariantCulture)) { ConversationId = conversationId }; files.Add(file); } NewFilesArrived?.Invoke(this, new NewFilesArrivedEventArgs() { Files = files }); break; #endregion #region NewConversation case MessageDictionary.NewConversation: _writer.Write(MessageDictionary.OK); conversationId = _reader.ReadString(); string conversationName = _reader.ReadString(); string visibleId = _reader.ReadString(); string dateCreatedString = _reader.ReadString(); notifyContactPersons = _reader.ReadBoolean(); DateTime dateCreated = DateTime.ParseExact(dateCreatedString, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); int memberCount = _reader.ReadInt32(); List <string> memberIds = new List <string>(); List <string> memberColors = new List <string>(); for (int j = 0; j < memberCount; j++) { string memberId = _reader.ReadString(); string memberColor = _reader.ReadString(); memberIds.Add(memberId); memberColors.Add(memberColor); } List <ConversationMessageModel> messages = new List <ConversationMessageModel>(); if (_reader.Read() == MessageDictionary.OK) { int messageCount = _reader.ReadInt32(); for (int i = 0; i < messageCount; i++) { id = _reader.ReadString(); string authorId = _reader.ReadString(); string authorFrom = _reader.ReadString(); string initialDateString = _reader.ReadString(); Boolean received = _reader.ReadBoolean(); byte[] previewData = ReceiveFile(); List <string> attachmentIds = new List <string>(); int attachmentCount = _reader.ReadInt32(); for (int j = 0; j < attachmentCount; j++) { attachmentIds.Add(_reader.ReadString()); } MemoryStream stream = new MemoryStream(previewData); BitmapImage previewImage = new BitmapImage(); previewImage.BeginInit(); previewImage.StreamSource = stream; previewImage.EndInit(); previewImage.Freeze(); DateTime initialDate = DateTime.ParseExact(initialDateString, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); ConversationMessageModel message = new ConversationMessageModel(id, conversationId, authorId, authorFrom, initialDate, received, previewImage, false, attachmentIds); byte type = _reader.ReadByte(); if (type == MessageDictionary.MessageTypeEmail) { string subject = _reader.ReadString(); string content = _reader.ReadString(); ConversationEmailMessageModel emailMessage = new ConversationEmailMessageModel(message, subject, content); message = emailMessage; } else if (type == MessageDictionary.MessageTypePhoneCall) { string recipientPhoneNumber = _reader.ReadString(); string callDescription = _reader.ReadString(); bool callAnswered = _reader.ReadBoolean(); ConversationPhoneMessageModel phoneMessage = new ConversationPhoneMessageModel(message, recipientPhoneNumber, callDescription, callAnswered); message = phoneMessage; } if (message != null) { messages.Add(message); } } if (_reader.Read() == MessageDictionary.EndOfMessage) { ConversationModel conversation = new ConversationModel(conversationId, conversationName, visibleId, dateCreated, memberIds, memberColors, notifyContactPersons); messages.ForEach(obj => conversation.AddMessage(obj)); NewConversationArrived?.Invoke(this, new NewConversationArrivedEventArgs() { Conversation = conversation }); } } break; #endregion #region RemoveConversation case MessageDictionary.RemoveConversation: _writer.Write(MessageDictionary.OK); conversationId = _reader.ReadString(); ConversationRemoved?.Invoke(this, new ConversationRemovedEventArgs() { ConversationId = conversationId }); break; #endregion #region NewEmailAddress case MessageDictionary.AddEmailAddress: _writer.Write(MessageDictionary.OK); emailId = _reader.ReadString(); emailAddress = _reader.ReadString(); string login = _reader.ReadString(); string imapHost = _reader.ReadString(); int imapPort = _reader.ReadInt32(); bool imapUseSel = _reader.ReadBoolean(); string smtpHost = _reader.ReadString(); int smtpPort = _reader.ReadInt32(); bool smtpUseSsl = _reader.ReadBoolean(); string addressName = _reader.ReadString(); NewEmailAddress?.Invoke(this, new NewEmailAddressEventArgs() { Id = emailId, Login = login, Address = emailAddress, ImapHost = imapHost, ImapPort = imapPort, ImapUseSsl = imapUseSel, SmtpHost = smtpHost, SmtpPort = smtpPort, SmtpUseSsl = smtpUseSsl, Name = addressName }); break; #endregion } } } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); ErrorHelper.LogError(ex); Logout(); } }
private void _processingWorker_DoWork(object sender, DoWorkEventArgs e) { while (_unprocessedMessages.Any()) { Tuple <string, Message> messageTuple = _unprocessedMessages.First(); string conversationId = messageTuple.Item1; Message message = messageTuple.Item2; PersonModel messageSender = _parent.Persons.Find( obj => obj.EmailAddresses.Any(obj2 => obj2.Address == message.From.Address)); EmailAddressModel authorEmailAddress = messageSender.EmailAddresses.Single(obj => obj.Address == message.From.Address); ConversationMessageModel receivedMessage = new ConversationMessageModel(messageSender, message.InternalDate) { ConversationId = conversationId }; ConversationEmailMessageModel emailMessage = new ConversationEmailMessageModel(receivedMessage, authorEmailAddress, message.Subject, message.Body.Html == "" ? message.Body.Text : message.Body.Html); this.Dispatcher.Invoke(() => { BitmapSource previewImage = ImageHelper.GetHtmlImagePreview( message.Body.Html == "" ? message.Body.Text : message.Body.Html, new Size(600, 60), new Size(600, 250)); previewImage.Freeze(); emailMessage.PreviewImage = previewImage; }); if (message.Attachments.Any()) { _parent.AwaitingMessages.Add(emailMessage); foreach (Attachment attachment in message.Attachments) { if (!attachment.Downloaded) { attachment.Download(); } FileModel file = new FileModel(attachment, DateTime.Today); emailMessage.Attachments.Add(file); } _parent.UploadClient.UploadFiles(emailMessage, emailMessage.Attachments); } else { _parent.Client.AddNewMessage(conversationId, emailMessage); } _unprocessedMessages.Remove(messageTuple); } }
public int AddNewMessage(string conversationId, ConversationMessageModel message) { try { Stream previewImageData = null; if (message.PreviewImage != null) { PngBitmapEncoder pngFile = new PngBitmapEncoder(); pngFile.Frames.Add((BitmapFrame)message.PreviewImage); MemoryStream stream = new MemoryStream(); pngFile.Save(stream); previewImageData = stream; } _writer.Write(MessageDictionary.NewMessage); if (_reader.Read() == MessageDictionary.OK) { _writer.Write(conversationId); _writer.Write(message.AuthorId); _writer.Write(message.Attachments.Count); foreach (FileModel attachment in message.Attachments) { _writer.Write(attachment.Id); } if (message is ConversationEmailMessageModel) { ConversationEmailMessageModel emailMessage = (ConversationEmailMessageModel)message; _writer.Write(MessageDictionary.MessageTypeEmail); if (_reader.Read() == MessageDictionary.OK) { _writer.Write(emailMessage.AuthorEmailaddress.Id); _writer.Write(emailMessage.InitialDate.Value.ToString("yyyy-MM-dd HH:mm")); _writer.Write(emailMessage.MessageSubject); _writer.Write(emailMessage.MessageContent); SendFile(previewImageData); if (_reader.Read() != MessageDictionary.OK) { throw new Exception("Connection unsynced"); } return(1); } throw new Exception("Connection unsynced"); } if (message is ConversationPhoneMessageModel) { ConversationPhoneMessageModel phoneMessage = (ConversationPhoneMessageModel)message; _writer.Write(MessageDictionary.MessageTypePhoneCall); _writer.Write(phoneMessage.AuthorPhoneNumber.Id); _writer.Write(phoneMessage.RecipientPhoneNumber.Id); _writer.Write(phoneMessage.InitialDate.Value.ToString("yyyy-MM-dd HH:mm")); _writer.Write(phoneMessage.CallDescription); _writer.Write(phoneMessage.CallAnswered); SendFile(previewImageData); if (_reader.Read() != MessageDictionary.OK) { throw new Exception("Connection unsynced"); } return(1); } throw new Exception("Connection unsynced"); } throw new Exception("Connection unsynced"); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); ErrorHelper.LogError(ex); Logout(); return(0); } }
public List <ConversationMessageModel> GetConversationMessages(string conversationId) { try { List <ConversationMessageModel> messages = new List <ConversationMessageModel>(); _writer.Write(MessageDictionary.GetConversationMessages); if (_reader.Read() == MessageDictionary.OK) { _writer.Write(conversationId); if (_reader.Read() == MessageDictionary.OK) { int messageCount = _reader.ReadInt32(); for (int i = 0; i < messageCount; i++) { string id = _reader.ReadString(); string authorId = _reader.ReadString(); string authorFrom = _reader.ReadString(); string initialDateString = _reader.ReadString(); Boolean received = _reader.ReadBoolean(); byte[] previewData = ReceiveFile(); List <string> attachmentIds = new List <string>(); int attachmentCount = _reader.ReadInt32(); for (int j = 0; j < attachmentCount; j++) { attachmentIds.Add(_reader.ReadString()); } MemoryStream stream = new MemoryStream(previewData); BitmapImage previewImage = new BitmapImage(); previewImage.BeginInit(); previewImage.StreamSource = stream; previewImage.EndInit(); previewImage.Freeze(); DateTime initialDate = DateTime.ParseExact(initialDateString, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); ConversationMessageModel message = new ConversationMessageModel(id, conversationId, authorId, authorFrom, initialDate, received, previewImage, false, attachmentIds); byte type = _reader.ReadByte(); if (type == MessageDictionary.MessageTypeEmail) { string subject = _reader.ReadString(); string content = _reader.ReadString(); ConversationEmailMessageModel emailMessage = new ConversationEmailMessageModel(message, subject, content); message = emailMessage; } else if (type == MessageDictionary.MessageTypePhoneCall) { string recipientPhoneNumberId = _reader.ReadString(); string callDescription = _reader.ReadString(); bool callAnswered = _reader.ReadBoolean(); ConversationPhoneMessageModel phoneMessage = new ConversationPhoneMessageModel(message, recipientPhoneNumberId, callDescription, callAnswered); message = phoneMessage; } if (message != null) { messages.Add(message); } } if (_reader.Read() == MessageDictionary.EndOfMessage) { return(messages); } throw new Exception("Connection unsynced"); } throw new Exception("Connection unsynced"); } throw new Exception("Connection unsynced"); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); ErrorHelper.LogError(ex); Logout(); return(null); } }