2021-05-13 20:36:20 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Models.View;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Utilities
|
|
|
|
|
{
|
|
|
|
|
public class IconImageConverter : IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
var cipher = value as CipherView;
|
|
|
|
|
return GetIcon(cipher);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetIcon(CipherView cipher)
|
|
|
|
|
{
|
|
|
|
|
string icon = null;
|
|
|
|
|
switch (cipher.Type)
|
|
|
|
|
{
|
|
|
|
|
case CipherType.Login:
|
2021-10-12 17:00:33 +02:00
|
|
|
|
icon = IconImageHelper.GetLoginIconImage(cipher);
|
2021-05-13 20:36:20 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
2021-10-12 17:00:33 +02:00
|
|
|
|
}
|
2021-05-13 20:36:20 +02:00
|
|
|
|
|
2021-10-12 17:00:33 +02:00
|
|
|
|
public static class IconImageHelper
|
|
|
|
|
{
|
|
|
|
|
public static string GetLoginIconImage(CipherView cipher)
|
2021-05-13 20:36:20 +02:00
|
|
|
|
{
|
|
|
|
|
string image = null;
|
2021-10-12 17:00:33 +02:00
|
|
|
|
if (cipher.Login.HasUris)
|
2021-05-13 20:36:20 +02:00
|
|
|
|
{
|
2021-10-12 17:00:33 +02:00
|
|
|
|
foreach (var uri in cipher.Login.Uris)
|
2021-05-13 20:36:20 +02:00
|
|
|
|
{
|
2021-10-12 17:00:33 +02:00
|
|
|
|
var hostnameUri = uri.Uri;
|
|
|
|
|
var isWebsite = false;
|
|
|
|
|
if (!hostnameUri.Contains("."))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!hostnameUri.Contains("://"))
|
|
|
|
|
{
|
|
|
|
|
hostnameUri = string.Concat("http://", hostnameUri);
|
|
|
|
|
}
|
|
|
|
|
isWebsite = hostnameUri.StartsWith("http");
|
2021-05-13 20:36:20 +02:00
|
|
|
|
|
2021-10-12 17:00:33 +02:00
|
|
|
|
if (isWebsite)
|
2021-05-13 20:36:20 +02:00
|
|
|
|
{
|
2021-10-12 17:00:33 +02:00
|
|
|
|
image = GetIconUrl(hostnameUri);
|
|
|
|
|
break;
|
2021-05-13 20:36:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return image;
|
|
|
|
|
}
|
2021-10-12 17:00:33 +02:00
|
|
|
|
|
|
|
|
|
private static string GetIconUrl(string hostnameUri)
|
|
|
|
|
{
|
|
|
|
|
IEnvironmentService _environmentService = ServiceContainer.Resolve<IEnvironmentService>("environmentService");
|
|
|
|
|
|
|
|
|
|
var hostname = CoreHelpers.GetHostname(hostnameUri);
|
|
|
|
|
var iconsUrl = _environmentService.IconsUrl;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(iconsUrl))
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(_environmentService.BaseUrl))
|
|
|
|
|
{
|
|
|
|
|
iconsUrl = string.Format("{0}/icons", _environmentService.BaseUrl);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
iconsUrl = "https://icons.bitwarden.net";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return string.Format("{0}/{1}/icon.png", iconsUrl, hostname);
|
|
|
|
|
|
|
|
|
|
}
|
2021-05-13 20:36:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|