Tutorial for LinphoneSDK x UWP - C#

This commit is contained in:
Anthony Gauchy
2020-12-10 17:20:19 +01:00
parent 50483699b5
commit 29f4bbef5c
215 changed files with 13724 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
<UserControl
x:Class="_06_GroupChat.Controls.ContentDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<StackPanel x:Name="TextStack" Orientation="Vertical" Visibility="Collapsed" Margin="0,0,0,10">
<TextBlock Text="Text :" />
<TextBlock x:Name="TextMessage" Text="" FontWeight="Bold" TextWrapping="Wrap" />
</StackPanel>
<StackPanel x:Name="FileStack" Orientation="Vertical" Visibility="Collapsed" Margin="0,0,0,10">
<TextBlock Text="File : " />
<TextBlock x:Name="FileName" Text="" FontWeight="Bold" />
<TextBlock x:Name="FileSize" Text="" />
<Button x:Name="Download" Content="Download" Click="Download_Click" Visibility="Collapsed" />
<Button x:Name="OpenFile" Content="Open file" Click="OpenFile_Click" Visibility="Collapsed" />
</StackPanel>
</Grid>
</UserControl>

View File

@@ -0,0 +1,96 @@
/*
* 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 Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace _06_GroupChat.Controls
{
public sealed partial class ContentDisplay : UserControl
{
private readonly ChatMessage ChatMessage;
private readonly Content DisplayedContent;
public ContentDisplay(Content content, ChatMessage chatMessage)
{
this.InitializeComponent();
DisplayedContent = content;
ChatMessage = chatMessage;
UpdateLayoutFromContent();
}
private void UpdateLayoutFromContent()
{
// We kept the code from the old MessageDisplay class. Working
// with a single object instead of a List of content make it
// cleaner.
if (DisplayedContent.IsFile || DisplayedContent.IsFileTransfer)
{
TextStack.Visibility = Visibility.Collapsed;
FileStack.Visibility = Visibility.Visible;
FileName.Text = DisplayedContent.Name;
FileSize.Text = DisplayedContent.FileSize + " bits";
if (DisplayedContent.IsFile || DisplayedContent.IsFileTransfer && ChatMessage.IsOutgoing)
{
OpenFile.Visibility = Visibility.Visible;
Download.Visibility = Visibility.Collapsed;
}
else
{
Download.Visibility = Visibility.Visible;
OpenFile.Visibility = Visibility.Collapsed;
}
}
else if (DisplayedContent.IsText)
{
TextStack.Visibility = Visibility.Visible;
FileStack.Visibility = Visibility.Collapsed;
TextMessage.Text = DisplayedContent.Utf8Text;
}
}
// The download and open file click method are the same as before
private void Download_Click(object sender, RoutedEventArgs e)
{
Download.Visibility = Visibility.Collapsed;
FileSize.Text = "Download in progress ...";
string downloadPathFolder = ApplicationData.Current.LocalFolder.Path + @"\Downloads\";
Directory.CreateDirectory(downloadPathFolder);
DisplayedContent.FilePath = downloadPathFolder + DisplayedContent.Name;
ChatMessage.DownloadContent(DisplayedContent);
}
private async void OpenFile_Click(object sender, RoutedEventArgs e)
{
string filePath = DisplayedContent.FilePath;
string folderPath = filePath.Substring(0, filePath.LastIndexOf("\\"));
await Launcher.LaunchFolderAsync(await StorageFolder.GetFolderFromPathAsync(folderPath));
}
}
}

View File

@@ -0,0 +1,11 @@
<UserControl
x:Class="_06_GroupChat.Controls.EventDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE949;" />
<TextBlock x:Name="EventText" Text="" Margin="10" />
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE949;" />
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,79 @@
/*
* 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 Windows.UI.Xaml.Controls;
namespace _06_GroupChat.Controls
{
public sealed partial class EventDisplay : UserControl
{
public EventDisplay(EventLog eventLog)
{
// An EventDisplay is always linked to an EventLog object and is displayed at the center
// of the message list.
this.InitializeComponent();
// After we simply create the text we want to display based on the type on event
// and from information we get from the EventLog object.
switch (eventLog.Type)
{
case EventLogType.ConferenceCreated:
// For example here we use the Subject attribute to display the name of the conference
// when it is created.
EventText.Text = $"The conference {eventLog.Subject} is created";
break;
case EventLogType.ConferenceTerminated:
EventText.Text = $"The conference {eventLog.Subject} is terminated";
break;
case EventLogType.ConferenceCallStart:
EventText.Text = "Call start";
break;
case EventLogType.ConferenceCallEnd:
EventText.Text = "Call end";
break;
case EventLogType.ConferenceParticipantAdded:
// Or you can access a ParticipantAddress attribute when the type of
// event is linked to a participant.
EventText.Text = $"{eventLog.ParticipantAddress.Username} is added";
break;
case EventLogType.ConferenceParticipantRemoved:
EventText.Text = $"{eventLog.ParticipantAddress.Username} is removed";
break;
case EventLogType.ConferenceParticipantSetAdmin:
EventText.Text = $"{eventLog.ParticipantAddress.Username} is now admin";
break;
case EventLogType.ConferenceParticipantUnsetAdmin:
EventText.Text = $"{eventLog.ParticipantAddress.Username} admin status removed";
break;
case EventLogType.ConferenceSubjectChanged:
EventText.Text = $"The conference subject is now {eventLog.Subject}";
break;
}
}
}
}

View File

@@ -0,0 +1,52 @@
<UserControl
x:Class="_06_GroupChat.Controls.GroupChatDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:linphone="using:Linphone"
Loaded="GroupChatDisplay_Loaded"
Unloaded="GroupChatDisplay_Unloaded">
<Grid x:Name="GroupChatDisplayGrid">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBlock>Participants :</TextBlock>
<Button x:Name="AddParticipant" Click="AddParticipant_Click" FontFamily="Segoe MDL2 Assets" Content="&#xE8FA;" />
</StackPanel>
<ListView x:Name="ParticipantsLV" SelectionMode="None" IsItemClickEnabled="False" Grid.Row="1">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="linphone:Participant">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.8*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Address.Username}" Grid.Row="0" Grid.Column="0" />
<Button Tag="{Binding}" Click="Remove_Click" FontFamily="Segoe MDL2 Assets" Content="&#xE8F8;" Grid.Column="1" Grid.Row="0" />
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="Is admin : " />
<TextBlock Text="{Binding Path=IsAdmin}" Margin="1,0,0,0" />
</StackPanel>
<Button Tag="{Binding}" Click="AdminSwitch_Click" FontFamily="Segoe MDL2 Assets" Content="&#xE8CB;" Grid.Column="1" Grid.Row="1" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="RenameGroupChat" Click="RenameGroupChat_Click" Content="Rename group chat" VerticalAlignment="Bottom" Grid.Row="2" />
</Grid>
</UserControl>

View File

@@ -0,0 +1,170 @@
/*
* 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 _06_GroupChat.Service;
using _06_GroupChat.Shared;
using Linphone;
using System;
using System.Linq;
using Windows.UI.Xaml.Controls;
namespace _06_GroupChat.Controls
{
public sealed partial class GroupChatDisplay : UserControl
{
private CoreService CoreService { get; } = CoreService.Instance;
private readonly ChatRoom ChatRoom;
public GroupChatDisplay(ChatRoom chatRoom)
{
this.InitializeComponent();
ChatRoom = chatRoom;
}
private void GroupChatDisplay_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
UpdateList();
UpdateGuiFromAdminState();
// We register to those callbacks so every time the participant list is
// modified we can update our GUI, see OnParticipantListUpdate and UpdateList.
ChatRoom.Listener.OnParticipantAdded += OnParticipantListUpdate;
ChatRoom.Listener.OnParticipantRemoved += OnParticipantListUpdate;
// Every time the admin status of one participant is changed this callback is called.
// We use this to update GUI because we display the admin status of each participant,
// see OnParticipantAdminStatusChanged and UpdateList. We also made a check to our admin
// status on this ChatRoom to see if we have access to the different controls (rename chat
// room, add/remove participant...)
ChatRoom.Listener.OnParticipantAdminStatusChanged += OnParticipantAdminStatusChanged;
}
private void GroupChatDisplay_Unloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// Don't forget to unregister to avoid memory leak
ChatRoom.Listener.OnParticipantAdded -= OnParticipantListUpdate;
ChatRoom.Listener.OnParticipantRemoved -= OnParticipantListUpdate;
ChatRoom.Listener.OnParticipantAdminStatusChanged -= OnParticipantAdminStatusChanged;
}
private void OnParticipantAdminStatusChanged(ChatRoom chatRoom, EventLog eventLog)
{
UpdateList();
UpdateGuiFromAdminState();
}
private void UpdateGuiFromAdminState()
{
// We check out our admin status with ChatRoom.Me.IsAdmin and if we
// aren't admin the controls are disabled.
AddParticipant.IsEnabled = ChatRoom.Me.IsAdmin;
foreach (var control in GroupChatDisplayGrid.Children.OfType<Control>())
{
control.IsEnabled = ChatRoom.Me.IsAdmin;
}
}
private void OnParticipantListUpdate(ChatRoom chatRoom, EventLog eventLog) => UpdateList();
private void UpdateList()
{
ParticipantsLV.Items.Clear();
// You can find the participant list in the ChatRoom.Participants attribute.
// You can note that the participant list doesn't contain yourself.
foreach (Participant participant in ChatRoom.Participants)
{
if (participant.Address != null && !String.IsNullOrWhiteSpace(participant.Address.Username))
{
ParticipantsLV.Items.Add(participant);
}
}
}
/// <summary>
/// This method is called when the remove button near a participant username is clicked.
/// </summary>
private void Remove_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Participant participantToRemove = (Participant)((Button)sender).Tag;
// To remove a participant simply use the RemoveParticipant(Participant participant) method
// on a ChatRoom object. If you are admin and the participant is present in the ChatRoom he
// will be removed.
// The method RemoveParticipants(IEnumerable<Participant> participants) also exist if you want to
// remove multiple participant at once.
ChatRoom.RemoveParticipant(participantToRemove);
}
/// <summary>
/// This method is called when the button to add a participant is clicked.
/// </summary>
private async void AddParticipant_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
string peerSipAddress = await Utils.InputTextDialogAsync("Enter peer sip address");
Address address = CoreService.Core.InterpretUrl(peerSipAddress);
if (address != null)
{
// To add a participant simply call the method AddParticipant(Address addr).
// If you are admin and the participant have a device that can handle
// group chat connected to the conference server he will be added.
// You can use AddParticipants(IEnumerable<Address> addresses) to add multiple
// participants at once.
// Here we use Core.InterpretUrl to transform a string sip address to a valid
// Linphone.Address object as we done multiple times before.
ChatRoom.AddParticipant(address);
}
else
{
ContentDialog badAddressDialog = new ContentDialog
{
Title = "Adding participant failed",
Content = "An error occurred during address interpretation, check sip address validity and try again.",
CloseButtonText = "OK"
};
await badAddressDialog.ShowAsync();
}
}
/// <summary>
/// This method is called when you switch the admin status of a participant
/// </summary>
private void AdminSwitch_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Participant participantToUpgrade = (Participant)((Button)sender).Tag;
// Use the SetParticipantAdminStatus(Participant participant, bool isAdmin) to change
// the admin of a participant, you must be admin yourself if you want this action to work.
ChatRoom.SetParticipantAdminStatus(participantToUpgrade, !participantToUpgrade.IsAdmin);
}
/// <summary>
/// This method is called when the "Rename group chat" button is clicked
/// </summary>
private async void RenameGroupChat_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
string newName = await Utils.InputTextDialogAsync("Enter new name for group");
// To change the subject of a ChatRoom simply update the Subject attribute of a ChatRoom.
ChatRoom.Subject = newName;
}
}
}

View File

@@ -0,0 +1,28 @@
<UserControl
x:Class="_06_GroupChat.Controls.MessageDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="MessageDisplay_Loaded"
Unloaded="MessageDisplay_Unloaded">
<Border BorderBrush="{ThemeResource SystemAccentColorDark3}" BorderThickness="1" Padding="2" CornerRadius="20,20,20,20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition MaxWidth="500" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="ReceiveDate" Grid.Column="0" Text="" VerticalAlignment="Center" />
<StackPanel Orientation="Vertical" Grid.Column="1" Padding="10">
<TextBlock x:Name="SenderName" Text="Sent by : " />
<StackPanel x:Name="ContentsStack" Orientation="Vertical">
</StackPanel>
<TextBlock x:Name="MessageState" />
</StackPanel>
</Grid>
</Border>
</UserControl>

View File

@@ -0,0 +1,113 @@
/*
* 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 Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace _06_GroupChat.Controls
{
public sealed partial class MessageDisplay : UserControl
{
private readonly ChatMessage ChatMessage;
public MessageDisplay(ChatMessage message)
{
this.InitializeComponent();
ChatMessage = message;
UpdateLayoutFromMessage();
UpdateLayoutFromContents();
}
private void MessageDisplay_Loaded(object sender, RoutedEventArgs e)
{
ChatMessage.Listener.OnMsgStateChanged += OnMessageStateChanged;
}
private void MessageDisplay_Unloaded(object sender, RoutedEventArgs e)
{
ChatMessage.Listener = null;
}
private void OnMessageStateChanged(ChatMessage message, ChatMessageState state)
{
MessageState.Text = "The message state is : " + state;
switch (state)
{
case ChatMessageState.FileTransferError:
case ChatMessageState.FileTransferDone:
UpdateLayoutFromContents();
return;
}
}
private void UpdateLayoutFromMessage()
{
// Here we keep only the informations kept at the ChatMessage level
MessageState.Text = "The message state is : " + ChatMessage.State;
ReceiveDate.Text = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(ChatMessage.Time).ToLocalTime().ToString("HH:mm");
SenderName.Text += ChatMessage.FromAddress.Username;
if (ChatMessage.IsOutgoing)
{
this.HorizontalAlignment = HorizontalAlignment.Right;
}
else
{
this.HorizontalAlignment = HorizontalAlignment.Left;
}
}
private void UpdateLayoutFromContents()
{
ContentsStack.Children.Clear();
// We iterate over the Contents list to display all the contents
// in a multipart message.
// This code is common for Basic and Flexisip ChatRoom so even if
// another SIP client don't respect the basic chat room rules and
// and send multipart we can display it.
foreach (Content content in ChatMessage.Contents)
{
AddContent(content);
}
}
private void AddContent(Content content)
{
// A Content object can himself be multipart
if (content.IsMultipart)
{
// So we make this method recursive
foreach (Content innerContent in content.Parts)
{
AddContent(innerContent);
}
return;
}
// And we create a content display for each content. You can watch the code
// in content ContentDisplay.xaml(.cs).
ContentDisplay contentDisplay = new ContentDisplay(content, ChatMessage);
ContentsStack.Children.Add(contentDisplay);
}
}
}