예제 #1
0
파일: Spotify.cs 프로젝트: xorguy/Toastify
        private static void ShowSpotify()
        {
            if (Spotify.IsAvailable())
            {
                var hWnd = Spotify.GetSpotify();

                // check Spotify's current window state
                var placement = new Win32.WINDOWPLACEMENT();
                Win32.GetWindowPlacement(hWnd, ref placement);

                int showCommand = Win32.Constants.SW_SHOW;

                // if Spotify is minimzed we need to send a restore so that the window
                // will come back exactly like it was before being minimized (i.e. maximized
                // or otherwise) otherwise if we call SW_RESTORE on a currently maximized window
                // then instead of staying maximized it will return to normal size.
                if (placement.showCmd == Win32.Constants.SW_SHOWMINIMIZED)
                {
                    showCommand = Win32.Constants.SW_RESTORE;
                }

                Win32.ShowWindow(hWnd, showCommand);

                Win32.SetForegroundWindow(hWnd);
                Win32.SetFocus(hWnd);
            }
        }
예제 #2
0
 private static void ShowSpotify()
 {
     if (Spotify.IsAvailable())
     {
         Win32.ShowWindow(Spotify.GetSpotify(), 1);
         Win32.SetForegroundWindow(Spotify.GetSpotify());
         Win32.SetFocus(Spotify.GetSpotify());
     }
 }
예제 #3
0
파일: Spotify.cs 프로젝트: xorguy/Toastify
        public static string GetCurrentTrack()
        {
            if (!Spotify.IsAvailable())
            {
                return(string.Empty);
            }

            string song   = "";
            string artist = "";

            try
            {
                var links = _spotifyDriver.FindElementsByTagName("a");

                foreach (var link in links)
                {
                    if (string.IsNullOrEmpty(link.Text))
                    {
                        continue;
                    }

                    // TODO: could use CSS selectors?
                    var databind = link.GetAttribute("data-bind");

                    if (databind == null)
                    {
                        continue;
                    }

                    System.Diagnostics.Debug.WriteLine(databind);

                    if (databind.Contains("href: trackURI"))
                    {
                        song = link.Text;
                    }
                    else if (databind.Contains("trackURI") && link.GetAttribute("href").Contains("artist"))
                    {
                        artist = link.Text;
                    }
                }

                return(artist + " - " + song);
            }
            catch
            {
                // exceptions will occur if the Spotify content changes while it's being enumerated
                // for example, if the song occurs while we're looking for the song title
                return("");
            }
        }
예제 #4
0
        public static string GetCurrentTrack()
        {
            if (!Spotify.IsAvailable())
            {
                return(string.Empty);
            }

            IntPtr        hWnd   = GetSpotify();
            int           length = Win32.GetWindowTextLength(hWnd);
            StringBuilder sb     = new StringBuilder(length + 1);

            Win32.GetWindowText(hWnd, sb, sb.Capacity);
            return(sb.ToString().Replace("Spotify", "").TrimStart(' ', '-').Trim());
        }
예제 #5
0
파일: Spotify.cs 프로젝트: xorguy/Toastify
        private static bool IsMinimized()
        {
            if (!Spotify.IsAvailable())
            {
                return(false);
            }

            var hWnd = Spotify.GetSpotify();

            // check Spotify's current window state
            var placement = new Win32.WINDOWPLACEMENT();

            Win32.GetWindowPlacement(hWnd, ref placement);

            return(placement.showCmd == Win32.Constants.SW_SHOWMINIMIZED);
        }
예제 #6
0
        private void EnsureSpotify()
        {
            SettingsXml settings = SettingsXml.Current;

            //Make sure Spotify is running when starting Toastify.
            //If not ask the user and try to start it.

            if (!Spotify.IsAvailable())
            {
                if ((settings.AlwaysStartSpotify.HasValue && settings.AlwaysStartSpotify.Value) || (MessageBox.Show("Spotify doesn't seem to be running.\n\nDo you want Toastify to try and start it for you?", "Toastify", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes))
                {
                    string spotifyPath = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Spotify", string.Empty, string.Empty) as string;  //string.Empty = (Default) value

                    // try in the secondary location
                    if (string.IsNullOrEmpty(spotifyPath))
                    {
                        spotifyPath = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\Spotify", "InstallLocation", string.Empty) as string;  //string.Empty = (Default) value
                    }

                    if (string.IsNullOrEmpty(spotifyPath))
                    {
                        MessageBox.Show("Unable to find Spotify. Make sure it is installed and/or start it manually.", "Toastify", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        try
                        {
                            System.Diagnostics.Process.Start(System.IO.Path.Combine(spotifyPath, "Spotify.exe"));

                            if (!settings.AlwaysStartSpotify.HasValue)
                            {
                                var ret = MessageBox.Show("Do you always want to start Spotify if it's not already running?", "Toastify", MessageBoxButton.YesNo, MessageBoxImage.Question);
                                settings.AlwaysStartSpotify = (ret == MessageBoxResult.Yes);
                                settings.Save();
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("An unknown error occurd when trying to start Spotify.\nPlease start Spotify manually.", "Toastify", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                }
            }
        }
예제 #7
0
        public static void SendAction(SpotifyAction a)
        {
            if (!Spotify.IsAvailable())
            {
                return;
            }

            switch (a)
            {
            case SpotifyAction.CopyTrackInfo:
            case SpotifyAction.ShowToast:
                //Nothing
                break;

            case SpotifyAction.ShowSpotify:
                ShowSpotify();
                break;

            default:
                Win32.SendMessage(GetSpotify(), Win32.Constants.WM_APPCOMMAND, IntPtr.Zero, new IntPtr((long)a));
                break;
            }
        }
예제 #8
0
파일: Spotify.cs 프로젝트: xorguy/Toastify
        public static void SendAction(SpotifyAction a)
        {
            if (!Spotify.IsAvailable())
            {
                return;
            }

            // bah. Because control cannot fall through cases we need to special case volume
            if (SettingsXml.Current.ChangeSpotifyVolumeOnly)
            {
                if (a == SpotifyAction.VolumeUp)
                {
                    VolumeHelper.IncrementVolume("Spotify");
                    return;
                }
                else if (a == SpotifyAction.VolumeDown)
                {
                    VolumeHelper.DecrementVolume("Spotify");
                    return;
                }
                else if (a == SpotifyAction.Mute)
                {
                    VolumeHelper.ToggleApplicationMute("Spotify");
                    return;
                }
            }

            switch (a)
            {
            case SpotifyAction.CopyTrackInfo:
            case SpotifyAction.ShowToast:
                //Nothing
                break;

            case SpotifyAction.ShowSpotify:

                if (Spotify.IsMinimized())
                {
                    ShowSpotify();
                }
                else
                {
                    Minimize();
                }

                break;

            case SpotifyAction.ThumbsUp:
                ThumbsUp();
                break;

            case SpotifyAction.ThumbsDown:
                ThumbsDown();
                break;

            case SpotifyAction.FastForward:

                SendComplexKeys("+{Right}");
                break;

            case SpotifyAction.Rewind:

                SendComplexKeys("+{Left}");
                break;

            default:
                Win32.SendMessage(GetSpotify(), Win32.Constants.WM_APPCOMMAND, IntPtr.Zero, new IntPtr((long)a));
                break;
            }
        }
예제 #9
0
        public void DisplayAction(SpotifyAction action, string trackBeforeAction)
        {
            //Anything that changes track doesn't need to be handled since
            //that will be handled in the timer event.

            const string VOLUME_UP_TEXT   = "Volume ++";
            const string VOLUME_DOWN_TEXT = "Volume --";
            const string MUTE_ON_OFF_TEXT = "Mute On/Off";
            const string NOTHINGS_PLAYING = "Nothing's playing";
            const string PAUSED_TEXT      = "Paused";
            const string STOPPED_TEXT     = "Stopped";
            const string SETTINGS_TEXT    = "Settings saved";

            if (!Spotify.IsAvailable() && action != SpotifyAction.SettingsSaved)
            {
                coverUrl    = "SpotifyToastifyLogo.png";
                Title1.Text = "Spotify not available!";
                Title2.Text = string.Empty;
                FadeIn();
                return;
            }

            string currentTrack = Spotify.GetCurrentTrack();

            string prevTitle1 = Title1.Text;
            string prevTitle2 = Title2.Text;

            switch (action)
            {
            case SpotifyAction.PlayPause:
                if (!string.IsNullOrEmpty(trackBeforeAction))
                {
                    //We pressed pause
                    Title1.Text = "Paused";
                    Title2.Text = trackBeforeAction;
                    FadeIn();
                }
                previousTitle = string.Empty;      //If we presses play this will force a toast to display in next timer event.
                break;

            case SpotifyAction.Stop:
                previousTitle = string.Empty;
                Title1.Text   = "Stopped";
                Title2.Text   = trackBeforeAction;
                FadeIn();
                break;

            case SpotifyAction.SettingsSaved:
                Title1.Text = SETTINGS_TEXT;
                Title2.Text = "Here is a preview of your settings!";
                FadeIn();
                break;

            case SpotifyAction.NextTrack:          //No need to handle
                break;

            case SpotifyAction.PreviousTrack:      //No need to handle
                break;

            case SpotifyAction.VolumeUp:
                Title1.Text = VOLUME_UP_TEXT;
                Title2.Text = currentTrack;
                FadeIn();
                break;

            case SpotifyAction.VolumeDown:
                Title1.Text = VOLUME_DOWN_TEXT;
                Title2.Text = currentTrack;
                FadeIn();
                break;

            case SpotifyAction.Mute:
                Title1.Text = MUTE_ON_OFF_TEXT;
                Title2.Text = currentTrack;
                FadeIn();
                break;

            case SpotifyAction.ShowToast:
                if (string.IsNullOrEmpty(currentTrack) && Title1.Text != PAUSED_TEXT && Title1.Text != STOPPED_TEXT)
                {
                    coverUrl    = "SpotifyToastifyLogo.png";
                    Title1.Text = NOTHINGS_PLAYING;
                    Title2.Text = string.Empty;
                }
                else
                {
                    string part1, part2;
                    if (SplitTitle(currentTrack, out part1, out part2))
                    {
                        Title1.Text = part2;
                        Title2.Text = part1;
                    }
                }
                FadeIn(force: true);
                break;

            case SpotifyAction.ShowSpotify:      //No need to handle
                break;
            }
        }