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="_07_AdvancedChat.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,92 @@
/*
* 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 _07_AdvancedChat.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()
{
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;
}
}
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="_07_AdvancedChat.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,83 @@
/*
* 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 _07_AdvancedChat.Controls
{
public sealed partial class EventDisplay : UserControl
{
public EventDisplay(EventLog eventLog)
{
this.InitializeComponent();
switch (eventLog.Type)
{
case EventLogType.ConferenceCreated:
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:
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;
// With the ephemeral mode handling new event types can appear.
case EventLogType.ConferenceEphemeralMessageDisabled:
EventText.Text = "Ephemeral message mode is disabled";
break;
case EventLogType.ConferenceEphemeralMessageEnabled:
EventText.Text = "Ephemeral message mode is enabled";
break;
case EventLogType.ConferenceEphemeralMessageLifetimeChanged:
EventText.Text = $"Ephemeral message lifetime is now {eventLog.EphemeralMessageLifetime}";
break;
}
}
}
}

View File

@@ -0,0 +1,52 @@
<UserControl
x:Class="_07_AdvancedChat.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,126 @@
/*
* 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 _07_AdvancedChat.Service;
using _07_AdvancedChat.Shared;
using Linphone;
using System;
using System.Linq;
using Windows.UI.Xaml.Controls;
namespace _07_AdvancedChat.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();
ChatRoom.Listener.OnParticipantAdded += OnParticipantListUpdate;
ChatRoom.Listener.OnParticipantRemoved += OnParticipantListUpdate;
ChatRoom.Listener.OnParticipantAdminStatusChanged += OnParticipantAdminStatusChanged;
}
private void GroupChatDisplay_Unloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ChatRoom.Listener.OnParticipantAdded -= OnParticipantListUpdate;
ChatRoom.Listener.OnParticipantRemoved -= OnParticipantListUpdate;
ChatRoom.Listener.OnParticipantAdminStatusChanged -= OnParticipantAdminStatusChanged;
}
private void OnParticipantAdminStatusChanged(ChatRoom chatRoom, EventLog eventLog)
{
UpdateList();
UpdateGuiFromAdminState();
}
private void UpdateGuiFromAdminState()
{
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();
foreach (Participant participant in ChatRoom.Participants)
{
if (participant.Address != null && !String.IsNullOrWhiteSpace(participant.Address.Username))
{
ParticipantsLV.Items.Add(participant);
}
}
}
private void Remove_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Participant participantToRemove = (Participant)((Button)sender).Tag;
ChatRoom.RemoveParticipant(participantToRemove);
}
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)
{
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();
}
}
private void AdminSwitch_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Participant participantToUpgrade = (Participant)((Button)sender).Tag;
ChatRoom.SetParticipantAdminStatus(participantToUpgrade, !participantToUpgrade.IsAdmin);
}
private async void RenameGroupChat_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
string newName = await Utils.InputTextDialogAsync("Enter new name for group");
ChatRoom.Subject = newName;
}
}
}

View File

@@ -0,0 +1,29 @@
<UserControl
x:Class="_07_AdvancedChat.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" />
<TextBlock x:Name="EphemeralLifetime" />
</StackPanel>
</Grid>
</Border>
</UserControl>

View File

@@ -0,0 +1,156 @@
/*
* 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 _07_AdvancedChat.Controls
{
public sealed partial class MessageDisplay : UserControl
{
private readonly ChatMessage ChatMessage;
private readonly DispatcherTimer Timer;
private int Basetime;
public MessageDisplay(ChatMessage message)
{
this.InitializeComponent();
ChatMessage = message;
UpdateLayoutFromMessage();
UpdateLayoutFromContents();
// Used to create a second by second count down.
Timer = new DispatcherTimer
{
Interval = new TimeSpan(0, 0, 1)
};
Timer.Tick += Timer_Tick;
}
private void MessageDisplay_Loaded(object sender, RoutedEventArgs e)
{
ChatMessage.Listener.OnMsgStateChanged += OnMessageStateChanged;
// The countdown for ephemeral lifetime start only when the message status is "Displayed".
// So we register to this callback to be notified when to start the countdown.
// See OnEphemeralMessageTimerStarted to see how we setup the countdown.
ChatMessage.Listener.OnEphemeralMessageTimerStarted += OnEphemeralMessageTimerStarted;
// Even if we setup a client side countdown we will be notified by the OnEphemeralMessageDeleted
// callback when we should destroy the message.
ChatMessage.Listener.OnEphemeralMessageDeleted += OnEphemeralMessageDeleted;
}
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()
{
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();
foreach (Content content in ChatMessage.Contents)
{
AddContent(content);
}
}
private void AddContent(Content content)
{
if (content.IsMultipart)
{
foreach (Content innerContent in content.Parts)
{
AddContent(innerContent);
}
return;
}
ContentDisplay contentDisplay = new ContentDisplay(content, ChatMessage);
ContentsStack.Children.Add(contentDisplay);
}
private void OnEphemeralMessageTimerStarted(ChatMessage message)
{
// Here we create a basic timer with the windows UI library, you can
// just note that we can found the ephemeral lifetime of this message
// as a read only attribute (ChatMessage.EphemeralLifetime).
Basetime = ChatMessage.EphemeralLifetime;
EphemeralLifetime.Text = $"{Basetime.ToString()} remaining before deletion";
// See Timer_Tick
Timer.Start();
}
private void OnEphemeralMessageDeleted(ChatMessage message)
{
// When this callback is triggered we erase the message from the view.
// Be careful, if you communicate with different clients some can choose
// to keep the message displayed even if it was an ephemeral one !
this.Content = new TextBlock
{
Text = "deleted ephemeral message"
};
}
private void Timer_Tick(object sender, object e)
{
// Every second we update the text under the ephemeral message
Basetime -= 1;
EphemeralLifetime.Text = $"{Basetime} remaining before deletion";
if (Basetime == 0)
{
Timer.Stop();
}
}
}
}