UpdateTextAndEmptyFilesMaybe() 공개 정적인 메소드

public static UpdateTextAndEmptyFilesMaybe ( string text ) : void
text string
리턴 void
예제 #1
0
 private void Snip_FormClosing(object sender, FormClosingEventArgs e)
 {
     // Empty file, clear artwork and save settings automatically when the form is being closed.
     TextHandler.UpdateTextAndEmptyFilesMaybe(LocalizedMessages.NoTrackPlaying);
     Globals.CurrentPlayer.SaveBlankImage();
     Settings.Save();
 }
예제 #2
0
        private void TogglePlayer(Globals.MediaPlayerSelection player)
        {
            this.toolStripMenuItemSpotify.Checked = player == Globals.MediaPlayerSelection.Spotify;
            this.toolStripMenuItemItunes.Checked  = player == Globals.MediaPlayerSelection.Itunes;

            Globals.CurrentPlayer.Unload();
            string playerName = string.Empty;

            switch (player)
            {
            case Globals.MediaPlayerSelection.Spotify:
                Globals.CurrentPlayer = new Spotify();
                playerName            = LocalizedMessages.Spotify;
                break;

            case Globals.MediaPlayerSelection.Itunes:
                Globals.CurrentPlayer = new Itunes();
                playerName            = LocalizedMessages.Itunes;
                break;

            default:
                break;
            }

            Globals.CurrentPlayer.Load();

            Globals.PlayerSelection = player;
            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedMessages.SwitchedToPlayer,
                    playerName));
        }
예제 #3
0
        public override void Update()
        {
            Process[] processes = Process.GetProcessesByName("vlc");

            if (processes.Length > 0)
            {
                string vlcTitle = string.Empty;

                foreach (Process process in processes)
                {
                    vlcTitle = process.MainWindowTitle;
                }

                // Check for a hyphen in the title. If a hyphen exists then we need to cut all of the text after the last
                // hyphen because that's the "VLC media player" text, which can vary based on language.
                // If no hyphen exists then VLC is not playing anything.
                int lastHyphen = vlcTitle.LastIndexOf("-", StringComparison.OrdinalIgnoreCase);

                if (lastHyphen > 0)
                {
                    vlcTitle = vlcTitle.Substring(0, lastHyphen).Trim();

                    string vlcExtension = System.IO.Path.GetExtension(vlcTitle);

                    if (Globals.SaveAlbumArtwork)
                    {
                        this.SaveBlankImage();
                    }

                    // Filter file extension
                    // VLC doesn't always have file extensions show in the title.
                    // The previous code would sometimes cut song titles prematurely if there was no extension in the title.
                    // If there's an extension, we'll trim it. Otherwise, we won't do anything to the string.
                    if (vlcExtension.Length > 0)
                    {
                        int lastDot = vlcTitle.LastIndexOf(vlcExtension);

                        if (lastDot > 0)
                        {
                            vlcTitle = vlcTitle.Substring(0, lastDot).Trim();
                        }
                    }

                    TextHandler.UpdateText(vlcTitle);
                }
                else
                {
                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                }
            }
            else
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));
            }
        }
예제 #4
0
        private void App_OnPlayerStopEvent(object o)
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(LocalizedMessages.NoTrackPlaying);
        }
예제 #5
0
파일: iTunes.cs 프로젝트: jaylapham/Snip
        private void App_OnPlayerStopEvent(object o)
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
        }
예제 #6
0
파일: iTunes.cs 프로젝트: jaylapham/Snip
        private void App_OnPlayerQuittingEvent()
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("iTunesNotRunning"));
        }
예제 #7
0
        private void ResetSinceSpotifyIsNotRunning()
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("SpotifyIsNotRunning"));

            this.Found      = false;
            this.NotRunning = true;
        }
예제 #8
0
        public override void Update()
        {
            Process[] processes = Process.GetProcessesByName("vlc");

            if (processes.Length > 0)
            {
                string vlcTitle = string.Empty;

                foreach (Process process in processes)
                {
                    vlcTitle = process.MainWindowTitle;
                }

                // Check for a hyphen in the title. If a hyphen exists then we need to cut all of the text after the last
                // hyphen because that's the "VLC media player" text, which can vary based on language.
                // If no hyphen exists then VLC is not playing anything.
                int lastHyphen = vlcTitle.LastIndexOf("-", StringComparison.OrdinalIgnoreCase);

                if (lastHyphen > 0)
                {
                    vlcTitle = vlcTitle.Substring(0, lastHyphen).Trim();

                    if (Globals.SaveAlbumArtwork)
                    {
                        this.SaveBlankImage();
                    }

                    // Filter file extension
                    int lastDot = vlcTitle.LastIndexOf('.');

                    if (lastDot > 0)
                    {
                        vlcTitle = vlcTitle.Substring(0, lastDot).Trim();
                    }

                    TextHandler.UpdateText(vlcTitle);
                }
                else
                {
                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                }
            }
            else
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));
            }
        }
예제 #9
0
파일: Spotify.cs 프로젝트: hylianux/Snip
 private void ResetSnipSinceSpotifyIsNotPlaying()
 {
     // Prevent writing a blank image if we already did
     if (!this.SavedBlankImage)
     {
         if (Globals.SaveAlbumArtwork)
         {
             this.SaveBlankImage();
         }
     }
     this.spotifyProcess = null;
     TextHandler.UpdateTextAndEmptyFilesMaybe(LocalizedMessages.NoTrackPlaying);
 }
예제 #10
0
파일: iTunes.cs 프로젝트: piccolo456/Snip
        private void App_OnPlayerQuittingEvent()
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Globals.ResourceManager.GetString("PlayerIsNotRunning"),
                    Globals.ResourceManager.GetString("iTunes")));
        }
예제 #11
0
        private void App_OnPlayerQuittingEvent()
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedMessages.PlayerIsNotRunning,
                    LocalizedMessages.iTunes));
        }
예제 #12
0
파일: Snip.cs 프로젝트: toilsomesea/Snip
        private void ToggleVLC()
        {
            this.toolStripMenuItemSpotify.Checked    = false;
            this.toolStripMenuItemItunes.Checked     = false;
            this.toolStripMenuItemWinamp.Checked     = false;
            this.toolStripMenuItemFoobar2000.Checked = false;
            this.toolStripMenuItemVlc.Checked        = true;

            Globals.CurrentPlayer.Unload();
            Globals.CurrentPlayer = new VLC();
            Globals.CurrentPlayer.Load();

            Globals.PlayerSelection = Globals.MediaPlayerSelection.VLC;
            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("SwitchedToVLC"));
        }
예제 #13
0
        private void UpdateCSRFTokenTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (this.spotifyPort > 0)
            {
                // CSRF token path
                string csrfAddress = "/simplecsrf/token.json";

                string downloadedJson = this.DownloadJson(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "{0}:{1}{2}",
                        this.spotilocalAddress,
                        this.spotifyPort,
                        csrfAddress),
                    SpotifyAddressContactType.CSRF);

                // Set the token to be blank until filled
                this.csrfToken = string.Empty;

                if (!string.IsNullOrEmpty(downloadedJson))
                {
                    dynamic jsonSummary = SimpleJson.DeserializeObject(downloadedJson);

                    if (jsonSummary != null)
                    {
                        // If Spotify is running this value will be null
                        if (jsonSummary.running == null)
                        {
                            this.csrfToken = jsonSummary.token.ToString();
                            this.updateCSRFTokenTimer.Interval = 3600 * 1000; // We got what we wanted
                        }
                        else
                        {
                            this.ResetSnipSinceSpotifyIsNotRunning();
                            this.updateCSRFTokenTimer.Interval = 1000; // Run continously until token is obtained
                        }
                    }
                }
            }
            else
            {
                // Let the user know that we're searching for SpotifyWebHelper.
                TextHandler.UpdateTextAndEmptyFilesMaybe(LocalizedMessages.LocatingSpotifyWebHelper);

                this.DetectSpotifyWebHelperPort();
            }
        }
예제 #14
0
        private void ResetSnipSinceSpotifyIsNotRunning()
        {
            if (this.spotifyWindowFound)
            {
                this.spotifyWindowFound = false;
            }

            // Prevent writing a blank image if we already did
            if (!this.SavedBlankImage)
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("SpotifyIsNotRunning"));
        }
예제 #15
0
        private void ResetSnipSinceSpotifyIsNotPlaying()
        {
            if (!this.snipReset)
            {
                // Prevent writing a blank image if we already did
                if (!this.SavedBlankImage)
                {
                    if (Globals.SaveAlbumArtwork)
                    {
                        this.SaveBlankImage();
                    }
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));

                this.LastTitle = string.Empty;

                this.snipReset = true;
            }
        }
예제 #16
0
        private void ResetSnipSinceGPMDPIsNotPlaying()
        {
            if (!this.gpmdpReset)
            {
                // Prevent writing a blank image if we already did
                if (!this.SavedBlankImage)
                {
                    if (Globals.SaveAlbumArtwork)
                    {
                        this.SaveBlankImage();
                    }
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(LocalizedMessages.NoTrackPlaying);

                this.LastTitle = string.Empty;

                this.gpmdpReset = true;
            }
        }
예제 #17
0
파일: Snip.cs 프로젝트: piccolo456/Snip
        private void ToggleQuodLibet()
        {
            this.toolStripMenuItemSpotify.Checked    = false;
            this.toolStripMenuItemItunes.Checked     = false;
            this.toolStripMenuItemWinamp.Checked     = false;
            this.toolStripMenuItemFoobar2000.Checked = false;
            this.toolStripMenuItemVlc.Checked        = false;
            this.toolStripMenuItemGPMDP.Checked      = false;
            this.toolStripMenuItemQuodLibet.Checked  = true;

            Globals.CurrentPlayer.Unload();
            Globals.CurrentPlayer = new QuodLibet();
            Globals.CurrentPlayer.Load();

            Globals.PlayerSelection = Globals.MediaPlayerSelection.QuodLibet;
            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Globals.ResourceManager.GetString("SwitchedToPlayer"),
                    Globals.ResourceManager.GetString("QuodLibet")));
        }
예제 #18
0
파일: Snip.cs 프로젝트: trentbraidner/Snip
        private void ToggleGPMDP()
        {
            this.toolStripMenuItemSpotify.Checked    = false;
            this.toolStripMenuItemItunes.Checked     = false;
            this.toolStripMenuItemWinamp.Checked     = false;
            this.toolStripMenuItemFoobar2000.Checked = false;
            this.toolStripMenuItemVlc.Checked        = false;
            this.toolStripMenuItemGPMDP.Checked      = true;
            this.toolStripMenuItemQuodLibet.Checked  = false;

            Globals.CurrentPlayer.Unload();
            Globals.CurrentPlayer = new GPMDP();
            Globals.CurrentPlayer.Load();

            Globals.PlayerSelection = Globals.MediaPlayerSelection.GPMDP;
            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedMessages.SwitchedToPlayer,
                    LocalizedMessages.GPMDP));
        }
예제 #19
0
파일: Spotify.cs 프로젝트: Deddie94/Snip
        private void ResetSnipSinceSpotifyIsNotRunning()
        {
            // Prevent writing a blank image if we already did
            if (!this.SavedBlankImage)
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }
            }

            this.Handle            = IntPtr.Zero;
            this.processId         = 0;
            this.moduleBaseAddress = 0;
            this.moduleLength      = 0;

            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedMessages.PlayerIsNotRunning,
                    LocalizedMessages.Spotify));
        }
예제 #20
0
        private void ResetSnipSinceSpotifyIsNotRunning()
        {
            if (this.spotifyWindowFound)
            {
                this.spotifyWindowFound = false;
            }

            // Prevent writing a blank image if we already did
            if (!this.SavedBlankImage)
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }
            }

            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedMessages.PlayerIsNotRunning,
                    LocalizedMessages.Spotify));
        }
예제 #21
0
파일: VLC.cs 프로젝트: psyrax/Snip
        public override void Update()
        {
            if (!this.Found)
            {
                this.Handle = UnsafeNativeMethods.FindWindow("QWidget", "VLC media player");

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string vlcTitle = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update the system tray text and text file text if the title changes.
                        if (vlcTitle != this.LastTitle)
                        {
                            if (vlcTitle.Equals("VLC media player"))
                            {
                                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                            }
                            else
                            {
                                string windowTitleFull = vlcTitle.Replace(" - VLC media player", string.Empty);

                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                TextHandler.UpdateText(windowTitleFull.Trim());
                            }

                            this.LastTitle = vlcTitle;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));

                            this.Found      = false;
                            this.NotRunning = true;
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));

                        this.Found      = false;
                        this.NotRunning = true;
                    }
                }
            }
        }
예제 #22
0
        public override void Update()
        {
            if (!this.Found)
            {
                this.Handle = UnsafeNativeMethods.FindWindow("SpotifyMainWindow", null);

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string spotifyTitle = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update if the title has actually changed.
                        // This prevents unnecessary calls and downloads.
                        if (spotifyTitle != this.LastTitle)
                        {
                            if (spotifyTitle == "Spotify")
                            {
                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                            }
                            else
                            {
                                this.DownloadJson(spotifyTitle);

                                dynamic jsonSummary = SimpleJson.DeserializeObject(this.json);

                                if (jsonSummary != null)
                                {
                                    var numberOfResults = jsonSummary.tracks.total;

                                    if (numberOfResults > 0)
                                    {
                                        jsonSummary = SimpleJson.DeserializeObject(jsonSummary.tracks["items"].ToString());

                                        TextHandler.UpdateText(
                                            jsonSummary[0].name.ToString(),
                                            jsonSummary[0].artists[0].name.ToString(),
                                            jsonSummary[0].album.name.ToString());

                                        if (Globals.SaveAlbumArtwork)
                                        {
                                            this.HandleSpotifyAlbumArtwork(jsonSummary[0].name.ToString());
                                        }
                                    }
                                    else
                                    {
                                        // In the event of an advertisement (or any song that returns 0 results)
                                        // then we'll just write the whole title as a single string instead.
                                        TextHandler.UpdateText(spotifyTitle);
                                    }
                                }
                            }

                            this.LastTitle = spotifyTitle;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            this.ResetSinceSpotifyIsNotRunning();
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        this.ResetSinceSpotifyIsNotRunning();
                    }
                }
            }
        }
예제 #23
0
파일: Spotify.cs 프로젝트: dxball/Snip
        private void UpdateSpotifyTrackInformation_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Get a list of all spotify.exe processes
            Process[] processes = Process.GetProcessesByName("spotify");

            // Array must be greater than 0 to continue (as in a process was successfully found)
            if (processes.Length > 0)
            {
                // We know Spotify is running at least
                this.spotifyRunning = true;

                // Only loop through and get the process handle if it's not set
                if (this.spotifyHandle == IntPtr.Zero)
                {
                    // Loop through all processes that matched spotify.exe
                    foreach (Process localSpotifyProcess in processes)
                    {
                        StringBuilder className = new StringBuilder(64);

                        UnsafeNativeMethods.GetClassName(localSpotifyProcess.MainWindowHandle, className, className.Capacity);

                        string classNameText = className.ToString();

                        // Spotify's main window that has the song name in the titlebar uses the Chrome_WidgetWin_0 class
                        // We also need to verify that the titlebar has something in it due to multiple processes sharing the same class
                        // From everything I've seen the titlebar will _always_ have at least some text
                        if (classNameText == "Chrome_WidgetWin_0" && localSpotifyProcess.MainWindowTitle.Length > 0)
                        {
                            this.spotifyHandle = localSpotifyProcess.MainWindowHandle;
                        }
                    }
                }
            }
            else
            {
                this.ResetSnipSinceSpotifyIsNotPlaying();
            }

            if (this.spotifyRunning == true && this.spotifyHandle != IntPtr.Zero)
            {
                // We'll limit the window title string to 256 characters
                // The length really doesn't matter since we're just storing what appears and comparing it
                // Even if it gets cut off it should compare well up to 256 characters
                StringBuilder stringBuilder = new StringBuilder(256);

                // Get the window title of Spotify and store it in stringBuilder
                UnsafeNativeMethods.GetWindowText(this.spotifyHandle, stringBuilder, stringBuilder.Capacity);

                // Convert the StringBuilder to a regular string
                string spotifyTitle = stringBuilder.ToString();

                if (spotifyTitle.Length > 0)
                {
                    if (spotifyTitle != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                    {
                        Globals.RewriteUpdatedOutputFormat = false;

                        if (spotifyTitle == "Spotify" || spotifyTitle == "Spotify Free" || spotifyTitle == "Spotify Premium")
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                        }
                        else
                        {
                            string downloadedJson = this.DownloadJson("https://api.spotify.com/v1/me/player/currently-playing", SpotifyAddressContactType.API);

                            if (!string.IsNullOrEmpty(downloadedJson))
                            {
                                dynamic jsonSummary = SimpleJson.DeserializeObject(downloadedJson);

                                string currentlyPlayingType = (string)jsonSummary.currently_playing_type;

                                // Spotify does not provide any detailed information for anything other than actual songs.
                                // Podcasts have types of "episode" but do not contain any useful data unfortunately.
                                if (currentlyPlayingType == "track")
                                {
                                    bool   isPlaying = (bool)jsonSummary.is_playing;
                                    string trackId   = (string)jsonSummary.item.id;

                                    if (isPlaying)
                                    {
                                        if (Globals.CacheSpotifyMetadata)
                                        {
                                            downloadedJson = this.ReadCachedJson(trackId);
                                        }

                                        // If there are multiple artists we want to join all of them together for display
                                        string artists = string.Empty;

                                        foreach (dynamic artist in jsonSummary.item.artists)
                                        {
                                            artists += artist.name.ToString() + ", ";
                                        }

                                        artists = artists.Substring(0, artists.LastIndexOf(',')); // Removes last comma

                                        TextHandler.UpdateText(
                                            jsonSummary.item.name.ToString(),
                                            artists,
                                            jsonSummary.item.album.name.ToString(),
                                            trackId,
                                            downloadedJson);

                                        if (Globals.SaveAlbumArtwork)
                                        {
                                            this.DownloadSpotifyAlbumArtwork(jsonSummary.item.album);
                                        }
                                    }
                                    else
                                    {
                                        this.ResetSnipSinceSpotifyIsNotPlaying();
                                    }
                                }
                                else
                                {
                                    this.ResetSnipSinceSpotifyIsNotPlaying();
                                }

                                // Reset timer after it was potentially changed by rate limit
                                // Since we should only reach this point if valid JSON was obtained this means
                                // that the timer shouldn't reset unless there was a success.
                                this.updateSpotifyTrackInformation.Enabled  = false;
                                this.updateSpotifyTrackInformation.Interval = updateSpotifyTrackInformationDefaultInterval;
                                this.updateSpotifyTrackInformation.Enabled  = true;
                            }
                        }

                        this.LastTitle = spotifyTitle;
                    }
                }
            }
        }
예제 #24
0
        public override void Update()
        {
            if (!this.Found)
            {
                System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("foobar2000");

                if (processes.Length > 0)
                {
                    this.Handle = processes[0].MainWindowHandle;
                }

                foreach (var process in processes)
                {
                    process.Dispose();
                }

                processes = null;

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string foobar2000Title = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update the system tray text and text file text if the title changes.
                        if (foobar2000Title != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                        {
                            Globals.RewriteUpdatedOutputFormat = false;

                            if (foobar2000Title.StartsWith("foobar2000", StringComparison.OrdinalIgnoreCase))
                            {
                                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                            }
                            else
                            {
                                // Winamp window titles look like "[%album artist% - ]['['%album%[ CD%discnumber%][ #%tracknumber%]']' ]%title%[ '//' %track artist%]".
                                // Require that the user use ATF and replace the format with something like:
                                // %artist% – %title%
                                string   windowTitleFull = System.Text.RegularExpressions.Regex.Replace(foobar2000Title, @"\s+\[foobar2000.+\]", string.Empty);
                                string[] windowTitle     = windowTitleFull.Split('–');

                                // Album artwork not supported by foobar2000
                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                if (windowTitle.Length > 1)
                                {
                                    string artist    = windowTitle[0].Trim();
                                    string songTitle = windowTitle[1].Trim();

                                    TextHandler.UpdateText(songTitle, artist);
                                }
                                else
                                {
                                    TextHandler.UpdateText(windowTitle[0].Trim());
                                }
                            }

                            this.LastTitle = foobar2000Title;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("foobar2000IsNotRunning"));

                            this.Found      = false;
                            this.NotRunning = true;
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("foobar2000IsNotRunning"));

                        this.Found      = false;
                        this.NotRunning = true;
                    }
                }
            }
        }
예제 #25
0
파일: Winamp.cs 프로젝트: piccolo456/Snip
        public override void Update()
        {
            if (!this.Found)
            {
                this.Handle = UnsafeNativeMethods.FindWindow("Winamp v1.x", null);

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string winampTitle = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update the system tray text and text file text if the title changes.
                        if (winampTitle != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                        {
                            Globals.RewriteUpdatedOutputFormat = false;

                            if (winampTitle.Contains("- Winamp [Stopped]") || winampTitle.Contains("- Winamp [Paused]"))
                            {
                                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                            }
                            else
                            {
                                // Winamp window titles look like "#. Artist - Track - Winamp".
                                // Require that the user use ATF and replace the format with something like:
                                // %artist% – %title%
                                string   windowTitleFull = winampTitle.Replace("- Winamp", string.Empty);
                                string[] windowTitle     = windowTitleFull.Split('–');

                                // Album artwork not supported by Winamp
                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                if (windowTitle.Length > 1)
                                {
                                    string artist    = windowTitle[0].Trim();
                                    string songTitle = windowTitle[1].Trim();

                                    TextHandler.UpdateText(songTitle, artist);
                                }
                                else
                                {
                                    TextHandler.UpdateText(windowTitle[0].Trim());
                                }
                            }

                            this.LastTitle = winampTitle;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            TextHandler.UpdateTextAndEmptyFilesMaybe(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    Globals.ResourceManager.GetString("PlayerIsNotRunning"),
                                    Globals.ResourceManager.GetString("Winamp")));

                            this.Found      = false;
                            this.NotRunning = true;
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        TextHandler.UpdateTextAndEmptyFilesMaybe(
                            string.Format(
                                CultureInfo.InvariantCulture,
                                Globals.ResourceManager.GetString("PlayerIsNotRunning"),
                                Globals.ResourceManager.GetString("Winamp")));

                        this.Found      = false;
                        this.NotRunning = true;
                    }
                }
            }
        }
예제 #26
0
파일: Spotify.cs 프로젝트: PlumpMath/Snip
        public override void Update()
        {
            // There's no sense in doing anything anymore without a valid token.
            if (!string.IsNullOrEmpty(this.token))
            {
                if (!this.Found)
                {
                    this.Handle = UnsafeNativeMethods.FindWindow("SpotifyMainWindow", null);

                    this.Found      = true;
                    this.NotRunning = false;
                }
                else
                {
                    // Make sure the process is still valid.
                    if (this.Handle != IntPtr.Zero && this.Handle != null)
                    {
                        int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                        string spotifyTitle = this.Title.ToString();

                        this.Title.Clear();

                        // If the window title length is 0 then the process handle is not valid.
                        if (windowTextLength > 0)
                        {
                            // Only update if the title has actually changed.
                            // This prevents unnecessary calls and downloads.
                            if (spotifyTitle != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                            {
                                Globals.RewriteUpdatedOutputFormat = false;

                                if (spotifyTitle == "Spotify")
                                {
                                    if (Globals.SaveAlbumArtwork)
                                    {
                                        this.SaveBlankImage();
                                    }

                                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                                }
                                else
                                {
                                    this.DownloadJson(spotifyTitle);

                                    if (!string.IsNullOrEmpty(this.json))
                                    {
                                        dynamic jsonSummary = SimpleJson.DeserializeObject(this.json);

                                        if (jsonSummary != null)
                                        {
                                            var numberOfResults = jsonSummary.tracks.total;

                                            if (numberOfResults > 0)
                                            {
                                                jsonSummary = SimpleJson.DeserializeObject(jsonSummary.tracks["items"].ToString());

                                                int mostPopular = SelectTrackByPopularity(jsonSummary, spotifyTitle);

                                                TextHandler.UpdateText(
                                                    jsonSummary[mostPopular].name.ToString(),
                                                    jsonSummary[mostPopular].artists[0].name.ToString(),
                                                    jsonSummary[mostPopular].album.name.ToString(),
                                                    jsonSummary[mostPopular].id.ToString());

                                                if (Globals.SaveAlbumArtwork)
                                                {
                                                    this.DownloadSpotifyAlbumArtwork(jsonSummary[mostPopular].album);
                                                }
                                            }
                                            else
                                            {
                                                // In the event of an advertisement (or any song that returns 0 results)
                                                // then we'll just write the whole title as a single string instead.
                                                TextHandler.UpdateText(spotifyTitle);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // For whatever reason the JSON file couldn't download
                                        // In the event this happens we'll just display Spotify's window title as the track
                                        TextHandler.UpdateText(spotifyTitle);
                                    }
                                }

                                this.LastTitle = spotifyTitle;
                            }
                        }
                        else
                        {
                            if (!this.NotRunning)
                            {
                                this.ResetSinceSpotifyIsNotRunning();
                            }
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            this.ResetSinceSpotifyIsNotRunning();
                        }
                    }
                }
            }
        }
예제 #27
0
파일: VLC.cs 프로젝트: jaylapham/Snip
        public override void Update()
        {
            Process[] processes = Process.GetProcessesByName("vlc");

            if (processes.Length > 0)
            {
                string vlcTitle = string.Empty;

                foreach (Process process in processes)
                {
                    vlcTitle = process.MainWindowTitle;
                }


                // Check for a hyphen in the title. If a hyphen exists then we need to cut all of the text after the last
                // hyphen because that's the "VLC media player" text, which can vary based on language.
                // If no hyphen exists then VLC is not playing anything.
                int lastHyphen = vlcTitle.LastIndexOf("-", StringComparison.OrdinalIgnoreCase);

                if (lastHyphen > 0)
                {
                    vlcTitle = vlcTitle.Substring(0, lastHyphen).Trim();

                    if (Globals.SaveAlbumArtwork)
                    {
                        this.SaveBlankImage();
                    }

                    // Filter file extension
                    // Using the previous method of using System.IO.Path to grab the file extension caused some problems.
                    // It treated the title as a path, restricting what characters were allowed in the titles.
                    // I changed it back to a similar version of the old method.
                    // Now we'll check if the section of the title after the dot is greater than 4 characters, and less than 5 (the dot is included)
                    // This is done because common file extensions are typically 3 characters long, with some exceptions like flac and aiff being 4.
                    // Additionally, we'll check if there's a space anywhere after the last dot. Extensions will not have spaces in them.
                    //
                    // Alternatively, you can use System.IO.Path, make it system dependent, and replace characters like " and | with
                    // equivalents.
                    //
                    // TODO:
                    // It may be best to just remove common extensions by name, i.e. cut off ".mp3", ".flac", etc.
                    int lastDot = vlcTitle.LastIndexOf(".", StringComparison.OrdinalIgnoreCase);
                    if (lastDot > 0)
                    {
                        string vlcTitleExtension = vlcTitle.Substring(lastDot);
                        if (vlcTitleExtension.Length >= 4 && vlcTitleExtension.Length <= 5 && !vlcTitleExtension.Contains(" "))
                        {
                            vlcTitle = vlcTitle.Substring(0, lastDot).Trim();
                        }
                    }

                    TextHandler.UpdateText(vlcTitle);
                }
                else
                {
                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                }
            }
            else
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));
            }
        }
예제 #28
0
파일: Spotify.cs 프로젝트: hylianux/Snip
        private void UpdateSpotifyTrackInformation_Elapsed(object sender, ElapsedEventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("spotify");
            foreach (Process localSpotifyProcess in processes)
            {
                // There's probably a better way to do this but for the sake of getting this going this will work
                // Spotify creates a lot of processes... but only one is actually going to have text in the window title.
                if (localSpotifyProcess.MainWindowTitle.Length > 0)
                {
                    this.spotifyProcess = localSpotifyProcess;
                }
            }

            string spotifyTitle = this.spotifyProcess.MainWindowTitle;

            if (spotifyTitle.Length > 0)
            {
                if (spotifyTitle != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                {
                    Globals.RewriteUpdatedOutputFormat = false;

                    if (spotifyTitle == "Spotify" || spotifyTitle == "Spotify Free" || spotifyTitle == "Spotify Premium")
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                    }
                    else
                    {
                        string downloadedJson = this.DownloadJson("https://api.spotify.com/v1/me/player/currently-playing", SpotifyAddressContactType.API);

                        if (!string.IsNullOrEmpty(downloadedJson))
                        {
                            dynamic jsonSummary = SimpleJson.DeserializeObject(downloadedJson);

                            string currentlyPlayingType = (string)jsonSummary.currently_playing_type;

                            // Spotify does not provide any detailed information for anything other than actual songs.
                            // Podcasts have types of "episode" but do not contain any useful data unfortunately.
                            if (currentlyPlayingType == "track")
                            {
                                bool   isPlaying = (bool)jsonSummary.is_playing;
                                string trackId   = (string)jsonSummary.item.id;

                                if (isPlaying)
                                {
                                    if (Globals.CacheSpotifyMetadata)
                                    {
                                        downloadedJson = this.ReadCachedJson(trackId);
                                    }

                                    // If there are multiple artists we want to join all of them together for display
                                    string artists = string.Empty;

                                    foreach (dynamic artist in jsonSummary.item.artists)
                                    {
                                        artists += artist.name.ToString() + ", ";
                                    }

                                    artists = artists.Substring(0, artists.LastIndexOf(',')); // Removes last comma

                                    TextHandler.UpdateText(
                                        jsonSummary.item.name.ToString(),
                                        artists,
                                        jsonSummary.item.album.name.ToString(),
                                        trackId,
                                        downloadedJson);

                                    if (Globals.SaveAlbumArtwork)
                                    {
                                        this.DownloadSpotifyAlbumArtwork(jsonSummary.item.album);
                                    }
                                }
                                else
                                {
                                    this.ResetSnipSinceSpotifyIsNotPlaying();
                                }
                            }
                            else
                            {
                                this.ResetSnipSinceSpotifyIsNotPlaying();
                            }

                            // Reset timer after it was potentially changed by rate limit
                            // Since we should only reach this point if valid JSON was obtained this means
                            // that the timer shouldn't reset unless there was a success.
                            this.updateSpotifyTrackInformation.Enabled  = false;
                            this.updateSpotifyTrackInformation.Interval = updateSpotifyTrackInformationDefaultInterval;
                            this.updateSpotifyTrackInformation.Enabled  = true;
                        }
                    }

                    this.LastTitle = spotifyTitle;
                }
            }
        }
예제 #29
0
        private void TogglePlayer(Globals.MediaPlayerSelection player)
        {
            this.toolStripMenuItemSpotify.Checked    = player == Globals.MediaPlayerSelection.Spotify;
            this.toolStripMenuItemItunes.Checked     = player == Globals.MediaPlayerSelection.iTunes;
            this.toolStripMenuItemWinamp.Checked     = player == Globals.MediaPlayerSelection.Winamp;
            this.toolStripMenuItemFoobar2000.Checked = player == Globals.MediaPlayerSelection.foobar2000;
            this.toolStripMenuItemVlc.Checked        = player == Globals.MediaPlayerSelection.VLC;
            this.toolStripMenuItemGPMDP.Checked      = player == Globals.MediaPlayerSelection.GPMDP;
            this.toolStripMenuItemQuodLibet.Checked  = player == Globals.MediaPlayerSelection.QuodLibet;

            Globals.CurrentPlayer.Unload();
            string playerName = string.Empty;

            switch (player)
            {
            case Globals.MediaPlayerSelection.Spotify:
                Globals.CurrentPlayer = new Spotify();
                playerName            = LocalizedMessages.Spotify;
                break;

            case Globals.MediaPlayerSelection.iTunes:
                Globals.CurrentPlayer = new iTunes();
                playerName            = LocalizedMessages.iTunes;
                break;

            case Globals.MediaPlayerSelection.Winamp:
                Globals.CurrentPlayer = new Winamp();
                playerName            = LocalizedMessages.Winamp;
                break;

            case Globals.MediaPlayerSelection.foobar2000:
                Globals.CurrentPlayer = new foobar2000();
                playerName            = LocalizedMessages.foobar2000;
                break;

            case Globals.MediaPlayerSelection.VLC:
                Globals.CurrentPlayer = new VLC();
                playerName            = LocalizedMessages.VLC;
                break;

            case Globals.MediaPlayerSelection.GPMDP:
                Globals.CurrentPlayer = new GPMDP();
                playerName            = LocalizedMessages.GPMDP;
                break;

            case Globals.MediaPlayerSelection.QuodLibet:
                Globals.CurrentPlayer = new QuodLibet();
                playerName            = LocalizedMessages.QuodLibet;
                break;

            default:
                break;
            }

            Globals.CurrentPlayer.Load();

            Globals.PlayerSelection = player;
            TextHandler.UpdateTextAndEmptyFilesMaybe(
                string.Format(
                    CultureInfo.InvariantCulture,
                    LocalizedMessages.SwitchedToPlayer,
                    playerName));
        }