Tutorial for LinphoneSDK x UWP - C#
This commit is contained in:
188
uwp/cs/05_FileTransfer/Controls/MessageDisplay.xaml.cs
Normal file
188
uwp/cs/05_FileTransfer/Controls/MessageDisplay.xaml.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2020 Belledonne Communications SARL.
|
||||
*
|
||||
* This file is part of Linphone TutorialCS.
|
||||
*
|
||||
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using Linphone;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Windows.Storage;
|
||||
using Windows.System;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
namespace _05_FileTransfer.Controls
|
||||
{
|
||||
public sealed partial class MessageDisplay : UserControl
|
||||
{
|
||||
private readonly ChatMessage ChatMessage;
|
||||
private Content CurrentShownContent;
|
||||
|
||||
public MessageDisplay(ChatMessage message)
|
||||
{
|
||||
this.InitializeComponent();
|
||||
// We link every MessageDisplay object to a ChatMessage.
|
||||
ChatMessage = message;
|
||||
UpdateLayoutFromMessage();
|
||||
}
|
||||
|
||||
private void MessageDisplay_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Like other Linphone objects you can register to a variety
|
||||
// of callbacks on a ChatMessage object (see ChatMessage.Listener for the list).
|
||||
// Here we want to be called when the message state is updated.
|
||||
ChatMessage.Listener.OnMsgStateChanged += OnMessageStateChanged;
|
||||
}
|
||||
|
||||
private void MessageDisplay_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Again, don't forget to unregister to avoid memory leak.
|
||||
ChatMessage.Listener = null;
|
||||
}
|
||||
|
||||
private void OnMessageStateChanged(ChatMessage message, ChatMessageState state)
|
||||
{
|
||||
// We display the message state. It can be really useful for the user
|
||||
// to know if the remote received the message (state = Delivered) or
|
||||
// if he read it (state = Displayed)
|
||||
MessageState.Text = "The message state is : " + state;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
// They're is multiple state during a file transfer (FileTransferInProgress,
|
||||
// FileTransferDone, FileTransferError). We update the layout if the file
|
||||
// is done downloading to replace the "Download" button by an "Open file" button.
|
||||
case ChatMessageState.FileTransferDone:
|
||||
UpdateLayoutFromMessage();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLayoutFromMessage()
|
||||
{
|
||||
MessageState.Text = "The message state is : " + ChatMessage.State;
|
||||
|
||||
// You can find the sending date of a ChatMessage in ChatMessage.Time.
|
||||
// The time number respect the time_t type specification.
|
||||
ReceiveDate.Text = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(ChatMessage.Time).ToLocalTime().ToString("HH:mm");
|
||||
|
||||
if (ChatMessage.IsOutgoing)
|
||||
{
|
||||
this.HorizontalAlignment = HorizontalAlignment.Right;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.HorizontalAlignment = HorizontalAlignment.Left;
|
||||
}
|
||||
|
||||
// A ChatMessage hold a list of Content object, Contents.
|
||||
// But in a basic ChatRoom, using a basic backend, by default the multipart
|
||||
// is disable. So in any received message there is only one Content in the list.
|
||||
// You can enable multipart on a ChatRoom object with ChatRoom.AllowMultipart() but it
|
||||
// can be risky. In fact if your remote doesn't support multipart and you send him
|
||||
// a multipart message it could not work properly.
|
||||
if (ChatMessage.Contents.Any(c => c.IsFile))
|
||||
{
|
||||
// If the Content object isFile it means that it is an already
|
||||
// downloaded file, so we display the OpenFile button. See
|
||||
// this.OpenFile_Click to understand how to find the file from
|
||||
// the Content object.
|
||||
TextStack.Visibility = Visibility.Collapsed;
|
||||
FileStack.Visibility = Visibility.Visible;
|
||||
OpenFile.Visibility = Visibility.Visible;
|
||||
Download.Visibility = Visibility.Collapsed;
|
||||
|
||||
// We can do this because we don't allowMultipart and can assume
|
||||
// they're is only one element, and ChatMessage.Contents.Any(c => c.IsFile)
|
||||
// returned true.
|
||||
Content content = ChatMessage.Contents.First((c) => c.IsFile);
|
||||
|
||||
// Here we are displaying the name and the size of the file
|
||||
FileName.Text = content.Name;
|
||||
FileSize.Text = content.FileSize + " bits";
|
||||
CurrentShownContent = content;
|
||||
}
|
||||
else if (ChatMessage.Contents.Any(c => c.IsFileTransfer))
|
||||
{
|
||||
// If the Content object IsFileTransfer it means that the file is
|
||||
// not downloaded yet, so we display the Download button. See
|
||||
// this.Download_Click to understand how to download the file from
|
||||
// the Content object.
|
||||
TextStack.Visibility = Visibility.Collapsed;
|
||||
FileStack.Visibility = Visibility.Visible;
|
||||
Download.Visibility = Visibility.Visible;
|
||||
OpenFile.Visibility = Visibility.Collapsed;
|
||||
|
||||
Content content = ChatMessage.Contents.First((c) => c.IsFileTransfer);
|
||||
|
||||
FileName.Text = content.Name;
|
||||
FileSize.Text = content.FileSize + " bits";
|
||||
CurrentShownContent = content;
|
||||
}
|
||||
else if (ChatMessage.Contents.Any(c => c.IsText))
|
||||
{
|
||||
// If the content isText we only display the text value like before
|
||||
TextStack.Visibility = Visibility.Visible;
|
||||
FileStack.Visibility = Visibility.Collapsed;
|
||||
|
||||
Content content = ChatMessage.Contents.First((c) => c.IsText);
|
||||
|
||||
TextMessage.Text = content.Utf8Text;
|
||||
CurrentShownContent = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method called when the "Download" button is clicked
|
||||
/// </summary>
|
||||
private void Download_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (CurrentShownContent != null)
|
||||
{
|
||||
Download.Visibility = Visibility.Collapsed;
|
||||
FileSize.Text = "Download in progress ...";
|
||||
|
||||
// We create a directory where we have write rights.
|
||||
string downloadPathFolder = ApplicationData.Current.LocalFolder.Path + @"\Downloads\";
|
||||
Directory.CreateDirectory(downloadPathFolder);
|
||||
|
||||
// We set the future file path before we start the download.
|
||||
CurrentShownContent.FilePath = downloadPathFolder + CurrentShownContent.Name;
|
||||
|
||||
// And we use ChatMessage.DownloadContent(Content content) with
|
||||
// our Content object as parameter. The download is async and
|
||||
// you can follow the file transfer with OnFileTransferProgressIndicationDelegate
|
||||
// or simply wait the FileTransferDone state on the ChatMessage like we are
|
||||
// doing here.
|
||||
ChatMessage.DownloadContent(CurrentShownContent);
|
||||
}
|
||||
}
|
||||
|
||||
private async void OpenFile_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Just get the FilePath attribute from the Content object
|
||||
string filePath = CurrentShownContent.FilePath;
|
||||
|
||||
// Only keep the folder part
|
||||
string folderPath = filePath.Substring(0, filePath.LastIndexOf("\\"));
|
||||
|
||||
// And launch the Windows explorer
|
||||
await Launcher.LaunchFolderAsync(await StorageFolder.GetFolderFromPathAsync(folderPath));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user