2021-10-01 00:24:18 +00:00
|
|
|
|
using RadioBrowser;
|
|
|
|
|
using RadioBrowser.Models;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
using System.Windows.Controls;
|
2021-10-03 11:54:54 +00:00
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Collections.Generic;
|
2021-10-01 00:24:18 +00:00
|
|
|
|
|
|
|
|
|
namespace unison
|
|
|
|
|
{
|
2021-10-03 11:54:54 +00:00
|
|
|
|
public class CountryListItem
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2021-10-03 15:20:14 +00:00
|
|
|
|
public uint Count { get; set; }
|
2021-10-03 11:54:54 +00:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2021-10-03 11:54:54 +00:00
|
|
|
|
if (Name == "")
|
|
|
|
|
return "None";
|
|
|
|
|
return $"{Name} ({Count})";
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
2021-10-03 11:54:54 +00:00
|
|
|
|
}
|
2021-10-01 00:24:18 +00:00
|
|
|
|
|
2021-10-03 11:54:54 +00:00
|
|
|
|
public class StationListItem
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public string Codec { get; set; }
|
|
|
|
|
public string Tags { get; set; }
|
2021-10-03 15:20:14 +00:00
|
|
|
|
public int Bitrate { get; set; }
|
2021-10-03 11:54:54 +00:00
|
|
|
|
public Uri Url { get; set; }
|
|
|
|
|
|
|
|
|
|
private string _country;
|
|
|
|
|
public string Country
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2021-10-03 11:54:54 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_country.Length == 0)
|
|
|
|
|
return "🏴☠️";
|
|
|
|
|
return string.Concat(_country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5))); // return emoji
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_country = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public partial class Radios : Window
|
|
|
|
|
{
|
2021-10-03 11:54:54 +00:00
|
|
|
|
private RadioBrowserClient _radioBrowser;
|
2021-10-01 00:24:18 +00:00
|
|
|
|
private MPDHandler _mpd;
|
2022-04-17 23:20:43 +00:00
|
|
|
|
private bool _connected = true;
|
|
|
|
|
|
|
|
|
|
public bool IsConnected() => _connected;
|
2021-10-01 00:24:18 +00:00
|
|
|
|
|
|
|
|
|
public Radios()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2021-10-03 11:54:54 +00:00
|
|
|
|
|
2022-04-04 22:23:55 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_radioBrowser = new RadioBrowserClient();
|
|
|
|
|
Initialize();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Exception while connecting to RadioBrowser: " + e.Message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-17 23:20:43 +00:00
|
|
|
|
|
|
|
|
|
_connected = true;
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-03 11:54:54 +00:00
|
|
|
|
public async void Initialize()
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2022-04-12 21:14:13 +00:00
|
|
|
|
try
|
2021-10-03 11:54:54 +00:00
|
|
|
|
{
|
2022-04-12 21:14:13 +00:00
|
|
|
|
List<NameAndCount> Countries = await _radioBrowser.Lists.GetCountriesAsync();
|
|
|
|
|
CountryList.Items.Add(new CountryListItem { Name = "", Count = 0 });
|
|
|
|
|
|
|
|
|
|
foreach (NameAndCount Country in Countries)
|
2021-10-03 11:54:54 +00:00
|
|
|
|
{
|
2022-04-12 21:14:13 +00:00
|
|
|
|
CountryList.Items.Add(new CountryListItem
|
|
|
|
|
{
|
|
|
|
|
Name = Country.Name,
|
|
|
|
|
Count = Country.Stationcount
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Exception while getting countries in RadioBrowser: " + e.Message);
|
|
|
|
|
return;
|
2021-10-03 11:54:54 +00:00
|
|
|
|
}
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
2021-10-03 11:54:54 +00:00
|
|
|
|
|
|
|
|
|
private string CleanString(string str)
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2021-10-03 11:54:54 +00:00
|
|
|
|
return str.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SearchAdvanced(string name, string country, string tags)
|
|
|
|
|
{
|
2022-04-04 22:23:55 +00:00
|
|
|
|
try
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2022-04-04 22:23:55 +00:00
|
|
|
|
SearchStatus.Text = unison.Resources.Resources.Radio_Loading;
|
2021-10-01 00:24:18 +00:00
|
|
|
|
|
2022-04-04 22:23:55 +00:00
|
|
|
|
List<StationInfo> advancedSearch = await _radioBrowser.Search.AdvancedAsync(new AdvancedSearchOptions
|
|
|
|
|
{
|
|
|
|
|
Name = name,
|
|
|
|
|
Country = country,
|
|
|
|
|
TagList = tags
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
RadioListGrid.Items.Clear();
|
|
|
|
|
if (advancedSearch.Count > 0)
|
2021-10-03 11:54:54 +00:00
|
|
|
|
{
|
2022-04-04 22:23:55 +00:00
|
|
|
|
SearchStatus.Text = "";
|
|
|
|
|
foreach (StationInfo station in advancedSearch)
|
2021-10-03 11:54:54 +00:00
|
|
|
|
{
|
2022-04-04 22:23:55 +00:00
|
|
|
|
RadioListGrid.Items.Add(new StationListItem
|
|
|
|
|
{
|
|
|
|
|
Name = CleanString(station.Name),
|
|
|
|
|
Country = station.CountryCode,
|
|
|
|
|
Codec = station.Codec,
|
|
|
|
|
Bitrate = station.Bitrate,
|
|
|
|
|
Url = station.Url,
|
|
|
|
|
Tags = string.Join(", ", station.Tags)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
FitToContent();
|
2021-10-03 11:54:54 +00:00
|
|
|
|
}
|
2022-04-04 22:23:55 +00:00
|
|
|
|
else
|
|
|
|
|
SearchStatus.Text = unison.Resources.Resources.Radio_NotFound;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception except)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Error on RadioBrowser search advanced: " + except.Message);
|
2021-10-03 11:54:54 +00:00
|
|
|
|
}
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-03 11:54:54 +00:00
|
|
|
|
private void FitToContent()
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2021-10-03 11:54:54 +00:00
|
|
|
|
foreach (DataGridColumn column in RadioListGrid.Columns)
|
|
|
|
|
column.Width = new DataGridLength(1.0, DataGridLengthUnitType.SizeToCells);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DataGrid grid = sender as DataGrid;
|
|
|
|
|
StationListItem station;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
station = grid.Items[grid.SelectedIndex] as StationListItem;
|
|
|
|
|
}
|
2021-10-03 15:20:14 +00:00
|
|
|
|
catch (ArgumentOutOfRangeException)
|
2021-10-03 11:54:54 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Error: Invalid index.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (station.Url == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Error: Invalid station.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-01 00:24:18 +00:00
|
|
|
|
|
|
|
|
|
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
|
2021-10-03 15:20:14 +00:00
|
|
|
|
_mpd.ClearAddAndPlay(station.Url.AbsoluteUri);
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-03 11:54:54 +00:00
|
|
|
|
private async void Search_Clicked(object sender, RoutedEventArgs e)
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2022-04-04 22:23:55 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CountryListItem a = (CountryListItem)CountryList.SelectedItem;
|
|
|
|
|
await SearchAdvanced(NameSearch.Text, a?.Name, TagSearch.Text);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception except)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Error on RadioBrowser search: " + except.Message);
|
|
|
|
|
}
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-03 11:54:54 +00:00
|
|
|
|
private void Reset_Clicked(object sender, RoutedEventArgs e)
|
2021-10-01 00:24:18 +00:00
|
|
|
|
{
|
2021-10-03 11:54:54 +00:00
|
|
|
|
NameSearch.Text = "";
|
|
|
|
|
TagSearch.Text = "";
|
|
|
|
|
CountryList.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchHandler(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Key == Key.Return)
|
|
|
|
|
Search_Clicked(null, null);
|
2021-10-01 00:24:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Window_Closing(object sender, CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
WindowState = WindowState.Minimized;
|
|
|
|
|
Hide();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitHwnd()
|
|
|
|
|
{
|
|
|
|
|
WindowInteropHelper helper = new(this);
|
|
|
|
|
helper.EnsureHandle();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-03 11:54:54 +00:00
|
|
|
|
}
|