Chat.xaml.cs 19.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
Chat.xaml.cs
Copyright (C) 2015  Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15 16 17 18 19 20 21 22 23 24 25 26 27
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Linphone.Model;
using System.Diagnostics;
using Linphone.Controls;
using System.Net.Http;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Core;
Margaux Clerc's avatar
Margaux Clerc committed
28 29 30 31 32 33 34 35
using System.IO;
using Windows.UI.Popups;
using Windows.ApplicationModel.Resources;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using System.Threading.Tasks;
Erwan Croze's avatar
Erwan Croze committed
36
using Linphone;
37

Erwan Croze's avatar
Erwan Croze committed
38 39
namespace Linphone.Views {
    public interface MessageReceivedListener {
40 41 42 43
        void MessageReceived(ChatMessage message);
        string GetSipAddressAssociatedWithDisplayConversation();
    }

Erwan Croze's avatar
Erwan Croze committed
44
    public interface ComposingReceivedListener {
45 46 47 48
        void ComposeReceived();
        string GetSipAddressAssociatedWithDisplayConversation();
    }

Erwan Croze's avatar
Erwan Croze committed
49
    public partial class Chat : Page {
50 51
        private const int SENT_IMAGES_QUALITY = 50;

Erwan Croze's avatar
Erwan Croze committed
52 53 54
        private HttpClient _httpPostClient {
            get; set;
        }
55 56 57

        public Address sipAddress;
        public ChatRoom chatRoom;
Margaux Clerc's avatar
Margaux Clerc committed
58 59
        private String upload_filename;
        private ChatMessage messageUploading = null;
60

Erwan Croze's avatar
Erwan Croze committed
61
        public Chat() {
62
            this.InitializeComponent();
Erwan Croze's avatar
Erwan Croze committed
63
            chatListenerInit();
Margaux Clerc's avatar
Margaux Clerc committed
64
            MessageBox.SendFileClick += send_file;
Margaux Clerc's avatar
Margaux Clerc committed
65
            MessageBox.SendMessageClick += send_message;
Erwan Croze's avatar
Erwan Croze committed
66
            scrollToBottom();
67 68 69
            SystemNavigationManager.GetForCurrentView().BackRequested += Back_requested;
        }

Erwan Croze's avatar
Erwan Croze committed
70 71 72 73 74 75 76 77
        private void chatListenerInit() {
            CoreListener listener = LinphoneManager.Instance.getCoreListener();
            if (listener == null)
                return;
            listener.OnMessageReceived = this.MessageReceived;
            listener.OnIsComposingReceived = this.ComposeReceived;
        }

Erwan Croze's avatar
Erwan Croze committed
78 79
        private void Back_requested(object sender, BackRequestedEventArgs e) {
            if (Frame.CanGoBack) {
80
                Frame.GoBack();
Erwan Croze's avatar
Erwan Croze committed
81
            } else {
82 83 84
                Frame.Navigate(typeof(Views.Chats), null);
            }
            e.Handled = true;
85 86
        }

Erwan Croze's avatar
Erwan Croze committed
87
        protected override void OnNavigatedTo(NavigationEventArgs e) {
88
            base.OnNavigatedTo(e);
Erwan Croze's avatar
Erwan Croze committed
89
            
90

Margaux Clerc's avatar
Margaux Clerc committed
91 92
            //ContactManager cm = ContactManager.Instance;
            //cm.ContactFound += cm_ContactFound;
93
            
94 95 96 97
            MessageBox.TextChanged += MessageBox_TextChanged;

            NewChat.Visibility = Visibility.Collapsed;
            ContactName.Visibility = Visibility.Visible;
Erwan Croze's avatar
Erwan Croze committed
98
            if (e.Parameter is String) {
Erwan Croze's avatar
Erwan Croze committed
99
                sipAddress = LinphoneManager.Instance.Core.InterpretUrl(e.Parameter as String);
100 101
                CreateChatRoom(sipAddress);
                UpdateComposingMessage();
Margaux Clerc's avatar
Margaux Clerc committed
102
                chatRoom.MarkAsRead();
Erwan Croze's avatar
Erwan Croze committed
103
                DisplayPastMessagesAsync(chatRoom.GetHistory(chatRoom.HistorySize));
104
            }
Erwan Croze's avatar
Erwan Croze committed
105
            if (sipAddress == null) {
Margaux Clerc's avatar
Margaux Clerc committed
106
                ContactName.Visibility = Visibility.Collapsed;
107 108 109 110 111
                NewChat.Visibility = Visibility.Visible;
            }
            scrollToBottom();
        }

Margaux Clerc's avatar
Margaux Clerc committed
112
        #region Events
Erwan Croze's avatar
Erwan Croze committed
113
        private void MessageBox_TextChanged(object sender, string text) {
114 115 116 117
            if (chatRoom != null && text.Length > 0)
                chatRoom.Compose();
        }

Erwan Croze's avatar
Erwan Croze committed
118 119
        private void Call_Click(object sender, RoutedEventArgs e) {
            if (chatRoom == null) {
Erwan Croze's avatar
Erwan Croze committed
120
                LinphoneManager.Instance.NewOutgoingCall(LinphoneManager.Instance.Core.InterpretUrl(NewChatSipAddress.Text).AsStringUriOnly());
Erwan Croze's avatar
Erwan Croze committed
121
            } else {
Margaux Clerc's avatar
Margaux Clerc committed
122 123 124 125
                LinphoneManager.Instance.NewOutgoingCall(chatRoom.PeerAddress.AsStringUriOnly());
            }
        }

Erwan Croze's avatar
Erwan Croze committed
126
        private void Cancel_Upload(object sender, RoutedEventArgs e) {
Margaux Clerc's avatar
Margaux Clerc committed
127 128 129 130
            if (_httpPostClient != null)
                _httpPostClient.CancelPendingRequests();
        }

Erwan Croze's avatar
Erwan Croze committed
131 132
        private void SendMessage(string message) {
            if (chatRoom != null) {
Margaux Clerc's avatar
Margaux Clerc committed
133
                ChatMessage chatMessage = chatRoom.CreateMessage(message);
134
                chatMessage.Listener.OnMsgStateChanged = MessageStateChanged;
Erwan Croze's avatar
Erwan Croze committed
135
                chatRoom.SendChatMessage(chatMessage);
Margaux Clerc's avatar
Margaux Clerc committed
136
            }
137 138
        }

Erwan Croze's avatar
Erwan Croze committed
139
        private void ChooseContact_Click(object sender, RoutedEventArgs e) {
Margaux Clerc's avatar
Margaux Clerc committed
140 141 142 143 144
            Frame.Navigate(typeof(Views.ContactList), null);
        }

        #endregion

Erwan Croze's avatar
Erwan Croze committed
145
        private async void DisplayPastMessagesAsync(IEnumerable<ChatMessage> messages) {
Erwan Croze's avatar
Erwan Croze committed
146 147
            foreach (ChatMessage message in messages) {
                if (!message.IsOutgoing) {
148 149 150
                    IncomingChatBubble bubble = new IncomingChatBubble(message);
                    bubble.MessageDeleted += bubble_MessageDeleted;
                    bubble.DownloadImage += bubble_DownloadImage;
Margaux Clerc's avatar
Margaux Clerc committed
151
                    bubble.ImageTapped += Bubble_ImageTapped;
Erwan Croze's avatar
Erwan Croze committed
152
                    bubble.RefreshImage();
Margaux Clerc's avatar
Margaux Clerc committed
153
                    MessagesList.Children.Add(bubble);
Erwan Croze's avatar
Erwan Croze committed
154
                } else {
Erwan Croze's avatar
Erwan Croze committed
155 156 157 158 159 160
                    if (!message.IsText && message.FileTransferInformation != null) {
                        var tempFolder = ApplicationData.Current.LocalFolder;
                        string name = message.FileTransferInformation.Name;
                        StorageFile tempFile = await tempFolder.GetFileAsync(name.Substring(0, name.IndexOf('.')));
                        message.FileTransferFilepath = tempFile.Path;
                    }
161 162 163
                    OutgoingChatBubble bubble = new OutgoingChatBubble(message);
                    bubble.MessageDeleted += bubble_MessageDeleted;
                    bubble.UpdateStatus(message.State);
Erwan Croze's avatar
Erwan Croze committed
164
                    bubble.RefreshImage();
Margaux Clerc's avatar
Margaux Clerc committed
165
                    MessagesList.Children.Add(bubble);
166 167 168 169
                }
            }
        }

Erwan Croze's avatar
Erwan Croze committed
170
        private void Bubble_ImageTapped(object sender, string appData) {
Margaux Clerc's avatar
Margaux Clerc committed
171 172 173
            Frame.Navigate(typeof(Views.FullScreenPicture), appData);
        }

Erwan Croze's avatar
Erwan Croze committed
174
        private void bubble_MessageDeleted(object sender, ChatMessage message) {
Margaux Clerc's avatar
Margaux Clerc committed
175
            MessagesList.Children.Remove(sender as UserControl);
Erwan Croze's avatar
Erwan Croze committed
176
            if (chatRoom != null) {
Margaux Clerc's avatar
Margaux Clerc committed
177 178
                chatRoom.DeleteMessage(message);
            }
Margaux Clerc's avatar
Margaux Clerc committed
179 180
        }

Erwan Croze's avatar
Erwan Croze committed
181
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) {
182
            chatRoom = null;
Margaux Clerc's avatar
Margaux Clerc committed
183
            upload_filename = "";
Margaux Clerc's avatar
Margaux Clerc committed
184 185
            LinphoneManager.Instance.MessageListener = null;
            LinphoneManager.Instance.ComposingListener = null;
Margaux Clerc's avatar
Margaux Clerc committed
186
            MessageBox.TextChanged -= MessageBox_TextChanged;
187 188 189
            base.OnNavigatingFrom(e);
        }

Margaux Clerc's avatar
Margaux Clerc committed
190 191 192 193 194 195 196
        /*  private void cm_ContactFound(object sender, ContactFoundEventArgs e)
          {
              if (e.ContactFound != null)
              {
                  ContactName.Text = e.ContactFound.DisplayName;
                  ContactManager.Instance.TempContact = e.ContactFound;
                  ContactName.Tap += ContactName_Tap;
197

Margaux Clerc's avatar
Margaux Clerc committed
198 199 200 201
                  ContactName.Visibility = Visibility.Visible;
                  NewChat.Visibility = Visibility.Collapsed;
              }
          }*/
202

Margaux Clerc's avatar
Margaux Clerc committed
203 204 205 206
        /* private void ContactName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
         {
             NavigationService.Navigate(new Uri("/Views/Contact.xaml", UriKind.RelativeOrAbsolute));
         }*/
207 208


Erwan Croze's avatar
Erwan Croze committed
209 210 211
        public void MessageStateChanged(ChatMessage message, ChatMessageState state) {
            if (LinphoneManager.Instance.CoreDispatcher == null)
                return;
212
#pragma warning disable CS4014 // Dans la mesure où cet appel n'est pas attendu, l'exécution de la méthode actuelle continue avant la fin de l'appel
213
            LinphoneManager.Instance.CoreDispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => {
Erwan Croze's avatar
Erwan Croze committed
214
                if (ProgressPopup.Visibility == Visibility.Visible) {
Margaux Clerc's avatar
Margaux Clerc committed
215 216 217 218
                    ProgressPopup.Visibility = Visibility.Collapsed;
                    MessageBox.Visibility = Visibility.Visible;
                }

Erwan Croze's avatar
Erwan Croze committed
219 220
                if (state == ChatMessageState.Delivered) {
                    if (messageUploading != null && messageUploading.Equals(message)) {
Margaux Clerc's avatar
Margaux Clerc committed
221 222 223 224
                        messageUploading = null;
                    }
                }

Erwan Croze's avatar
Erwan Croze committed
225
                if (state == ChatMessageState.InProgress && message.IsOutgoing && messageUploading == null) {
Margaux Clerc's avatar
Margaux Clerc committed
226
                    // Create the chat bubble for both text or image messages
Erwan Croze's avatar
Erwan Croze committed
227
                    if (message.Appdata != null && messageUploading == null) {
Margaux Clerc's avatar
Margaux Clerc committed
228 229 230 231 232 233
                        messageUploading = message;
                    }
                    OutgoingChatBubble bubble = new OutgoingChatBubble(message);
                    bubble.MessageDeleted += bubble_MessageDeleted;
                    MessagesList.Children.Add(bubble);
                    scrollToBottom();
Erwan Croze's avatar
Erwan Croze committed
234 235
                } else if (state == ChatMessageState.FileTransferDone && !message.IsOutgoing) {
                    try {
Margaux Clerc's avatar
Margaux Clerc committed
236
                        IncomingChatBubble bubble = (IncomingChatBubble)MessagesList.Children.OfType<IncomingChatBubble>().Where(b => message.Equals(((IncomingChatBubble)b).ChatMessage)).Last();
Erwan Croze's avatar
Erwan Croze committed
237
                        if (bubble != null) {
238
                            ((IncomingChatBubble)bubble).ChatMessage.FileTransferFilepath = message.FileTransferFilepath;
Margaux Clerc's avatar
Margaux Clerc committed
239 240 241
                            ((IncomingChatBubble)bubble).RefreshImage();
                        }
                        EnableDownloadButtons(true);
242 243 244
                    } catch {
                        Debug.WriteLine("Cannot create load download image");
                    }
Erwan Croze's avatar
Erwan Croze committed
245 246 247 248 249 250 251 252 253 254
                } else if (state == ChatMessageState.FileTransferDone && message.IsOutgoing) {
                    try {
                        OutgoingChatBubble bubble = (OutgoingChatBubble)MessagesList.Children.OfType<OutgoingChatBubble>().Where(b => message.Equals(((OutgoingChatBubble)b).ChatMessage)).Last();
                        if (bubble != null) {
                            ((OutgoingChatBubble)bubble).ChatMessage.FileTransferFilepath = message.FileTransferFilepath;
                            ((OutgoingChatBubble)bubble).RefreshImage();
                        }
                    } catch {
                        Debug.WriteLine("Cannot load uploaded image");
                    }
Erwan Croze's avatar
Erwan Croze committed
255 256 257 258
                } else {
                    try {
                        foreach (OutgoingChatBubble bubble in MessagesList.Children.OfType<OutgoingChatBubble>()) {
                            if (bubble.ChatMessage.Equals(message)) {
Margaux Clerc's avatar
Margaux Clerc committed
259
                                bubble.UpdateStatus(state);
Margaux Clerc's avatar
Margaux Clerc committed
260 261
                            }
                        }
262 263 264
                    } catch {
                        Debug.WriteLine("Cannot update message state");
                    }
Margaux Clerc's avatar
Margaux Clerc committed
265 266
                }

Erwan Croze's avatar
Erwan Croze committed
267
                if (chatRoom != null) {
Margaux Clerc's avatar
Margaux Clerc committed
268 269 270 271
                    chatRoom.MarkAsRead();
                }
            });
        }
272

Erwan Croze's avatar
Erwan Croze committed
273
        private void CreateChatRoom(Address sipAddress) {
274 275
            this.sipAddress = sipAddress;
            //ContactManager.Instance.FindContact(String.Format("{0}@{1}", sipAddress.UserName, sipAddress.Domain));
Erwan Croze's avatar
Erwan Croze committed
276
            ContactName.Text = sipAddress.Username;
277 278 279
            ContactName.Visibility = Visibility.Visible;
            NewChat.Visibility = Visibility.Collapsed;

Erwan Croze's avatar
Erwan Croze committed
280
            try {
281
                chatRoom = LinphoneManager.Instance.Core.GetChatRoom(sipAddress);
Erwan Croze's avatar
Erwan Croze committed
282
            } catch {
283 284 285 286 287
                Debug.WriteLine("Cannot create chatroom");
                throw;
            }
        }

Erwan Croze's avatar
Erwan Croze committed
288 289 290
        private void send_message(object sender) {
            if (NewChatSipAddress.Text != null || NewChatSipAddress.Visibility == Visibility.Collapsed) {
                if (chatRoom == null) {
Erwan Croze's avatar
Erwan Croze committed
291
                    CreateChatRoom(LinphoneManager.Instance.Core.InterpretUrl(NewChatSipAddress.Text));
Margaux Clerc's avatar
Margaux Clerc committed
292
                }
293

Erwan Croze's avatar
Erwan Croze committed
294 295
                if (chatRoom != null) {
                    if (MessageBox.Text != null && MessageBox.Text.Length > 0) {
296
                        SendMessage(MessageBox.Text);
Erwan Croze's avatar
Erwan Croze committed
297
                    } else if (MessageBox.ImageName != null && MessageBox.ImageLocalPath != null) {
298
                        InitiateImageUpload(MessageBox.ImageLocalPath, MessageBox.ImageName, MessageBox.ImageType);
Erwan Croze's avatar
Erwan Croze committed
299
                    } else {
300 301
                        Debug.WriteLine("No text :)");
                    }
Margaux Clerc's avatar
Margaux Clerc committed
302
                    MessageBox.Reset();
Erwan Croze's avatar
Erwan Croze committed
303
                } else {
Margaux Clerc's avatar
Margaux Clerc committed
304
                    //TODO error message
Margaux Clerc's avatar
Margaux Clerc committed
305 306
                }
            }
307 308
        }

Erwan Croze's avatar
Erwan Croze committed
309
        private async void send_file(object sender) {
Margaux Clerc's avatar
Margaux Clerc committed
310 311 312 313 314 315 316 317
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            StorageFile file = await openPicker.PickSingleFileAsync();

Erwan Croze's avatar
Erwan Croze committed
318
            if (file != null) {
Margaux Clerc's avatar
Margaux Clerc committed
319
                imageSelectionTask_Completed(file);
Erwan Croze's avatar
Erwan Croze committed
320
            } else {
Margaux Clerc's avatar
Margaux Clerc committed
321 322
                //TODO error message
            }
Margaux Clerc's avatar
Margaux Clerc committed
323 324
        }

Erwan Croze's avatar
Erwan Croze committed
325 326
        private void back_click(object sender, RoutedEventArgs e) {
            if (Frame.CanGoBack) {
327 328 329 330
                Frame.GoBack();
            }
        }

331
        private async void InitiateImageUpload(string filePath, string fileName, string fileType) {
332

Margaux Clerc's avatar
Margaux Clerc committed
333 334
            if (chatRoom == null) //This code will be executed only in case of new conversation
            {
Erwan Croze's avatar
Erwan Croze committed
335
                CreateChatRoom(LinphoneManager.Instance.Core.InterpretUrl(NewChatSipAddress.Text));
Margaux Clerc's avatar
Margaux Clerc committed
336
            }
Erwan Croze's avatar
Erwan Croze committed
337
            if (chatRoom != null) {
Margaux Clerc's avatar
Margaux Clerc committed
338 339 340
                ProgressPopup.Visibility = Visibility.Visible;
                MessageBox.Visibility = Visibility.Collapsed;
                //CancelUpload.Visibility = Visibility.Visible;
Erwan Croze's avatar
Erwan Croze committed
341
                Task.Run(() => {
Margaux Clerc's avatar
Margaux Clerc committed
342
                    FileInfo fileInfo;
Erwan Croze's avatar
Erwan Croze committed
343
                    try {
Margaux Clerc's avatar
Margaux Clerc committed
344
                        fileInfo = new FileInfo(filePath);
Erwan Croze's avatar
Erwan Croze committed
345
                        Content content = LinphoneManager.Instance.Core.CreateContent();
346 347 348
                        content.Type = "image";
                        content.Subtype = fileType;
                        content.Name = fileName+ "." + fileType;
Erwan Croze's avatar
Erwan Croze committed
349 350 351
                        content.Size = (int)fileInfo.Length;
                        ChatMessage msg = chatRoom.CreateFileTransferMessage(content);
                        msg.Appdata = fileName;
352
                        msg.FileTransferFilepath = filePath;
Erwan Croze's avatar
Erwan Croze committed
353
                        msg.Listener.OnMsgStateChanged = MessageStateChanged;
Erwan Croze's avatar
Erwan Croze committed
354
                        chatRoom.SendChatMessage(msg);
Erwan Croze's avatar
Erwan Croze committed
355
                    } catch (Exception e) {
356
                        Debug.WriteLine("Cannot upload image: " + e);
Margaux Clerc's avatar
Margaux Clerc committed
357 358
                    }
                });
359

Erwan Croze's avatar
Erwan Croze committed
360
            } else {
Margaux Clerc's avatar
Margaux Clerc committed
361 362 363 364 365 366 367 368 369
                ProgressPopup.Visibility = Visibility.Collapsed;
                MessageBox.Visibility = Visibility.Visible;
                var messageDialog = new MessageDialog(ResourceLoader.GetForCurrentView().GetString("ChatRoomCreationError"), ResourceLoader.GetForCurrentView().GetString("GenericError"));
                await messageDialog.ShowAsync();
                //CancelUpload.Visibility = Visibility.Collapsed;
            }

        }

Erwan Croze's avatar
Erwan Croze committed
370
        private async void imageSelectionTask_Completed(StorageFile file) {
Margaux Clerc's avatar
Margaux Clerc committed
371 372 373 374

            string fileName = file.Name;
            BitmapImage image = new BitmapImage();
            IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
Erwan Croze's avatar
Erwan Croze committed
375
            try {
Margaux Clerc's avatar
Margaux Clerc committed
376
                image.SetSource(fileStream);
Erwan Croze's avatar
Erwan Croze committed
377
            } catch (Exception e) {
Margaux Clerc's avatar
Margaux Clerc committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

            }
            /*ChatSettingsManager chatMgr = new ChatSettingsManager();
            chatMgr.Load();
            if ((bool)chatMgr.ScaleDownSentPictures)
            {
                // Resize down the image
                WriteableBitmap bm = new WriteableBitmap(image);
                MemoryStream ms = new MemoryStream();
                int w = image.PixelWidth;
                int h = image.PixelHeight;
                if (w > h && w > 500)
                {
                    h = (500 * h) / w;
                    w = 500;
                }
                else if (h > w && h > 500)
                {
                    w = (500 * w) / h;
                    h = 500;
                }
                bm.SaveJpeg(ms, w, h, 0, 100);
                image.SetSource(ms);
            }*/
            string filename = await Utils.SaveImageInLocalFolder(file);
            MessageBox.SetImage(image);
            MessageBox.ImageName = filename;
405
            MessageBox.ImageType = file.FileType.Substring(1);
Margaux Clerc's avatar
Margaux Clerc committed
406 407 408 409 410
            var tempFolder = ApplicationData.Current.LocalFolder;
            StorageFile tempFile = await tempFolder.GetFileAsync(filename);
            MessageBox.ImageLocalPath = tempFile.Path;
            // RefreshSendMessageButtonEnabledState();
        }
411

Erwan Croze's avatar
Erwan Croze committed
412
        public void MessageReceived(Core lc, ChatRoom room, ChatMessage message) {
Erwan Croze's avatar
Erwan Croze committed
413 414
            if (room != chatRoom)
                return;
415
#pragma warning disable CS4014 // Dans la mesure où cet appel n'est pas attendu, l'exécution de la méthode actuelle continue avant la fin de l'appel
Erwan Croze's avatar
Erwan Croze committed
416
            MessagesList.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
417 418 419
                IncomingChatBubble bubble = new IncomingChatBubble(message);
                bubble.MessageDeleted += bubble_MessageDeleted;
                bubble.DownloadImage += bubble_DownloadImage;
Margaux Clerc's avatar
Margaux Clerc committed
420
                MessagesList.Children.Add(bubble);
421

Erwan Croze's avatar
Erwan Croze committed
422
                if (chatRoom != null) {
423 424 425 426 427 428 429
                    chatRoom.MarkAsRead();
                }

                scrollToBottom();
            });
#pragma warning restore CS4014 // Dans la mesure où cet appel n'est pas attendu, l'exécution de la méthode actuelle continue avant la fin de l'appel
        }
Margaux Clerc's avatar
Margaux Clerc committed
430

Erwan Croze's avatar
Erwan Croze committed
431
        public void ComposeReceived(Core lc, ChatRoom room) {
Margaux Clerc's avatar
Margaux Clerc committed
432
            UpdateComposingMessage();
433 434
        }

Erwan Croze's avatar
Erwan Croze committed
435
        private void UpdateComposingMessage() {
Margaux Clerc's avatar
Margaux Clerc committed
436
            if (chatRoom == null)
437 438 439 440 441 442
                return;

            bool isRemoteComposing = chatRoom.IsRemoteComposing;
            RemoteComposing.Visibility = isRemoteComposing ? Visibility.Visible : Visibility.Collapsed;

            string remoteName = chatRoom.PeerAddress.DisplayName;
Erwan Croze's avatar
Erwan Croze committed
443 444
            if (remoteName == null || remoteName.Length <= 0)
                remoteName = chatRoom.PeerAddress.Username;
445
            var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
Margaux Clerc's avatar
Margaux Clerc committed
446
            RemoteComposing.Text = remoteName + loader.GetString("RemoteComposing");
447 448
        }

Erwan Croze's avatar
Erwan Croze committed
449
        public string GetSipAddressAssociatedWithDisplayConversation() {
Erwan Croze's avatar
Erwan Croze committed
450
            return String.Format("{0}@{1}", sipAddress.Username, sipAddress.Domain);
451 452
        }

Erwan Croze's avatar
Erwan Croze committed
453
        private void scrollToBottom() {
Margaux Clerc's avatar
Margaux Clerc committed
454
            MessagesScroll.UpdateLayout();
Erwan Croze's avatar
Erwan Croze committed
455
            MessagesScroll.ChangeView(1, MessagesScroll.ExtentHeight, 1);
456 457
        }

Erwan Croze's avatar
Erwan Croze committed
458
        public async void bubble_DownloadImage(object sender, ChatMessage message) {
459
            EnableDownloadButtons(false);
460 461 462 463
            string fileName = Utils.GetFileName();
            message.FileTransferFilepath = Path.Combine(ApplicationData.Current.LocalFolder.Path, fileName);
            message.Appdata = fileName;
            message.Listener.OnMsgStateChanged = MessageStateChanged;
Erwan Croze's avatar
Erwan Croze committed
464
            message.DownloadFile();
465 466
        }

Erwan Croze's avatar
Erwan Croze committed
467 468
        private void EnableDownloadButtons(bool enable) {
            foreach (IncomingChatBubble bubble in MessagesList.Children.OfType<IncomingChatBubble>()) {
Margaux Clerc's avatar
Margaux Clerc committed
469
                // (bubble as IncomingChatBubble).Download.IsEnabled = enable;  
470 471
            }
        }
Margaux Clerc's avatar
Margaux Clerc committed
472
    }
473
}