Commit 3328dc2c authored by Ghislain MARY's avatar Ghislain MARY

Use LinphoneAddress objects instead of error-prone string handling in the application.

parent 5e104627
......@@ -1446,15 +1446,30 @@ void Linphone::Core::LinphoneCore::EnableSelfView(Platform::Boolean enable)
API_UNLOCK;
}
Linphone::Core::LinphoneChatRoom^ Linphone::Core::LinphoneCore::GetOrCreateChatRoom(Platform::String^ to)
Linphone::Core::LinphoneChatRoom^ Linphone::Core::LinphoneCore::GetChatRoom(Linphone::Core::LinphoneAddress^ address)
{
API_LOCK;
const char* address = Linphone::Core::Utils::pstoccs(to);
Linphone::Core::LinphoneChatRoom^ chatRoom = (Linphone::Core::LinphoneChatRoom^) Linphone::Core::Utils::CreateLinphoneChatRoom(linphone_core_get_or_create_chat_room(this->lc, address));
delete(address);
::LinphoneChatRoom * chatRoom = linphone_core_get_chat_room(this->lc, address->address);
Linphone::Core::RefToPtrProxy<Linphone::Core::LinphoneChatRoom^> *proxy = reinterpret_cast< Linphone::Core::RefToPtrProxy<Linphone::Core::LinphoneChatRoom^> *>(linphone_chat_room_get_user_data(chatRoom));
Linphone::Core::LinphoneChatRoom^ lChatRoom = (proxy) ? proxy->Ref() : nullptr;
if (lChatRoom == nullptr) {
lChatRoom = (Linphone::Core::LinphoneChatRoom^) Linphone::Core::Utils::CreateLinphoneChatRoom(chatRoom);
}
API_UNLOCK;
return lChatRoom;
}
return chatRoom;
Linphone::Core::LinphoneChatRoom^ Linphone::Core::LinphoneCore::GetChatRoomFromUri(Platform::String^ to)
{
API_LOCK;
::LinphoneChatRoom * chatRoom = linphone_core_get_chat_room_from_uri(this->lc, Linphone::Core::Utils::pstoccs(to));
Linphone::Core::RefToPtrProxy<Linphone::Core::LinphoneChatRoom^> *proxy = reinterpret_cast< Linphone::Core::RefToPtrProxy<Linphone::Core::LinphoneChatRoom^> *>(linphone_chat_room_get_user_data(chatRoom));
Linphone::Core::LinphoneChatRoom^ lChatRoom = (proxy) ? proxy->Ref() : nullptr;
if (lChatRoom == nullptr) {
lChatRoom = (Linphone::Core::LinphoneChatRoom^) Linphone::Core::Utils::CreateLinphoneChatRoom(chatRoom);
}
API_UNLOCK;
return lChatRoom;
}
void Linphone::Core::LinphoneCore::SetLogCollectionUploadServerUrl(Platform::String^ url)
......
......@@ -1072,9 +1072,16 @@ namespace Linphone
void EnableSelfView(Platform::Boolean enable);
/// <summary>
/// Creates a ChatRoom to send text messages to given addrees.
/// Get a chat room whose peer is the supplied address. If it does not exist yet, it will be created.
/// </summary>
Linphone::Core::LinphoneChatRoom^ GetOrCreateChatRoom(Platform::String^ to);
/// <param name="address">A LinphoneAddress</param>
Linphone::Core::LinphoneChatRoom^ GetChatRoom(Linphone::Core::LinphoneAddress^ address);
/// <summary>
/// Get a chat room for messaging from a sip uri like sip:joe@sip.linphone.org. If it does not exist yet, it will be created.
/// </summary>
/// <param name="to">The destination address for messages</param>
Linphone::Core::LinphoneChatRoom^ GetChatRoomFromUri(Platform::String^ to);
/// <summary>
/// Sets the log collection upload server URL.
......
......@@ -5,7 +5,7 @@ using System.Windows.Navigation;
namespace Linphone.Model
{
/// <summary>
/// Class used to allow the app to be automatically launched when user click on a sip: link.
/// Class used to allow the app to be automatically launched when user click on a sip: or sips: link.
/// </summary>
class AssociationUriMapper : UriMapperBase
{
......@@ -16,7 +16,7 @@ namespace Linphone.Model
if (tempUri.StartsWith("/Protocol?encodedLaunchUri="))
{
tempUri = tempUri.Replace("/Protocol?encodedLaunchUri=", "");
if (tempUri.Contains("sip:"))
if (tempUri.StartsWith("sip:") || tempUri.StartsWith("sips:"))
{
return new Uri("/Views/Dialer.xaml?sip=" + tempUri, UriKind.RelativeOrAbsolute);
}
......
using Microsoft.Phone.UserData;
using Linphone.Core;
using Microsoft.Phone.UserData;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -138,7 +139,7 @@ namespace Linphone.Model
return recent;
}
private static bool IsDigitsOnly(string str)
private static bool IsPhoneNumber(string str)
{
foreach (char c in str)
{
......@@ -154,24 +155,14 @@ namespace Linphone.Model
/// <param name="numberOrAddress"></param>
public void FindContact(String numberOrAddress)
{
if (numberOrAddress.Contains('@'))
LinphoneAddress address = LinphoneManager.Instance.LinphoneCore.InterpretURL(numberOrAddress);
string addressWithoutScheme = String.Format("{0}@{1}", address.GetUserName(), address.GetDomain());
string username = address.GetUserName();
if (IsPhoneNumber(username))
{
string cleanAddress = numberOrAddress.Replace("sip:", "").Replace("sips:", "");
FindContactByEmail(cleanAddress, numberOrAddress);
string number = cleanAddress.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)[0];
if (IsDigitsOnly(number))
{
FindContactByNumber(number, numberOrAddress);
}
}
else
{
if (IsDigitsOnly(numberOrAddress))
{
FindContactByNumber(numberOrAddress, numberOrAddress);
}
FindContactByEmail(numberOrAddress + "@sip.linphone.org", numberOrAddress);
FindContactByNumber(username, addressWithoutScheme);
}
FindContactByEmail(addressWithoutScheme, addressWithoutScheme);
}
/// <summary>
......
......@@ -461,11 +461,17 @@ namespace Linphone.Model
{
string from = log.GetFrom().GetDisplayName();
if (from.Length == 0)
from = log.GetFrom().AsStringUriOnly().Replace("sip:", "");
{
LinphoneAddress fromAddress = log.GetFrom();
from = String.Format("{0}@{1}", fromAddress.GetUserName(), fromAddress.GetDomain());
}
string to = log.GetTo().GetDisplayName();
if (to.Length == 0)
to = log.GetTo().AsStringUriOnly().Replace("sip:", "");
{
LinphoneAddress toAddress = log.GetTo();
to = String.Format("{0}@{1}", toAddress.GetUserName(), toAddress.GetDomain());
}
bool isMissed = log.GetStatus() == LinphoneCallStatus.Missed;
long startDate = log.GetStartDate();
......@@ -1037,7 +1043,8 @@ namespace Linphone.Model
if (BaseModel.UIDispatcher == null) return;
BaseModel.UIDispatcher.BeginInvoke(() =>
{
string sipAddress = message.GetFrom().AsStringUriOnly().Replace("sip:", "");
LinphoneAddress fromAddress = message.GetFrom();
string sipAddress = String.Format("{0}@{1}", fromAddress.GetUserName(), fromAddress.GetDomain());
Logger.Msg("[LinphoneManager] Message received from " + sipAddress + ": " + message.GetText() + "\r\n");
//Vibrate
......@@ -1115,7 +1122,8 @@ namespace Linphone.Model
if (ComposingListener != null && room != null)
{
string currentListenerSipAddress = ComposingListener.GetSipAddressAssociatedWithDisplayConversation();
string roomComposingSipAddress = room.GetPeerAddress().AsStringUriOnly().Replace("sip:", "");
LinphoneAddress peerAddress = room.GetPeerAddress();
string roomComposingSipAddress = String.Format("{0}@{1}", peerAddress.GetUserName(), peerAddress.GetDomain());
if (currentListenerSipAddress != null && roomComposingSipAddress.Equals(currentListenerSipAddress))
ComposingListener.ComposeReceived();
......@@ -1185,13 +1193,10 @@ namespace Linphone.Model
{
try
{
string sipAddress = call.GetRemoteAddress().AsStringUriOnly();
if (call.GetRemoteAddress().GetDisplayName().Length == 0)
LinphoneAddress remoteAddress = call.GetRemoteAddress();
if (remoteAddress.GetDisplayName().Length == 0)
{
if (sipAddress.StartsWith("sip:"))
{
sipAddress = sipAddress.Substring(4);
}
string sipAddress = String.Format("{0}@{1}", remoteAddress.GetUserName(), remoteAddress.GetDomain());
Logger.Msg("[LinphoneManager] Display name null, looking for remote address in contact: " + sipAddress + "\r\n");
ContactManager.ContactFound += OnContactFound;
......@@ -1204,7 +1209,7 @@ namespace Linphone.Model
}
catch
{
Logger.Warn("[LinphoneManager] Execption occured while looking for contact...\r\n");
Logger.Warn("[LinphoneManager] Exception occured while looking for contact...\r\n");
}
}
......
......@@ -3,6 +3,7 @@ using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using Linphone.Core;
namespace Linphone.Model
{
......@@ -15,15 +16,8 @@ namespace Linphone.Model
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string sipAddress = (string)value;
if (sipAddress.StartsWith("sip:"))
{
sipAddress = sipAddress.Replace("sip:", "");
}
if (sipAddress.Contains("@"))
{
sipAddress = sipAddress.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)[0];
}
return sipAddress;
LinphoneAddress addr = LinphoneManager.Instance.LinphoneCore.InterpretURL(sipAddress);
return addr.GetUserName();
}
/// <summary>
......
......@@ -70,7 +70,7 @@ namespace Linphone.Views
/// <summary>
/// SIP address linked to the current displayed chat.
/// </summary>
public string sipAddress;
public LinphoneAddress sipAddress;
/// <summary>
/// ChatRoom used to send and receive messages.
......@@ -105,12 +105,7 @@ namespace Linphone.Views
ContactName.Visibility = Visibility.Visible;
if (NavigationContext.QueryString.ContainsKey("sip"))
{
sipAddress = NavigationContext.QueryString["sip"];
if (sipAddress.StartsWith("sip:"))
{
sipAddress = sipAddress.Replace("sip:", "");
}
sipAddress = LinphoneManager.Instance.LinphoneCore.InterpretURL(NavigationContext.QueryString["sip"]);
CreateChatRoom(sipAddress);
UpdateComposingMessage();
......@@ -120,7 +115,7 @@ namespace Linphone.Views
DisplayPastMessages(chatRoom.GetHistory());
}
}
else if (e.NavigationMode != NavigationMode.Back || sipAddress == null || sipAddress.Length == 0)
else if (e.NavigationMode != NavigationMode.Back || sipAddress == null)
{
ContactName.Visibility = Visibility.Collapsed;
NewChat.Visibility = Visibility.Visible;
......@@ -278,36 +273,17 @@ namespace Linphone.Views
});
}
private void CreateChatRoom(string sipAddress)
private void CreateChatRoom(LinphoneAddress sipAddress)
{
if (!sipAddress.Contains("@"))
{
if (LinphoneManager.Instance.LinphoneCore.GetProxyConfigList().Count > 0)
{
LinphoneProxyConfig config = LinphoneManager.Instance.LinphoneCore.GetProxyConfigList()[0] as LinphoneProxyConfig;
sipAddress += "@" + config.GetDomain();
}
else
{
System.Windows.MessageBox.Show(AppResources.InvalidSipAddressError, AppResources.GenericError, MessageBoxButton.OK);
return;
}
}
this.sipAddress = sipAddress;
ContactManager.Instance.FindContact(sipAddress);
string displayedSipAddress = sipAddress;
if (displayedSipAddress.Contains("@"))
{
displayedSipAddress = displayedSipAddress.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)[0];
}
ContactName.Text = displayedSipAddress;
ContactManager.Instance.FindContact(String.Format("{0}@{1}", sipAddress.GetUserName(), sipAddress.GetDomain()));
ContactName.Text = sipAddress.GetUserName();
ContactName.Visibility = Visibility.Visible;
NewChat.Visibility = Visibility.Collapsed;
try
{
chatRoom = LinphoneManager.Instance.LinphoneCore.GetOrCreateChatRoom(sipAddress);
chatRoom = LinphoneManager.Instance.LinphoneCore.GetChatRoom(sipAddress);
}
catch
{
......@@ -329,7 +305,7 @@ namespace Linphone.Views
{
if (chatRoom == null) //This code will be executed only in case of new conversation
{
CreateChatRoom(NewChatSipAddress.Text);
CreateChatRoom(LinphoneManager.Instance.LinphoneCore.InterpretURL(NewChatSipAddress.Text));
}
if (chatRoom != null)
......@@ -365,7 +341,7 @@ namespace Linphone.Views
{
if (chatRoom == null) //This code will be executed only in case of new conversation
{
CreateChatRoom(NewChatSipAddress.Text);
CreateChatRoom(LinphoneManager.Instance.LinphoneCore.InterpretURL(NewChatSipAddress.Text));
}
if (chatRoom != null)
{
......@@ -453,7 +429,7 @@ namespace Linphone.Views
private bool ShouldSendMessageButtonBeEnabled()
{
return ((MessageBox.Text != null && MessageBox.Text.Length > 0) || (MessageBox.ImageName != null && MessageBox.ImageName.Length > 0 && MessageBox.ImageLocalPath != null && MessageBox.ImageLocalPath.Length > 0))
&& ((sipAddress != null && sipAddress.Length > 0) || (NewChatSipAddress.Text != null && NewChatSipAddress.Text.Length > 0));
&& ((sipAddress != null) || (NewChatSipAddress.Text != null && NewChatSipAddress.Text.Length > 0));
}
private void AddSendButtonsToAppBar()
......@@ -539,7 +515,7 @@ namespace Linphone.Views
/// <returns></returns>
public string GetSipAddressAssociatedWithDisplayConversation()
{
return sipAddress;
return String.Format("{0}@{1}", sipAddress.GetUserName(), sipAddress.GetDomain());
}
private void scrollToBottom()
......
......@@ -116,11 +116,12 @@ namespace Linphone.Views
{
if (conversation.GetHistorySize() > 0)
{
string address = conversation.GetPeerAddress().AsStringUriOnly().Replace("sip:", "");
string name = conversation.GetPeerAddress().GetDisplayName();
LinphoneAddress peerAddress = conversation.GetPeerAddress();
string address = String.Format("{0}@{1}", peerAddress.GetUserName(), peerAddress.GetDomain());
string name = peerAddress.GetDisplayName();
if (name == null || name.Length <= 0)
{
name = conversation.GetPeerAddress().GetUserName();
name = peerAddress.GetUserName();
}
_conversations.Add(new Conversation(address, name, conversation.GetHistory()));
ContactManager.Instance.FindContact(address);
......@@ -141,7 +142,7 @@ namespace Linphone.Views
string sipAddress = (string)item.CommandParameter;
if (sipAddress != null && sipAddress.Length > 0)
{
LinphoneManager.Instance.LinphoneCore.GetOrCreateChatRoom(sipAddress).DeleteHistory();
LinphoneManager.Instance.LinphoneCore.GetChatRoomFromUri(sipAddress).DeleteHistory();
GetMessagesAndDisplayConversationsList();
}
}
......@@ -150,7 +151,7 @@ namespace Linphone.Views
{
foreach (var c in _selection)
{
LinphoneManager.Instance.LinphoneCore.GetOrCreateChatRoom(c.SipAddress).DeleteHistory();
LinphoneManager.Instance.LinphoneCore.GetChatRoomFromUri(c.SipAddress).DeleteHistory();
}
GetMessagesAndDisplayConversationsList();
......
......@@ -185,10 +185,8 @@ namespace Linphone.Views
if (NavigationContext.QueryString.ContainsKey("sip"))
{
String calledNumber = NavigationContext.QueryString["sip"];
if (calledNumber.StartsWith("sip:"))
{
calledNumber = calledNumber.Substring(4);
}
LinphoneAddress address = LinphoneManager.Instance.LinphoneCore.InterpretURL(calledNumber);
calledNumber = String.Format("{0}@{1}", address.GetUserName(), address.GetDomain());
// While we dunno if the number matches a contact one, we consider it won't and we display the phone number as username
Contact.Text = calledNumber;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment