Пример #1
0
        /// <summary>
        /// Set required files from the XML document
        /// </summary>
        private void SetRequiredFiles()
        {
            // Itterate through the RF XML and populate the RF collection
            XmlNodeList fileNodes = _requiredFilesXml.SelectNodes("/files/file");

            foreach (XmlNode file in fileNodes)
            {
                RequiredFile rf = new RequiredFile();

                XmlAttributeCollection attributes = file.Attributes;

                rf.FileType    = attributes["type"].Value;
                rf.Downloading = false;
                rf.Complete    = false;
                rf.LastChecked = DateTime.Now;
                rf.ChunkOffset = 0;
                rf.ChunkSize   = 0;

                // Fill in some information that we already know
                if (rf.FileType == "media")
                {
                    rf.Id        = int.Parse(attributes["id"].Value);
                    rf.Path      = attributes["path"].Value;
                    rf.SaveAs    = (attributes["saveAs"] == null || string.IsNullOrEmpty(attributes["saveAs"].Value)) ? rf.Path : attributes["saveAs"].Value;
                    rf.Http      = (attributes["download"].Value == "http");
                    rf.ChunkSize = 512000;
                }
                else if (rf.FileType == "layout")
                {
                    rf.Id   = int.Parse(attributes["id"].Value);
                    rf.Path = attributes["path"].Value;
                    rf.Http = (attributes["download"].Value == "http");

                    if (rf.Http)
                    {
                        rf.SaveAs = attributes["saveAs"].Value;
                    }
                    else
                    {
                        rf.Path   = rf.Path + ".xlf";
                        rf.SaveAs = rf.Path;
                    }

                    rf.ChunkSize = rf.Size;
                }
                else if (rf.FileType == "resource")
                {
                    // Do something special here. Check to see if the resource file already exists otherwise add to RF
                    try
                    {
                        // Set the ID to be some random number
                        rf.Id       = int.Parse(attributes["id"].Value);
                        rf.LayoutId = int.Parse(attributes["layoutid"].Value);
                        rf.RegionId = attributes["regionid"].Value;
                        rf.MediaId  = attributes["mediaid"].Value;
                        rf.Path     = rf.MediaId + ".htm";
                        rf.SaveAs   = rf.Path;

                        // Set the size to something arbitary
                        rf.Size = 10000;

                        // Check to see if this has already been downloaded
                        if (File.Exists(ApplicationSettings.Default.LibraryPath + @"\" + rf.MediaId + ".htm"))
                        {
                            // Has it expired?
                            int updated = 0;

                            try
                            {
                                updated = int.Parse(attributes["updated"].Value);
                            }
                            catch (Exception) {}

                            DateTime updatedDt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                            updatedDt = updatedDt.AddSeconds(updated);

                            if (File.GetLastWriteTimeUtc(ApplicationSettings.Default.LibraryPath + @"\" + rf.MediaId + ".htm") > updatedDt)
                            {
                                rf.Complete = true;
                            }
                        }

                        // Add to the Rf Node
                        RequiredFileList.Add(rf);
                        continue;
                    }
                    catch
                    {
                        // Forget about this resource
                        continue;
                    }
                }
                else
                {
                    continue;
                }

                // This stuff only executes for Layout/Files items
                rf.Md5  = attributes["md5"].Value;
                rf.Size = double.Parse(attributes["size"].Value);

                // Does this file already exist in the RF node? We might receive duplicates.
                bool found = false;

                foreach (RequiredFile existingRf in RequiredFileList)
                {
                    if (existingRf.Id == rf.Id && existingRf.FileType == rf.FileType)
                    {
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    Trace.WriteLine(new LogMessage("RequiredFiles - SetRequiredFiles", "Duplicate file detected, ignoring. FileId = " + rf.Id), LogType.Audit.ToString());
                    continue;
                }

                // Does this file exist?
                if (File.Exists(ApplicationSettings.Default.LibraryPath + @"\" + rf.SaveAs))
                {
                    // Compare MD5 of the file we currently have, to what we should have
                    if (rf.Md5 != _cacheManager.GetMD5(rf.SaveAs))
                    {
                        Trace.WriteLine(new LogMessage("RequiredFiles - SetRequiredFiles", "MD5 different for existing file: " + rf.SaveAs), LogType.Info.ToString());

                        // They are different
                        _cacheManager.Remove(rf.SaveAs);

                        // TODO: Resume the file download under certain conditions. Make sure its not bigger than it should be.
                        // Make sure it is fairly fresh
                        FileInfo info = new FileInfo(ApplicationSettings.Default.LibraryPath + @"\" + rf.SaveAs);

                        if (info.Length < rf.Size && info.LastWriteTime > DateTime.Now.AddDays(-1))
                        {
                            // Continue the file
                            rf.ChunkOffset = (int)info.Length;
                        }
                        else
                        {
                            // Delete the old file as it is wrong
                            try
                            {
                                File.Delete(ApplicationSettings.Default.LibraryPath + @"\" + rf.SaveAs);
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine(new LogMessage("CompareAndCollect", "Unable to delete incorrect file because: " + ex.Message));
                            }
                        }
                    }
                    else
                    {
                        // The MD5 is equal - we already have an up to date version of this file.
                        rf.Complete = true;
                        _cacheManager.Add(rf.SaveAs, rf.Md5);
                    }
                }
                else
                {
                    // File does not exist, therefore remove it from the cache manager (on the off chance that it is in there for some reason)
                    _cacheManager.Remove(rf.SaveAs);
                }

                RequiredFileList.Add(rf);
            }
        }
Пример #2
0
        /// <summary>
        /// Manage Overlays
        /// </summary>
        /// <param name="overlays"></param>
        public void ManageOverlays(Collection <ScheduleItem> overlays)
        {
            try
            {
                // Parse all overlays and compare what we have now to the overlays we have already created (see OverlayRegions)

                // Take the ones we currently have up and remove them if they aren't in the new list
                // We use a for loop so that we are able to remove the region from the collection
                for (int i = 0; i < _overlays.Count; i++)
                {
                    Region region = _overlays[i];
                    bool   found  = false;

                    foreach (ScheduleItem item in overlays)
                    {
                        if (item.scheduleid == region.scheduleId && _cacheManager.GetMD5(item.id + ".xlf") == region.hash)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        Debug.WriteLine("Removing overlay which is no-longer required. Overlay: " + region.scheduleId, "Overlays");
                        region.Clear();
                        region.Dispose();
                        Controls.Remove(region);
                        _overlays.Remove(region);
                    }
                }

                // Take the ones that are in the new list and add them
                foreach (ScheduleItem item in overlays)
                {
                    // Check its not already added.
                    bool found = false;
                    foreach (Region region in _overlays)
                    {
                        if (region.scheduleId == item.scheduleid)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        continue;
                    }

                    // Parse the layout for regions, and create them.
                    string layoutPath = item.layoutFile;

                    // Get this layouts XML
                    XmlDocument layoutXml = new XmlDocument();

                    try
                    {
                        // try to open the layout file
                        using (FileStream fs = File.Open(layoutPath, FileMode.Open, FileAccess.Read, FileShare.Write))
                        {
                            using (XmlReader reader = XmlReader.Create(fs))
                            {
                                layoutXml.Load(reader);

                                reader.Close();
                            }
                            fs.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(new LogMessage("MainForm - _schedule_OverlayChangeEvent", string.Format("Could not find the layout file {0}: {1}", layoutPath, ex.Message)), LogType.Info.ToString());
                        continue;
                    }

                    // Attributes of the main layout node
                    XmlNode layoutNode = layoutXml.SelectSingleNode("/layout");

                    XmlAttributeCollection layoutAttributes = layoutNode.Attributes;

                    // Set the background and size of the form
                    double layoutWidth  = int.Parse(layoutAttributes["width"].Value, CultureInfo.InvariantCulture);
                    double layoutHeight = int.Parse(layoutAttributes["height"].Value, CultureInfo.InvariantCulture);

                    // Scaling factor, will be applied to all regions
                    double scaleFactor = Math.Min(_clientSize.Width / layoutWidth, _clientSize.Height / layoutHeight);

                    // Want to be able to center this shiv - therefore work out which one of these is going to have left overs
                    int backgroundWidth  = (int)(layoutWidth * scaleFactor);
                    int backgroundHeight = (int)(layoutHeight * scaleFactor);

                    double leftOverX;
                    double leftOverY;

                    try
                    {
                        leftOverX = Math.Abs(_clientSize.Width - backgroundWidth);
                        leftOverY = Math.Abs(_clientSize.Height - backgroundHeight);

                        if (leftOverX != 0)
                        {
                            leftOverX = leftOverX / 2;
                        }
                        if (leftOverY != 0)
                        {
                            leftOverY = leftOverY / 2;
                        }
                    }
                    catch
                    {
                        leftOverX = 0;
                        leftOverY = 0;
                    }

                    // New region and region options objects
                    RegionOptions options = new RegionOptions();

                    // Get the regions
                    XmlNodeList listRegions = layoutXml.SelectNodes("/layout/region");

                    foreach (XmlNode region in listRegions)
                    {
                        // Is there any media
                        if (region.ChildNodes.Count == 0)
                        {
                            Debug.WriteLine("A region with no media detected");
                            continue;
                        }

                        //each region
                        XmlAttributeCollection nodeAttibutes = region.Attributes;

                        options.scheduleId  = item.scheduleid;
                        options.layoutId    = item.id;
                        options.regionId    = nodeAttibutes["id"].Value.ToString();
                        options.width       = (int)(Convert.ToDouble(nodeAttibutes["width"].Value, CultureInfo.InvariantCulture) * scaleFactor);
                        options.height      = (int)(Convert.ToDouble(nodeAttibutes["height"].Value, CultureInfo.InvariantCulture) * scaleFactor);
                        options.left        = (int)(Convert.ToDouble(nodeAttibutes["left"].Value, CultureInfo.InvariantCulture) * scaleFactor);
                        options.top         = (int)(Convert.ToDouble(nodeAttibutes["top"].Value, CultureInfo.InvariantCulture) * scaleFactor);
                        options.scaleFactor = scaleFactor;

                        // Store the original width and original height for scaling
                        options.originalWidth  = (int)Convert.ToDouble(nodeAttibutes["width"].Value, CultureInfo.InvariantCulture);
                        options.originalHeight = (int)Convert.ToDouble(nodeAttibutes["height"].Value, CultureInfo.InvariantCulture);

                        // Set the backgrounds (used for Web content offsets)
                        options.backgroundLeft = options.left * -1;
                        options.backgroundTop  = options.top * -1;

                        // Account for scaling
                        options.left = options.left + (int)leftOverX;
                        options.top  = options.top + (int)leftOverY;

                        // All the media nodes for this region / layout combination
                        options.mediaNodes = region.SelectNodes("media");

                        Region temp = new Region(ref _statLog, ref _cacheManager);
                        temp.scheduleId  = item.scheduleid;
                        temp.hash        = _cacheManager.GetMD5(item.id + ".xlf");
                        temp.BorderStyle = _borderStyle;

                        // Dont be fooled, this innocent little statement kicks everything off
                        temp.regionOptions = options;

                        _overlays.Add(temp);
                        Controls.Add(temp);
                        temp.BringToFront();
                    }

                    // Null stuff
                    listRegions = null;
                }

                _clientInfoForm.ControlCount = Controls.Count;
            }
            catch (Exception e)
            {
                Trace.WriteLine(new LogMessage("MainForm - _schedule_OverlayChangeEvent", "Unknown issue managing overlays. Ex = " + e.Message), LogType.Info.ToString());
            }
        }