Skip to content

Commit

Permalink
Some more refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyCrazy committed Oct 28, 2015
1 parent cd74868 commit 96bcb7b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 87 deletions.
10 changes: 5 additions & 5 deletions SpotifyAPI.Example/LocalControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ public void Connect()
{
if (!SpotifyLocalAPI.IsSpotifyRunning())
{
MessageBox.Show("Spotify isn't running!");
MessageBox.Show(@"Spotify isn't running!");
return;
}
if (!SpotifyLocalAPI.IsSpotifyWebHelperRunning())
{
MessageBox.Show("SpotifyWebHelper isn't running!");
MessageBox.Show(@"SpotifyWebHelper isn't running!");
return;
}

bool successful = _spotify.Connect();
if (successful)
{
connectBtn.Text = "Connection to Spotify successful";
connectBtn.Text = @"Connection to Spotify successful";
connectBtn.Enabled = false;
UpdateInfos();
_spotify.ListenForEvents = true;
}
else
{
DialogResult res = MessageBox.Show("Couldn't connect to the spotify client. Retry?", "Spotify", MessageBoxButtons.YesNo);
DialogResult res = MessageBox.Show(@"Couldn't connect to the spotify client. Retry?", @"Spotify", MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
Connect();
}
Expand Down Expand Up @@ -109,7 +109,7 @@ private void _spotify_OnVolumeChange(VolumeChangeEventArgs e)

private void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e)
{
timeLabel.Text = FormatTime(e.TrackTime) + "/" + FormatTime(_currentTrack.Length);
timeLabel.Text = $"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}";
timeProgressBar.Value = (int)e.TrackTime;
}

Expand Down
4 changes: 2 additions & 2 deletions SpotifyAPI.Example/WebControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ private void _auth_OnResponseReceivedEvent(Token token, string state)

if (state != "XSS")
{
MessageBox.Show("Wrong state received.", "SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(@"Wrong state received.", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (token.Error != null)
{
MessageBox.Show("Error: " + token.Error, "SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Error: {token.Error}", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion SpotifyAPI/Local/RemoteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal async void SendPlayRequest()
internal async void SendPlayRequest(String url, String context = "")
{
// TODO: instead of having an empty context, one way to fix the bug with the playback time beyond the length of a song would be to provide a 1-song context, and it would be fixed.
await QueryAsync(string.Format("remote/play.json?uri={0}&context={1}", url, context), true, true, -1);
await QueryAsync($"remote/play.json?uri={url}&context={context}", true, true, -1);
}

internal async void SendQueueRequest(String url)
Expand Down
3 changes: 1 addition & 2 deletions SpotifyAPI/Local/SpotifyLocalAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.InteropServices;
using System.Timers;
Expand Down Expand Up @@ -216,7 +215,7 @@ public void SetSpotifyVolume(float volume = 100)
if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
if (volume < 0 || volume > 100)
throw new ArgumentOutOfRangeException("Volume parameter has to be a value between 0 and 100");
throw new ArgumentOutOfRangeException(nameof(volume));
VolumeMixerControl.SetSpotifyVolume(volume);
}

Expand Down
64 changes: 30 additions & 34 deletions SpotifyAPI/Local/VolumeMixerControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace SpotifyAPI.Local
{
internal class VolumeMixerControl
{
private const String SPOTIFY_PROCESS_NAME = "spotify";
private const String SpotifyProcessName = "spotify";

internal static float GetSpotifyVolume()
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");

Expand All @@ -19,7 +19,6 @@ internal static float GetSpotifyVolume()
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}

Expand All @@ -31,7 +30,7 @@ internal static float GetSpotifyVolume()

internal static bool IsSpotifyMuted()
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");

Expand All @@ -40,7 +39,6 @@ internal static bool IsSpotifyMuted()
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}

Expand All @@ -52,7 +50,7 @@ internal static bool IsSpotifyMuted()

internal static void SetSpotifyVolume(float level)
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");

Expand All @@ -61,7 +59,6 @@ internal static void SetSpotifyVolume(float level)
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}

Expand All @@ -72,7 +69,7 @@ internal static void SetSpotifyVolume(float level)

internal static void MuteSpotify(bool mute)
{
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
if (p.Length == 0)
throw new Exception("Spotify process is not running or was not found!");

Expand All @@ -81,7 +78,6 @@ internal static void MuteSpotify(bool mute)
ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null)
{
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}

Expand All @@ -93,14 +89,14 @@ internal static void MuteSpotify(bool mute)
private static ISimpleAudioVolume GetVolumeObject(int pid)
{
// get the speakers (1st render + multimedia) device
IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
IMMDevice speakers;
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator)(new MMDeviceEnumerator());
IMmDevice speakers;
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out speakers);

// activate the session manager. we need the enumerator
Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
object o;
speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
speakers.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out o);
IAudioSessionManager2 mgr = (IAudioSessionManager2)o;

// enumerate sessions for on this device
Expand All @@ -121,7 +117,7 @@ private static ISimpleAudioVolume GetVolumeObject(int pid)

if (cpid == pid)
{
volumeControl = ctl as ISimpleAudioVolume;
volumeControl = (ISimpleAudioVolume) ctl;
break;
}
Marshal.ReleaseComObject(ctl);
Expand All @@ -142,31 +138,31 @@ private class MMDeviceEnumerator

private enum EDataFlow
{
eRender,
eCapture,
eAll,
EDataFlow_enum_count
ERender,
ECapture,
EAll,
EDataFlowEnumCount
}

private enum ERole
{
eConsole,
eMultimedia,
eCommunications,
ERole_enum_count
EConsole,
EMultimedia,
ECommunications,
ERoleEnumCount
}

[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IMMDeviceEnumerator
private interface IMmDeviceEnumerator
{
int NotImpl1();

[PreserveSig]
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMMDevice ppDevice);
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMmDevice ppDevice);
}

[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IMMDevice
private interface IMmDevice
{
[PreserveSig]
int Activate(ref Guid iid, int dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
Expand All @@ -180,30 +176,30 @@ private interface IAudioSessionManager2
int NotImpl2();

[PreserveSig]
int GetSessionEnumerator(out IAudioSessionEnumerator SessionEnum);
int GetSessionEnumerator(out IAudioSessionEnumerator sessionEnum);
}

[Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IAudioSessionEnumerator
{
[PreserveSig]
int GetCount(out int SessionCount);
int GetCount(out int sessionCount);

[PreserveSig]
int GetSession(int SessionCount, out IAudioSessionControl2 Session);
int GetSession(int sessionCount, out IAudioSessionControl2 session);
}

[Guid("87CE5498-68D6-44E5-9215-6DA47EF883D8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ISimpleAudioVolume
{
[PreserveSig]
int SetMasterVolume(float fLevel, ref Guid EventContext);
int SetMasterVolume(float fLevel, ref Guid eventContext);

[PreserveSig]
int GetMasterVolume(out float pfLevel);

[PreserveSig]
int SetMute(bool bMute, ref Guid EventContext);
int SetMute(bool bMute, ref Guid eventContext);

[PreserveSig]
int GetMute(out bool pbMute);
Expand All @@ -219,19 +215,19 @@ private interface IAudioSessionControl2
int GetDisplayName([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);

[PreserveSig]
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)]string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)]string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);

[PreserveSig]
int GetIconPath([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);

[PreserveSig]
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);

[PreserveSig]
int GetGroupingParam(out Guid pRetVal);

[PreserveSig]
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);

[PreserveSig]
int NotImpl1();
Expand Down
13 changes: 6 additions & 7 deletions SpotifyAPI/Web/Auth/AutorizationCodeAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ public void StartHttpServer(int port = 80)

private void HttpServerOnOnAuth(AuthEventArgs e)
{
if (OnResponseReceivedEvent != null)
OnResponseReceivedEvent(new AutorizationCodeAuthResponse()
{
Code = e.Code,
State = e.State,
Error = e.Error
});
OnResponseReceivedEvent?.Invoke(new AutorizationCodeAuthResponse()
{
Code = e.Code,
State = e.State,
Error = e.Error
});
}

/// <summary>
Expand Down
15 changes: 7 additions & 8 deletions SpotifyAPI/Web/Auth/ImplicitGrantAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ public void StartHttpServer(int port = 80)

private void HttpServerOnOnAuth(AuthEventArgs e)
{
if (OnResponseReceivedEvent != null)
OnResponseReceivedEvent(new Token
{
AccessToken = e.Code,
TokenType = e.TokenType,
ExpiresIn = e.ExpiresIn,
Error = e.Error
}, e.State);
OnResponseReceivedEvent?.Invoke(new Token
{
AccessToken = e.Code,
TokenType = e.TokenType,
ExpiresIn = e.ExpiresIn,
Error = e.Error
}, e.State);
}

/// <summary>
Expand Down
Loading

0 comments on commit 96bcb7b

Please sign in to comment.