/** * Contains the logic for when to mute Spotify **/ private void MainTimer_Tick(object sender, EventArgs e) { try { if (Process.GetProcessesByName("spotify").Length < 1) { if (exitTolerance > 20) { File.AppendAllText(logPath, "Spotify process not found\r\n"); Notify("Spotify not found, please restart SpotBlocker."); if (exitTolerance > 22) { Notify("Exiting SpotBlocker."); Application.Exit(); } } exitTolerance += 1; } else { exitTolerance = 0; } WebHelperResult whr = WebHelperHook.GetStatus(); if (whr.isAd) // Track is ad { MainTimer.Interval = 1000; if (whr.isPlaying) { Debug.WriteLine("Ad is playing"); if (lastArtistName != whr.artistName) { if (!muted) { Mute(1); } artistTooltip.SetToolTip(StatusLabel, StatusLabel.Text = "Muting ad"); lastArtistName = whr.artistName; LogAction("/mute/" + whr.artistName); Debug.WriteLine("Blocked " + whr.artistName); } } else // Ad is paused { Debug.WriteLine("Ad is paused"); Resume(); } } else if (whr.isPrivateSession) { if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: *Private Session*"; artistTooltip.SetToolTip(StatusLabel, ""); lastArtistName = whr.artistName; MessageBox.Show("Please disable 'Private Session' on Spotify for SpotBlocker to function properly.", "SpotBlocker", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); } } else if (!whr.isRunning) { StatusLabel.Text = "Spotify is not running"; artistTooltip.SetToolTip(StatusLabel, ""); //Notify("Error connecting to Spotify. Retrying..."); File.AppendAllText(logPath, "Not running.\r\n"); MainTimer.Interval = 5000; /* * MainTimer.Enabled = false; * MessageBox.Show("Spotify is not running. Please restart SpotBlocker after starting Spotify.", "SpotBlocker", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); * StatusLabel.Text = "Spotify is not running"; * Application.Exit(); */ } else if (!whr.isPlaying) { StatusLabel.Text = "Spotify is paused"; artistTooltip.SetToolTip(StatusLabel, lastArtistName = ""); } else // Song is playing { if (muted) { Mute(0); } if (MainTimer.Interval > 600) { MainTimer.Interval = 600; } if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: " + ShortenName(whr.artistName); artistTooltip.SetToolTip(StatusLabel, lastArtistName = whr.artistName); } } } catch (Exception ex) { Debug.WriteLine(ex); File.AppendAllText(logPath, ex.Message); } }
private void Main_Load(object sender, EventArgs e) { // Enable web helper if (File.Exists(spotifyPrefsPath)) { string[] lines = File.ReadAllLines(spotifyPrefsPath); bool webhelperEnabled = false; for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("webhelper.enabled")) { if (lines[i].Contains("false")) { lines[i] = "webhelper.enabled=true"; File.WriteAllLines(spotifyPrefsPath, lines); } webhelperEnabled = true; break; } } if (!webhelperEnabled) { File.AppendAllText(spotifyPrefsPath, "\r\nwebhelper.enabled=true"); } } CheckUpdate(); // Start Spotify and give SpotBlocker higher priority try { if (File.Exists(spotifyPath) && Process.GetProcessesByName("spotify").Length < 1) { Process.Start(spotifyPath); } Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; // Windows throttles down when minimized to task tray, so make sure SpotBlocker runs smoothly // Check for open.spotify.com in hosts String hostsContent = File.ReadAllText(hostsPath); if (hostsContent.Contains("open.spotify.com")) { if (IsUserAnAdmin()) { File.WriteAllText(hostsPath, hostsContent.Replace("open.spotify.com", "localhost")); MessageBox.Show("An SpotBlocker patch has been applied to your hosts file. If SpotBlocker is stuck at 'Loading', please restart your computer.", "SpotBlocker", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("SpotBlocker has detected an error in your hosts file.\r\n\r\nPlease re-run SpotBlocker as Administrator or remove 'open.spotify.com' from your hosts file.", "SpotBlocker", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } } catch (Exception ex) { Debug.WriteLine(ex); } // Extract dependencies try { if (!File.Exists(nircmdPath)) { File.WriteAllBytes(nircmdPath, Properties.Resources.nircmd32); } if (!File.Exists(jsonPath)) { File.WriteAllBytes(jsonPath, Properties.Resources.Newtonsoft_Json); } if (!File.Exists(coreaudioPath)) { File.WriteAllBytes(coreaudioPath, Properties.Resources.CoreAudio); } } catch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show("Error loading SpotBlocker dependencies. Please run SpotBlocker as administrator or put SpotBlocker in a user folder."); } // Set up UI SpotifyMuteCheckbox.Checked = Properties.Settings.Default.SpotifyMute; StartMinimizedCheckbox.Checked = Properties.Settings.Default.StartMinimized; if (File.Exists(hostsPath)) { string hostsFile = File.ReadAllText(hostsPath); BlockBannersCheckbox.Checked = adHosts.All(host => hostsFile.Contains("0.0.0.0 " + host)); } RegistryKey startupKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (startupKey.GetValue("SpotBlocker") != null) { if (startupKey.GetValue("SpotBlocker").ToString() == "\"" + Application.ExecutablePath + "\"") { StartupCheckbox.Checked = true; } else // Reg value exists, but not in right path { startupKey.DeleteValue("SpotBlocker"); } } // Google Analytics rnd = new Random(Environment.TickCount); starttime = DateTime.Now.Ticks; if (string.IsNullOrEmpty(Properties.Settings.Default.UID)) { Properties.Settings.Default.UID = rnd.Next(100000000, 999999999).ToString(); // Build unique visitorId; Properties.Settings.Default.Save(); } visitorId = Properties.Settings.Default.UID; File.AppendAllText(logPath, "-----------\r\n"); bool unsafeHeaders = WebHelperHook.SetAllowUnsafeHeaderParsing20(); Debug.WriteLine("Unsafe Headers: " + unsafeHeaders); if (!hasNet45()) { if (MessageBox.Show("You do not have .NET Framework 4.5. Download now?", "SpotBlocker Error", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=30653"); } else { MessageBox.Show("SpotBlocker may not function properly without .NET Framework 4.5 or above."); } } Mute(0); MainTimer.Enabled = true; if (Process.GetProcessesByName("spotifywebhelper").Length < 1) { Notify("Please enable 'Allow Spotify to be opened from the web' in your Spotify 'Preferences' -> 'Advanced settings'."); } // Load Images backgroundImages = GetBackgroundImages(); // Assign new image pnlMain.BackgroundImage = GetRandomTitleImage(); // Minimize at start if (Properties.Settings.Default.StartMinimized) { SendToTray(); } LogAction("/launch"); }