Beispiel #1
0
        /// <summary>
        /// Sets the next media node. Should be used either from a mediaComplete event, or an options reset from
        /// the parent.
        /// </summary>
        private bool SetNextMediaNode()
        {
            int playingSequence = currentSequence;

            // What if there are no media nodes?
            if (options.mediaNodes.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("No media nodes to display", "Region - SetNextMediaNode");
                hasExpired = true;
                return(false);
            }

            if (options.mediaNodes.Count == 1 && currentSequence != -1)
            {
                //dont bother discarding this media, keep all the same details, but still trigger an expired event
                System.Diagnostics.Debug.WriteLine("Media Expired:" + options.ToString() + " . Nothing else to show", "Region - SetNextMediaNode");
                hasExpired = true;

                DurationElapsedEvent();
                return(true);
            }

            // Move the sequence on
            currentSequence++;

            if (currentSequence >= options.mediaNodes.Count)
            {
                currentSequence = 0; //zero it

                hasExpired = true;   //we have expired (want to raise an expired event to the parent)

                System.Diagnostics.Debug.WriteLine("Media Expired:" + options.ToString() + " . Reached the end of the sequence. Starting from the beginning.", "Region - SetNextMediaNode");

                DurationElapsedEvent();

                // We want to continue on to show the next media (unless the duration elapsed event triggers a region change)
                if (layoutExpired)
                {
                    return(true);
                }
            }

            //Zero out the options that are persisted
            options.text             = "";
            options.documentTemplate = "";
            options.copyrightNotice  = "";
            options.scrollSpeed      = 30;
            options.updateInterval   = 6;
            options.Uri        = "";
            options.direction  = "none";
            options.javaScript = "";
            options.Dictionary = new MediaDictionary();

            // Get a media node
            bool validNode   = false;
            int  numAttempts = 0;

            while (!validNode)
            {
                numAttempts++;

                // Get the media node for this sequence
                LayoutRegionMedia medium = options.mediaNodes[currentSequence];



                if (medium.Id != null)
                {
                    options.mediaid = medium.Id;
                }

                // Check isnt blacklisted
                if (blackList.BlackListed(options.mediaid))
                {
                    Debug.WriteLine(String.Format("The File [{0}] has been blacklisted", options.mediaid), "Region - SetNextMediaNode");

                    // Increment and Loop
                    currentSequence++;

                    if (currentSequence >= options.mediaNodes.Count)
                    {
                        currentSequence = 0; //zero it

                        hasExpired = true;   //we have expired (want to raise an expired event to the parent)

                        Debug.WriteLine("Media Expired:" + options.ToString() + " . Reached the end of the sequence. Starting from the beginning.", "Region - SetNextMediaNode");

                        DurationElapsedEvent();

                        // We want to continue on to show the next media (unless the duration elapsed event triggers a region change)
                        if (layoutExpired)
                        {
                            return(true);
                        }
                    }
                }
                else
                {
                    validNode = true;

                    // New version has a different schema - the right way to do it would be to pass the <options> and <raw> nodes to
                    // the relevant media class - however I dont feel like engineering such a change so the alternative is to
                    // parse all the possible media type nodes here.

                    // Type and Duration will always be on the media node
                    options.FileType = medium.Type;

                    //TODO: Check the type of node we have, and make sure it is supported.

                    if (medium.Duration != -1)
                    {
                        options.Duration = medium.Duration;
                    }
                    else
                    {
                        options.Duration = 60;
                        System.Diagnostics.Trace.WriteLine("Duration is Empty, using a default of 60.",
                                                           "Region - SetNextMediaNode");
                    }

                    // We cannot have a 0 duration here... not sure why we would... but
                    if (options.Duration == 0 && options.FileType != "Video")
                    {
                        int emptyLayoutDuration = int.Parse(Settings.Default.emptyLayoutDuration.ToString());
                        options.Duration = (emptyLayoutDuration == 0) ? 10 : emptyLayoutDuration;
                    }

                    // There will be some stuff on option nodes
                    if (medium.Options != null)
                    {
                        options.direction = medium.Options.Direction;
                        // Add this to the options object
                        options.Dictionary.Add("direction", medium.Options.Direction);
                        options.Uri = medium.Options.Uri;
                        options.Dictionary.Add("Uri", medium.Options.Uri);
                        options.copyrightNotice = medium.Options.Copyright;
                        options.Dictionary.Add("copyrightNotice", medium.Options.Copyright);
                        options.scrollSpeed = medium.Options.ScrollSpeed;
                        if (medium.Options != null)
                        {
                            options.Dictionary.Add("ScrollSpeed",
                                                   (medium.Options.ScrollSpeed).ToString());
                        }
                        if (medium.Options.ScrollSpeed == -1)
                        {
                            System.Diagnostics.Trace.WriteLine("Non integer scrollSpeed in XLF",
                                                               "Region - SetNextMediaNode");
                        }
                        options.updateInterval = medium.Options.UpdateInterval;
                        if (medium.Options.UpdateInterval == -1)
                        {
                            System.Diagnostics.Trace.WriteLine("Non integer updateInterval in XLF",
                                                               "Region - SetNextMediaNode");
                        }
                    }
                    // And some stuff on Raw nodes

                    if (medium.Raw != null)
                    {
                        if (!String.IsNullOrEmpty(medium.Raw.Text))
                        {
                            options.text = medium.Raw.Text;
                        }

                        if (!String.IsNullOrEmpty(medium.Raw.Text))
                        {
                            options.documentTemplate = medium.Raw.DocumentTemplate;
                        }
                        if (!String.IsNullOrEmpty(medium.Raw.EmbedHtml))
                        {
                            options.text = medium.Raw.EmbedHtml;
                        }

                        if (!String.IsNullOrEmpty(medium.Raw.EmbedScript))
                        {
                            options.javaScript = medium.Raw.EmbedScript;
                        }
                    }
                    // Is this a file based media node?
                    if (options.FileType == "Video" || options.FileType == "flash" || options.FileType == "Image" || options.FileType == "powerpoint")
                    {
                        // Use the cache manager to determine if the file is valid
                        validNode = _cacheManager.IsValidPath(options.Uri);
                    }
                }

                if (numAttempts > options.mediaNodes.Count)
                {
                    // There are no valid nodes in this region, so just signify that the region is ending, and show nothing.
                    System.Diagnostics.Trace.WriteLine("No Valid media nodes to display", "Region - SetNextMediaNode");

                    hasExpired = true;
                    return(false);
                }
            }

            System.Diagnostics.Debug.WriteLine("New media detected " + options.FileType, "Region - SetNextMediaNode");

            // Remove the old one if we have found a valid node - otherwise keep it
            if ((validNode && playingSequence != -1) && playingSequence != currentSequence)
            {
                System.Diagnostics.Debug.WriteLine("Trying to dispose of the current media", "Region - SetNextMediaNode");
                // Dispose of the current media
                try
                {
                    //       media.AnimateAway(MediaCanvas);
                    ThreadPool.QueueUserWorkItem((r) =>
                    {
                        Thread.Sleep(2500);
                        if (media != null && media.Parent != null)
                        {
                            if (media.OldOne != null)
                            {
                                media.OldOne.AnimateAway(MediaCanvas);
                            }
                        }
                        else
                        {
                            if (media != null && media.OldOne != null)
                            {
                                media.OldOne.AnimateAway(MediaCanvas);
                            }
                            if (media != null)
                            {
                                media.AnimateAway(MediaCanvas);
                            }
                        }
                    });
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("No media to remove");
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }

                try
                {
                    // Here we say that this media is expired
                    _stat.ToDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                    // Record this stat event in the statLog object
                    _statLog.RecordStat(_stat);
                }
                catch
                {
                    System.Diagnostics.Trace.WriteLine("No Stat record when one was expected", LogType.Error.ToString());
                }
            }

            return(true);
        }