Skip to content

Commit

Permalink
Merge pull request #284 from Belphemur/name_fallback
Browse files Browse the repository at this point in the history
Name fallback
  • Loading branch information
Belphemur committed Jun 6, 2018
2 parents cef1443 + 8eb4f65 commit 388b311
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 64 deletions.
6 changes: 6 additions & 0 deletions SoundSwitch/Framework/Configuration/ConfigurationManager.cs
Expand Up @@ -27,6 +27,11 @@ public interface IConfiguration
string FileLocation { get; set; }

void Save();

/// <summary>
/// Migrate configuration to a new schema
/// </summary>
void Migrate();
}

public static class ConfigurationManager
Expand Down Expand Up @@ -65,6 +70,7 @@ public static T LoadConfiguration<T>() where T : IConfiguration, new()
}
}
obj.FileLocation = filePath;
obj.Migrate();
return obj;
}

Expand Down
77 changes: 77 additions & 0 deletions SoundSwitch/Framework/Configuration/Device/DeviceInfo.cs
@@ -0,0 +1,77 @@
using System;
using NAudio.CoreAudioApi;
using Newtonsoft.Json;

namespace SoundSwitch.Framework.Configuration.Device
{
public class DeviceInfo : IEquatable<DeviceInfo>, IComparable<DeviceInfo>
{
public string Name { get; }
public string Id { get; }
public DataFlow Type { get; }

[JsonConstructor]
public DeviceInfo(string name, string id, DataFlow type)
{
Name = name;
Id = id;
Type = type;
}

public DeviceInfo(MMDevice device)
{
Name = device.FriendlyName;
Id = device.ID;
Type = device.DataFlow;
}

public bool Equals(DeviceInfo other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Type == other.Type && (string.Equals(Id, other.Id) || string.Equals(Name, other.Name));
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((DeviceInfo) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = (int) Type + 1;
hashCode = (hashCode * 397) ^ (Id != null ? Id.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
return hashCode;
}
}

public static bool operator ==(DeviceInfo left, DeviceInfo right)
{
return Equals(left, right);
}

public static bool operator !=(DeviceInfo left, DeviceInfo right)
{
return !Equals(left, right);
}


public int CompareTo(DeviceInfo other)
{
if (ReferenceEquals(this, other)) return 0;
if (ReferenceEquals(null, other)) return 1;
var typeComparison = Type.CompareTo(other.Type);
if (typeComparison != 0) return typeComparison;
var idComparison = string.Compare(Id, other.Id, StringComparison.Ordinal);
if (idComparison != 0) return idComparison;
return string.Compare(Name, other.Name, StringComparison.Ordinal);
}

}
}
4 changes: 4 additions & 0 deletions SoundSwitch/Framework/Configuration/IPCConfiguration.cs
Expand Up @@ -52,5 +52,9 @@ public void Save()
ConfigurationManager.SaveConfiguration(this);
}

public void Migrate()
{

}
}
}
Expand Up @@ -12,7 +12,9 @@
* GNU General Public License for more details.
********************************************************************/

using System;
using System.Collections.Generic;
using SoundSwitch.Framework.Configuration.Device;
using SoundSwitch.Framework.DeviceCyclerManager;
using SoundSwitch.Framework.NotificationManager;
using SoundSwitch.Framework.TooltipInfoManager.TootipInfo;
Expand All @@ -23,8 +25,14 @@ namespace SoundSwitch.Framework.Configuration
{
public interface ISoundSwitchConfiguration : IConfiguration
{

[Obsolete]
HashSet<string> SelectedPlaybackDeviceListId { get; }
[Obsolete]
HashSet<string> SelectedRecordingDeviceListId { get; }

HashSet<DeviceInfo> SelectedDevices { get; }

bool FirstRun { get; set; }
HotKeys PlaybackHotKeys { get; set; }
HotKeys RecordingHotKeys { get; set; }
Expand Down
32 changes: 25 additions & 7 deletions SoundSwitch/Framework/Configuration/SoundSwitchConfiguration.cs
Expand Up @@ -14,8 +14,11 @@

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using Serilog;
using SoundSwitch.Framework.Configuration.Device;
using SoundSwitch.Framework.DeviceCyclerManager;
using SoundSwitch.Framework.NotificationManager;
using SoundSwitch.Framework.TooltipInfoManager.TootipInfo;
Expand Down Expand Up @@ -49,18 +52,14 @@ public SoundSwitchConfiguration()
SelectedRecordingDeviceListId = new HashSet<string>();
PlaybackHotKeys = new HotKeys(Keys.F11, HotKeys.ModifierKeys.Alt | HotKeys.ModifierKeys.Control);
RecordingHotKeys = new HotKeys(Keys.F7, HotKeys.ModifierKeys.Alt | HotKeys.ModifierKeys.Control);
}

/*TODO: Remove in next VERSION (3.12.8)*/
public UpdateState UpdateState
{
set {
UpdateMode = value == UpdateState.Steath ? UpdateMode.Silent : UpdateMode.Notify;
}
SelectedDevices = new HashSet<DeviceInfo>();
}


public HashSet<string> SelectedPlaybackDeviceListId { get; }
public HashSet<string> SelectedRecordingDeviceListId { get; }
public HashSet<DeviceInfo> SelectedDevices { get; }
public bool FirstRun { get; set; }
public HotKeys PlaybackHotKeys { get; set; }
public HotKeys RecordingHotKeys { get; set; }
Expand All @@ -78,6 +77,25 @@ public UpdateState UpdateState

// Needed by Interface
public string FileLocation { get; set; }
/// <summary>
/// Migrate configuration to a new schema
/// </summary>
public void Migrate()
{
if (SelectedPlaybackDeviceListId.Count > 0)
{
SelectedDevices.UnionWith(
SelectedPlaybackDeviceListId.Select((s => new DeviceInfo("", s, DataFlow.Render))));
SelectedPlaybackDeviceListId.Clear();
}

if (SelectedRecordingDeviceListId.Count > 0)
{
SelectedDevices.UnionWith(
SelectedRecordingDeviceListId.Select((s => new DeviceInfo("", s, DataFlow.Capture))));
SelectedRecordingDeviceListId.Clear();
}
}

public void Save()
{
Expand Down
51 changes: 10 additions & 41 deletions SoundSwitch/Model/AppModel.cs
Expand Up @@ -23,6 +23,7 @@
using SoundSwitch.Framework;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Framework.Configuration;
using SoundSwitch.Framework.Configuration.Device;
using SoundSwitch.Framework.DeviceCyclerManager;
using SoundSwitch.Framework.Updater;
using SoundSwitch.Framework.NotificationManager;
Expand Down Expand Up @@ -100,40 +101,29 @@ public bool IncludeBetaVersions
}
}

public HashSet<string> SelectedPlaybackDevicesList => AppConfigs.Configuration.SelectedPlaybackDeviceListId;

public HashSet<DeviceInfo> SelectedDevices { get; } = AppConfigs.Configuration.SelectedDevices;

public ICollection<MMDevice> AvailablePlaybackDevices
{
get
{
using (var devices = ActiveAudioDeviceLister.GetPlaybackDevices())
{
return
devices
.Join(SelectedPlaybackDevicesList,
a => a.ID,
selected => selected,
(a, selected) => a)
.ToList();
return devices.Where((device) => SelectedDevices.Any((info => new DeviceInfo(device).Equals(info)))).ToList();
}
}
}

public HashSet<string> SelectedRecordingDevicesList => AppConfigs.Configuration.SelectedRecordingDeviceListId;


public ICollection<MMDevice> AvailableRecordingDevices
{
get
{
using (var devices = ActiveAudioDeviceLister.GetRecordingDevices())
{
return devices
.Join(SelectedRecordingDevicesList,
a => a.ID,
selected => selected,
(a, selected) => a)
.ToList();
return devices.Where((device) => SelectedDevices.Any((info => new DeviceInfo(device).Equals(info)))).ToList();
}
}
}
Expand Down Expand Up @@ -300,19 +290,8 @@ public bool SelectDevice(MMDevice device)
{
var result = false;
DeviceListChanged eventChanged = null;
switch ((DeviceType)device.DataFlow)
{
case DeviceType.Playback:
result = SelectedPlaybackDevicesList.Add(device.ID);
eventChanged = new DeviceListChanged(SelectedPlaybackDevicesList, (DeviceType)device.DataFlow);
break;
case DeviceType.Recording:
result = SelectedRecordingDevicesList.Add(device.ID);
eventChanged = new DeviceListChanged(SelectedRecordingDevicesList, (DeviceType)device.DataFlow);
break;
default:
throw new ArgumentOutOfRangeException();
}
result = SelectedDevices.Add(new DeviceInfo(device));
eventChanged = new DeviceListChanged(SelectedDevices, (DeviceType)device.DataFlow);

if (result)
{
Expand All @@ -330,20 +309,10 @@ public bool SelectDevice(MMDevice device)
public bool UnselectDevice(MMDevice device)
{
var result = false;
var deviceToRemove = new DeviceInfo(device);
DeviceListChanged eventChanged = null;
switch ((DeviceType)device.DataFlow)
{
case DeviceType.Playback:
result = SelectedPlaybackDevicesList.Remove(device.ID);
eventChanged = new DeviceListChanged(SelectedPlaybackDevicesList, (DeviceType)device.DataFlow);
break;
case DeviceType.Recording:
result = SelectedRecordingDevicesList.Remove(device.ID);
eventChanged = new DeviceListChanged(SelectedRecordingDevicesList, (DeviceType)device.DataFlow);
break;
default:
throw new ArgumentOutOfRangeException();
}
result = SelectedDevices.RemoveWhere((info => info.Equals(deviceToRemove))) > 0;
eventChanged = new DeviceListChanged(SelectedDevices, (DeviceType)device.DataFlow);

if (result)
{
Expand Down
5 changes: 3 additions & 2 deletions SoundSwitch/Model/Events.cs
Expand Up @@ -17,6 +17,7 @@
using AudioDefaultSwitcherWrapper;
using NAudio.CoreAudioApi;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Framework.Configuration.Device;
using SoundSwitch.Framework.NotificationManager;
using SoundSwitch.Framework.Updater;

Expand All @@ -35,13 +36,13 @@ public ExceptionEvent(Exception exception)

public class DeviceListChanged : EventArgs
{
public DeviceListChanged(IEnumerable<string> seletedDevicesList, DeviceType type)
public DeviceListChanged(IEnumerable<DeviceInfo> seletedDevicesList, DeviceType type)
{
SeletedDevicesList = seletedDevicesList;
Type = type;
}

public IEnumerable<string> SeletedDevicesList { get; private set; }
public IEnumerable<DeviceInfo> SeletedDevicesList { get; private set; }
public DeviceType Type { get; private set; }
}

Expand Down
12 changes: 4 additions & 8 deletions SoundSwitch/Model/IAppModel.cs
Expand Up @@ -19,6 +19,7 @@
using NAudio.CoreAudioApi;
using SoundSwitch.Framework;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Framework.Configuration.Device;
using SoundSwitch.Framework.NotificationManager;
using SoundSwitch.Framework.Updater;
using SoundSwitch.Localization;
Expand All @@ -30,21 +31,16 @@ public interface IAppModel
{
#region Properties


/// <summary>
/// The list of Playback device selected to be used for Switching default devices.
/// Devices selected for Switching
/// </summary>
HashSet<string> SelectedPlaybackDevicesList { get; }

HashSet<DeviceInfo> SelectedDevices { get; }
/// <summary>
/// An union between the Active <see cref="IAudioDevice" /> of Windows and <see cref="SelectedPlaybackDevicesList" />
/// </summary>
ICollection<MMDevice> AvailablePlaybackDevices { get; }

/// <summary>
/// The list of Recording device selected to be used for Switching default devices.
/// </summary>
HashSet<string> SelectedRecordingDevicesList { get; }

/// <summary>
/// An union between the Active <see cref="IAudioDevice" /> of Windows and <see cref="SelectedRecordingDevicesList" />
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions SoundSwitch/SoundSwitch.csproj
Expand Up @@ -257,6 +257,7 @@
<Compile Include="Framework\AutoStart.cs" />
<Compile Include="Framework\Banner\BannerData.cs" />
<Compile Include="Framework\Banner\BannerManager.cs" />
<Compile Include="Framework\Configuration\Device\DeviceInfo.cs" />
<Compile Include="Framework\Configuration\IPCConfiguration.cs" />
<Compile Include="Framework\Configuration\AppConfigs.cs" />
<Compile Include="Framework\Configuration\ConfigurationManager.cs" />
Expand Down

0 comments on commit 388b311

Please sign in to comment.