예제 #1
0
        private void CheckTitle()
        {
            string currentTitle = Spotify.GetCurrentTrack();

            if (!string.IsNullOrEmpty(currentTitle) && currentTitle != previousTitle)
            {
                string part1, part2;

                // set the previous title asap so that the next timer call to this function will
                // fail fast (setting it at the end may cause multiple web requests)
                previousTitle = currentTitle;

                if (SplitTitle(currentTitle, out part1, out part2))
                {
                    this.Dispatcher.Invoke((Action) delegate { Title1.Text = part2; Title2.Text = part1; }, System.Windows.Threading.DispatcherPriority.Normal);

                    foreach (var p in this.Plugins)
                    {
                        try
                        {
                            p.TrackChanged(part1, part2);
                        }
                        catch (Exception)
                        {
                            //For now we swallow any plugin errors.
                        }
                    }
                }

                try
                {
                    //Use the iTunes API to get the cover art
                    String url  = "https://itunes.apple.com/search?term=" + part2 + "&attribute=songTerm&entity=album";
                    var    json = new WebClient().DownloadString(url);

                    iTunesResult deserializedJson = JsonConvert.DeserializeObject <iTunesResult>(json);

                    //Filter tracks by artist
                    List <iTunesTrack> resultsForArtist = getResultsForArtist(deserializedJson, part1);

                    //Get the oldest track (filters out all the "best of" albums, etc.)
                    iTunesTrack oldestTrack = getOldestTitle(resultsForArtist);

                    coverUrl = oldestTrack.artworkUrl100;
                }
                catch (Exception)
                {
                    coverUrl = "SpotifyToastifyLogo.png";
                }

                this.Dispatcher.Invoke((Action) delegate { FadeIn(); }, System.Windows.Threading.DispatcherPriority.Normal);
            }
        }
예제 #2
0
        private void CheckTitle()
        {
            string currentTitle = Spotify.GetCurrentTrack();

            if (!string.IsNullOrEmpty(currentTitle) && currentTitle != previousTitle)
            {
                string artist, title;

                // set the previous title asap so that the next timer call to this function will
                // fail fast (setting it at the end may cause multiple web requests)
                previousTitle = currentTitle;

                if (SplitTitle(currentTitle, out artist, out title))
                {
                    this.Dispatcher.Invoke((Action) delegate { Title1.Text = title; Title2.Text = artist; }, System.Windows.Threading.DispatcherPriority.Normal);

                    foreach (var p in this.Plugins)
                    {
                        try
                        {
                            p.TrackChanged(artist, title);
                        }
                        catch (Exception)
                        {
                            //For now we swallow any plugin errors.
                        }
                    }
                }

                CheckTitle(artist, title);

                this.Dispatcher.Invoke((Action) delegate { FadeIn(); }, System.Windows.Threading.DispatcherPriority.Normal);

                if (SettingsXml.Current.SaveTrackToFile)
                {
                    if (!string.IsNullOrEmpty(SettingsXml.Current.SaveTrackToFilePath))
                    {
                        try
                        {
                            string trackText = GetClipboardText(currentTitle);

                            File.WriteAllText(SettingsXml.Current.SaveTrackToFilePath, trackText);
                        }
                        catch { } // ignore errors writing out the album
                    }
                }
            }
        }
예제 #3
0
        internal static void ActionHookCallback(Hotkey hotkey)
        {
            // Bug 9421: ignore this keypress if it is the same as the previous one and it's been less than
            //           WAIT_BETWEEN_HOTKEY_PRESS since the last press. Note that we do not update
            //           _lastHotkeyPressTime in this case to avoid being trapped in a never ending cycle of
            //           ignoring keypresses if the user (for some reason) decides to press really quickly,
            //           really often on the hotkey
            if (hotkey == _lastHotkey && DateTime.Now.Subtract(_lastHotkeyPressTime).TotalMilliseconds < WAIT_BETWEEN_HOTKEY_PRESS)
            {
                return;
            }

            _lastHotkey          = hotkey;
            _lastHotkeyPressTime = DateTime.Now;

            string currentTrack = string.Empty;

            try
            {
                string trackBeforeAction = Spotify.GetCurrentTrack();
                if (hotkey.Action == SpotifyAction.CopyTrackInfo && !string.IsNullOrEmpty(trackBeforeAction))
                {
                    CopySongToClipboard(trackBeforeAction);
                }
                else if (hotkey.Action == SpotifyAction.PasteTrackInfo && !string.IsNullOrEmpty(trackBeforeAction))
                {
                    CopySongToClipboard(trackBeforeAction);

                    SendPasteKey(hotkey);
                }
                else
                {
                    Spotify.SendAction(hotkey.Action);
                }

                Toast.Current.DisplayAction(hotkey.Action, trackBeforeAction);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debugger.Break();
                System.Diagnostics.Debug.WriteLine("Exception with hooked key! " + ex);
                Toast.Current.Title1.Text = "Unable to communicate with Spotify";
                Toast.Current.Title2.Text = "";
                Toast.Current.FadeIn();
            }
        }
예제 #4
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;
            }
        }