TraktPlugin.TraktPlugin.GUIWindowManager_Receivers C# (CSharp) Method

GUIWindowManager_Receivers() private method

private GUIWindowManager_Receivers ( GUIMessage message ) : void
message MediaPortal.GUI.Library.GUIMessage
return void
        void GUIWindowManager_Receivers(GUIMessage message)
        {
            bool validWatchListItem = false;
            bool validCustomListItem = false;
            bool validRateItem = false;
            bool validShoutItem = false;
            bool validRelatedItem = false;
            bool validTraktMenuItem = false;
            bool validSearchItem = false;
            bool updatePluginFilters = false;
            string title = string.Empty;
            string year = string.Empty;
            string imdbid = string.Empty;
            string tmdbid = string.Empty;
            string showtvdbid = string.Empty;
            string epTvdbId = string.Empty;
            string season = string.Empty;
            string episode = string.Empty;
            string fanart = string.Empty;
            bool isWatched = false;
            SearchPeople searchPeople = null;
            string type = "movie";

            switch (message.Message)
            {
                case GUIMessage.MessageType.GUI_MSG_CLICKED:
                    switch (GUIWindowManager.ActiveWindow)
                    {
                        case (int)ExternalPluginWindows.OnlineVideos:
                            #region WatchList/CustomList Button
                            switch (message.SenderControlId)
                            {
                                case ((int)ExternalPluginControls.WatchList):
                                case ((int)ExternalPluginControls.CustomList):
                                    // Confirm we are in IMDB/iTunes Trailer Details view
                                    // This will give us enough information to send to trakt
                                    bool isDetails = GUIPropertyManager.GetProperty("#OnlineVideos.state").ToLowerInvariant() == "details";
                                    string siteUtil = GUIPropertyManager.GetProperty("#OnlineVideos.selectedSiteUtil").ToLowerInvariant();
                                    if (isDetails && (siteUtil == "imdb" || siteUtil == "itmovietrailers"))
                                    {
                                        title = GUIPropertyManager.GetProperty("#OnlineVideos.Details.Title").Trim();
                                        year = GUIPropertyManager.GetProperty("#OnlineVideos.Details.Year").Trim();
                                        if (siteUtil == "imdb")
                                        {
                                            // IMDb site exposes IMDb ID, use this to get a better match on trakt
                                            // this property is new, check for null in case user hasn't updated site
                                            imdbid = GUIPropertyManager.GetProperty("#OnlineVideos.Details.IMDbId");
                                            if (imdbid == null) imdbid = string.Empty;

                                            // could be a TV Show
                                            type = GUIPropertyManager.GetProperty("#OnlineVideos.Details.Type").ToLowerInvariant();
                                        }
                                        if ((!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(year)) || imdbid.StartsWith("tt"))
                                        {
                                            if (message.SenderControlId == (int)ExternalPluginControls.WatchList) validWatchListItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.CustomList) validCustomListItem = true;
                                        }
                                        // Return focus to details list now so we dont go in a loop
                                        GUIControl.FocusControl((int)ExternalPluginWindows.OnlineVideos, 51);
                                    }
                                    break;
                            }
                            #endregion
                            break;
                        case (int)ExternalPluginWindows.Showtimes:
                            #region WatchList/CustomList Button
                            switch (message.SenderControlId)
                            {
                                case ((int)ExternalPluginControls.WatchList):
                                case ((int)ExternalPluginControls.CustomList):
                                    // Confirm we are in Showtimes Details view
                                    // This will give us enough information to send to trakt
                                    bool isDetails = GUIWindowManager.GetWindow(GUIWindowManager.ActiveWindow).GetControl(24).Visible;
                                    if (isDetails)
                                    {
                                        title = GUIPropertyManager.GetProperty("#st_title").Trim();
                                        DateTime releaseDate = DateTime.MinValue;

                                        if (DateTime.TryParse(GUIPropertyManager.GetProperty("#st_releasedate").Trim(), out releaseDate))
                                        {
                                            year = releaseDate.Year.ToString();
                                        }

                                        imdbid = GUIPropertyManager.GetProperty("#st_imdb");
                                        if (imdbid == null) imdbid = string.Empty;

                                        tmdbid = GUIPropertyManager.GetProperty("#st_tmdb");
                                        if (tmdbid == null) imdbid = string.Empty;

                                        if ((!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(year)) || imdbid.StartsWith("tt") || !string.IsNullOrEmpty(tmdbid))
                                        {
                                            if (message.SenderControlId == (int)ExternalPluginControls.WatchList) validWatchListItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.CustomList) validCustomListItem = true;
                                        }
                                        // set focus to next button so we dont go in a loop
                                        GUIControl.FocusControl((int)ExternalPluginWindows.Showtimes, 42);
                                    }
                                    break;
                            }
                            #endregion
                            break;
                        case (int)ExternalPluginWindows.VideoInfo:
                            #region Watchlist/CustomList/Rate/Shouts/RelatedItem/SearchBy
                            switch (message.SenderControlId)
                            {
                                case ((int)ExternalPluginControls.WatchList):
                                case ((int)ExternalPluginControls.CustomList):
                                case ((int)ExternalPluginControls.Rate):
                                case ((int)ExternalPluginControls.Shouts):
                                case ((int)ExternalPluginControls.RelatedItems):
                                case ((int)ExternalPluginControls.SearchBy):
                                case ((int)ExternalPluginControls.TraktMenu):
                                    type = "movie";
                                    title = GUIPropertyManager.GetProperty("#title").Trim();
                                    year = GUIPropertyManager.GetProperty("#year").Trim();
                                    imdbid = GUIPropertyManager.GetProperty("#imdbnumber").Trim();

                                    MediaPortal.Util.FanArt.GetFanArtfilename(title, 0, out fanart);
                                    if (fanart.ToLowerInvariant().Equals("unknown"))
                                    {
                                        string movieid = GUIPropertyManager.GetProperty("#movieid").Trim();
                                        MediaPortal.Util.FanArt.GetFanArtfilename(movieid, 0, out fanart);
                                    }

                                    searchPeople = new SearchPeople();
                                    string people = GUIPropertyManager.GetProperty("#cast").Trim();
                                    if (people != string.Empty && people != "unknown")
                                    {
                                        // actors seperated by newlines
                                        var peopleAndRoles = people.Split('\n').Select(s => s.Trim());

                                        // each actor string also includes the role: {0} as {1} 

                                        // get the seperator from the localised string and then reverse the formatted string
                                        string roleSepString = GUILocalizeStrings.Get(1320).Split(' ')[1].Trim();

                                        foreach (var personAndRole in peopleAndRoles)
                                        {
                                            var personAndRoleStrings = personAndRole.Split(new string[] { string.Format(" {0} ", roleSepString) }, StringSplitOptions.None);
                                            searchPeople.Actors.Add(personAndRoleStrings.First());
                                        }
                                    }

                                    people = GUIPropertyManager.GetProperty("#director").Trim();
                                    if (people != string.Empty && people != "unknown") searchPeople.Directors.AddRange(people.Split(',').Select(s => s.Trim()));

                                    people = GUIPropertyManager.GetProperty("#credits").Trim();
                                    if (people != string.Empty && people != "unknown")
                                    {
                                        var writers = people.Split(',').Select(s => s.Trim());
                                        foreach(var writer in writers)
                                        {
                                            // remove the writer type e.g. (Story), (Screenplay)
                                            searchPeople.Writers.Add(writer.Split('(').First().Trim());
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(imdbid) || (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(year)))
                                    {
                                        if (message.SenderControlId == (int)ExternalPluginControls.WatchList) validWatchListItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.CustomList) validCustomListItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.Rate) validRateItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.Shouts) validShoutItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.RelatedItems) validRelatedItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.TraktMenu) validTraktMenuItem = true;
                                    }

                                    // Set focus to Play Button now so we dont go in a loop
                                    GUIControl.FocusControl((int)ExternalPluginWindows.VideoInfo, 2);
                                    break;
                            }
                            #endregion
                            break;
                        case (int)ExternalPluginWindows.MovingPictures:
                            #region WatchList/CustomList/Rate/Shouts/RelatedItem/Search
                            switch (message.SenderControlId)
                            {
                                case ((int)ExternalPluginControls.WatchList):
                                case ((int)ExternalPluginControls.CustomList):
                                case ((int)ExternalPluginControls.Rate):
                                case ((int)ExternalPluginControls.Shouts):
                                case ((int)ExternalPluginControls.RelatedItems):
                                case ((int)ExternalPluginControls.SearchBy):
                                case ((int)ExternalPluginControls.TraktMenu):
                                    type = "movie";
                                    updatePluginFilters = true;
                                    title = GUIPropertyManager.GetProperty("#MovingPictures.SelectedMovie.title").Trim();
                                    year = GUIPropertyManager.GetProperty("#MovingPictures.SelectedMovie.year").Trim();
                                    imdbid = GUIPropertyManager.GetProperty("#MovingPictures.SelectedMovie.imdb_id").Trim();
                                    fanart = GUIPropertyManager.GetProperty("#MovingPictures.SelectedMovie.backdropfullpath").Trim();
                                    isWatched = GUIPropertyManager.GetProperty("#MovingPictures.UserMovieSettings.watched").Trim() != "0";

                                    // get movie people from database
                                    searchPeople = new SearchPeople();
                                    if (TraktHelper.IsMovingPicturesAvailableAndEnabled)
                                    {
                                        int? movieID = null;
                                        int iYear = 0; int.TryParse(year, out iYear);
                                        if (MovingPictures.FindMovieID(title, iYear, imdbid, ref movieID))
                                            MovingPictures.GetMoviePersonInfo(movieID, out searchPeople);
                                    }

                                    if (!string.IsNullOrEmpty(imdbid) || (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(year)))
                                    {
                                        if (message.SenderControlId == (int)ExternalPluginControls.WatchList) validWatchListItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.CustomList) validCustomListItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.Rate) validRateItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.Shouts) validShoutItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.RelatedItems) validRelatedItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.TraktMenu) validTraktMenuItem = true;
                                        if (message.SenderControlId == (int)ExternalPluginControls.SearchBy) validSearchItem = true;
                                    }

                                    // Set focus to Play Button now so we dont go in a loop
                                    GUIControl.FocusControl((int)ExternalPluginWindows.MovingPictures, 6);
                                    break;
                            }
                            #endregion
                            break;
                        case (int)ExternalPluginWindows.TVSeries:
                            #region WatchList/CustomList/Rate/Shouts/Related
                            switch (message.SenderControlId)
                            {
                                case ((int)ExternalPluginControls.WatchList):
                                case ((int)ExternalPluginControls.CustomList):
                                case ((int)ExternalPluginControls.Rate):
                                case ((int)ExternalPluginControls.Shouts):
                                case ((int)ExternalPluginControls.RelatedItems):
                                case ((int)ExternalPluginControls.SearchBy):
                                case ((int)ExternalPluginControls.TraktMenu):
                                    Object obj = TVSeries.SelectedObject;
                                    bool validItem = false;
                                    if (obj != null)
                                    {
                                        searchPeople = new SearchPeople();

                                        switch (TVSeries.GetSelectedType(obj))
                                        {
                                            case TVSeries.SelectedType.Episode:
                                                type = "episode";
                                                validItem = TVSeries.GetEpisodeInfo(obj, out title, out year, out showtvdbid, out epTvdbId, out season, out episode, out isWatched);
                                                validItem |= TVSeries.GetEpisodePersonInfo(obj, out searchPeople);
                                                break;

                                            case TVSeries.SelectedType.Series:
                                                type = "series";
                                                validItem =  TVSeries.GetSeriesInfo(obj, out title, out year, out showtvdbid);
                                                validItem |= TVSeries.GetSeriesPersonInfo(obj, out searchPeople);
                                                break;

                                            default:
                                                break;
                                        }

                                        fanart = GUIPropertyManager.GetProperty("#TVSeries.Current.Fanart").Trim();

                                        if (validItem)
                                        {
                                            if (message.SenderControlId == (int)ExternalPluginControls.WatchList) validWatchListItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.CustomList) validCustomListItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.Rate) validRateItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.Shouts) validShoutItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.RelatedItems) validRelatedItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.SearchBy) validSearchItem = true;
                                            if (message.SenderControlId == (int)ExternalPluginControls.TraktMenu) validTraktMenuItem = true;
                                        }
                                    }

                                    // Set focus to Facade now so we dont go in a loop
                                    GUIControl.FocusControl((int)ExternalPluginWindows.TVSeries, 50);
                                    break;
                            }
                            #endregion
                            break;
                    }
                    break;

                default:
                    break;
            }

            #region Add To Watch List
            if (validWatchListItem)
            {
                if (type == "movie")
                {
                    if (GUIUtils.ShowYesNoDialog(Translation.WatchList, string.Format("{0}\n{1} ({2})", Translation.AddThisItemToWatchList, title, year), true))
                    {
                        TraktLogger.Info("Adding movie to Watchlist. Title = '{0}', Year = '{1}', IMDb ID = '{2}'", title, year, imdbid);
                        TraktHelper.AddMovieToWatchList(title, year.ToNullableInt32(), imdbid.ToNullIfEmpty(), tmdbid.ToNullableInt32(), updatePluginFilters);
                    }
                }
                else if (type == "show")
                {
                    if (GUIUtils.ShowYesNoDialog(Translation.WatchList, Translation.AddShowToWatchList, true))
                    {
                        TraktLogger.Info("Adding show to Watchlist. Title = '{0}', Year = '{1}', TVDb ID = '{2}'", title, year, showtvdbid);
                        TraktHelper.AddShowToWatchList(title, year.ToNullableInt32(), showtvdbid.ToNullableInt32(), imdbid.ToNullIfEmpty(), tmdbid.ToNullableInt32(), null);
                    }
                }
                else if (type == "episode")
                {
                    if (GUIUtils.ShowYesNoDialog(Translation.WatchList, Translation.AddEpisodeToWatchList, true))
                    {
                        TraktLogger.Info("Adding episode to Watchlist. Title = '{0}', Year = '{1}', Season = '{2}', Episode = '{3}', Episode TVDb ID = '{4}'", title, year, season, episode, epTvdbId);
                        TraktHelper.AddEpisodeToWatchList(null, season.ToInt(), episode.ToInt(), epTvdbId.ToNullableInt32(), null, null, null);
                    }
                }
            }
            #endregion

            #region Add To Custom List
            if (validCustomListItem)
            {
                if (type == "movie")
                {
                    TraktLogger.Info("Adding movie to Custom List. Title = '{0}', Year = '{1}', IMDb ID = '{2}'", title, year, imdbid);
                    TraktHelper.AddRemoveMovieInUserList(title, year, imdbid, false);
                }
                else if (type == "show")
                {
                    TraktLogger.Info("Adding show to Custom List. Title = '{0}', Year = '{1}', TVDb ID = '{2}'", title, year, showtvdbid);
                    TraktHelper.AddRemoveShowInUserList(title, year, showtvdbid, false);
                }
                else if (type == "episode")
                {
                    TraktLogger.Info("Adding episode to Custom List. Title = '{0}', Year = '{1}', Season = '{2}', Episode = '{3}', Episode TVDb ID = '{4}'", title, year, season, episode, epTvdbId);
                    TraktHelper.AddRemoveEpisodeInUserList(new TraktEpisode
                    {
                        Ids = new TraktEpisodeId
                        {
                            Tvdb = epTvdbId.ToNullableInt32()
                        },
                        Number = episode.ToInt(),
                        Season = season.ToInt()
                    }, false);
                }
            }
            #endregion

            #region Rate
            if (validRateItem)
            {
                if (!GUICommon.CheckLogin(false)) return;

                switch (type)
                {
                    case "movie":
                        TraktLogger.Info("Showing rate dialog for movie. Title = '{0}', Year = '{1}', IMDb ID = '{2}'", title, year, imdbid);
                        GUIUtils.ShowRateDialog<TraktSyncMovieRated>(new TraktSyncMovieRated
                        {
                            Ids = new TraktMovieId { Imdb = imdbid.ToNullIfEmpty(), Tmdb = tmdbid.ToNullableInt32() },
                            Title = title,
                            Year = year.ToNullableInt32()
                        });
                        break;

                    case "series":
                        TraktLogger.Info("Showing rate dialog for tv show. Title = '{0}', Year = '{1}', TVDb ID = '{2}'", title, year, showtvdbid);
                        GUIUtils.ShowRateDialog<TraktSyncShowRated>(new TraktSyncShowRated
                        {
                            Ids = new TraktShowId { Tvdb = showtvdbid.ToNullableInt32(), Imdb = imdbid.ToNullIfEmpty() },
                            Title = title,
                            Year = year.ToNullableInt32()
                        });
                        break;

                    case "episode":
                        TraktLogger.Info("Showing rate dialog for tv episode. Title = '{0}', Year = '{1}', Season = '{2}', Episode = '{3}', Episode TVDb ID = '{4}'", title, year, season, episode, epTvdbId);
                        GUIUtils.ShowRateDialog<TraktSyncEpisodeRated>(new TraktSyncEpisodeRated
                        {
                            Ids = new TraktEpisodeId { Tvdb = showtvdbid.ToNullableInt32() },
                            Number = episode.ToInt(),
                            Season = season.ToInt(),
                            RatedAt = DateTime.UtcNow.ToISO8601()
                        });
                        break;
                }
            }
            #endregion

            #region Shouts
            if (validShoutItem)
            {
                if (!GUICommon.CheckLogin(false)) return;

                // Initialize Shout window
                switch (type)
                {
                    #region movie
                    case "movie":
                        TraktLogger.Info("Displaying Shouts for {0}. Title = '{0}', Year = '{1}', IMDb ID = '{2}'", title, year, imdbid);
                        TraktHelper.ShowMovieShouts(title, year, imdbid, isWatched, fanart);
                        break;
                    #endregion
                    #region episode
                    case "episode":
                        TraktLogger.Info("Displaying Shouts for {0}. Title = '{0}', Year = '{1}', TVDb ID = '{2}'", title, year, showtvdbid);
                        TraktHelper.ShowEpisodeShouts(title, showtvdbid, season, episode, isWatched, fanart);
                        break;
                    #endregion
                    #region series
                    case "series":
                        TraktLogger.Info("Displaying Shouts for {0}. Title = '{0}', Year = '{1}', TVDb ID = '{2}'", title, year, showtvdbid);
                        TraktHelper.ShowTVShowShouts(title, showtvdbid.ToNullableInt32(), null, isWatched, fanart);
                        break;
                    #endregion
                }
            }
            #endregion

            #region Related Movies/Shows
            if (validRelatedItem)
            {
                // Initialize Shout window
                switch (type)
                {
                    #region movie
                    case "movie":
                        TraktLogger.Info("Displaying Related Movies for {0}. Title = '{0}', Year = '{1}', IMDb ID = '{2}'", title, year, imdbid);
                        TraktHelper.ShowRelatedMovies(title, year, imdbid);
                        break;
                    #endregion
                    #region series
                    case "series":
                        TraktLogger.Info("Displaying Related Shows for {0}. Title = '{0}', Year = '{1}', TVDb ID = '{2}'", title, year, showtvdbid);
                        TraktHelper.ShowRelatedShows(title, showtvdbid);
                        break;
                    #endregion
                }
            }
            #endregion

            #region Trakt Menu
            if (validTraktMenuItem)
            {
                if (!GUICommon.CheckLogin(false)) return;

                switch (type)
                {
                    case "movie":
                        GUICommon.ShowTraktExtMovieMenu(title, year, imdbid, isWatched, fanart, searchPeople, false);
                        break;

                    case "series":
                        GUICommon.ShowTraktExtTVShowMenu(title, year, showtvdbid, imdbid, fanart, searchPeople, false);
                        break;

                    case "episode":
                        GUICommon.ShowTraktExtEpisodeMenu(title, year, season, episode, showtvdbid, isWatched, fanart, searchPeople, false);
                        break;
                }
            }
            #endregion

            #region Search Menu
            if (validSearchItem)
            {
                if (searchPeople.Count == 0)
                {
                    GUIUtils.ShowOKDialog(Translation.SearchBy, Translation.NoPeopleToSearch);
                }
                else
                {
                    GUICommon.ShowSearchByMenu(searchPeople, title, fanart);
                }
            }
            #endregion
        }