Beispiel #1
0
        /// <summary>
        ///   Assigns all member values of this instance to the respective members of the given instance.
        /// </summary>
        /// <param name="other">
        ///   The target instance to assign to.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="other" /> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   <paramref name="other" /> is not castable to the <see cref="WallpaperTextOverlay" /> type.
        /// </exception>
        public virtual void AssignTo(object other)
        {
            if (other == null)
            {
                throw new ArgumentNullException();
            }
            if (!(other is WallpaperTextOverlay))
            {
                throw new ArgumentException();
            }

            WallpaperTextOverlay otherInstance = (WallpaperTextOverlay)other;

            otherInstance.Format      = this.Format;
            otherInstance.FontName    = this.FontName;
            otherInstance.FontSize    = this.FontSize;
            otherInstance.FontStyle   = this.FontStyle;
            otherInstance.ForeColor   = this.ForeColor;
            otherInstance.BorderColor = this.BorderColor;
            otherInstance.Offset      = this.Offset;
            otherInstance.Position    = this.Position;
        }
Beispiel #2
0
        // TODO: Use XmlSerializer for reading and writing the config.
        // TODO: Extract Read/Write functionality into a new class (and interface perhaps)
        /// <summary>
        ///   Creates a new <see cref="Configuration" /> instance by reading the data from a <see cref="Stream" />.
        /// </summary>
        /// <remarks>
        ///   This method reads Wallpaper Manager configuration files (version 1.0, 1.1 and 1.2) by using
        ///   <see cref="XmlDocument" />.
        /// </remarks>
        /// <param name="sourceStream">
        ///   The source <see cref="Stream" /> to read from.
        /// </param>
        /// <returns>
        ///   A new <see cref="Configuration" /> instance containing the read data.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="sourceStream" /> is <c>null</c>.
        /// </exception>
        /// <exception cref="IOException">
        ///   <paramref name="sourceStream" /> is not readable.
        /// </exception>
        /// <exception cref="XmlException">
        ///   There is a load or parse error in the XML-Data.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///   Error while deserializing. Refer to the object returned by the  <see cref="Exception.InnerException" /> property
        ///   for details.
        /// </exception>
        /// <overloads>
        ///   <summary>
        ///     Creates a new <see cref="Configuration" /> instance by reading the data from a data source.
        ///   </summary>
        ///   <returns>
        ///     A new <see cref="Configuration" /> instance containing the read data.
        ///   </returns>
        /// </overloads>
        /// <seealso cref="Stream">Stream Class</seealso>
        public static Configuration Read(Stream sourceStream)
        {
            if (sourceStream == null)
            {
                throw new ArgumentNullException();
            }
            if (!sourceStream.CanRead)
            {
                throw new IOException();
            }

            XmlDocument document = new XmlDocument();

            document.Load(sourceStream);

            if ((document.DocumentElement == null) || (document.DocumentElement.Name != Configuration.RootNodeName))
            {
                throw new XmlException("The configuration file has an invalid root element.");
            }

            XmlAttribute versionAttribute = document.DocumentElement.Attributes["Version"];
            Version      configVersion;

            if ((versionAttribute == null) || (!Version.TryParse(versionAttribute.InnerText, out configVersion)))
            {
                throw new XmlException("The configuration file has an invalid root element.");
            }

            Configuration configuration = new Configuration();

            #region <General>
            XmlElement generalElement = document.DocumentElement["General"];
            if (generalElement == null)
            {
                throw new XmlException("The configuration file does not contain expected element 'General'.");
            }

            XmlElement element = generalElement["CycleAfterStartup"];
            if (element != null)
            {
                configuration.General.CycleAfterStartup = bool.Parse(element.InnerText);
            }

            element = generalElement["TerminateAfterStartup"];
            if (element != null)
            {
                configuration.General.TerminateAfterStartup = bool.Parse(element.InnerText);
            }

            element = (generalElement["MinimizeAfterStart"] ?? generalElement["MinimizeAfterStartup"]);
            if (element != null)
            {
                configuration.General.MinimizeAfterStartup = bool.Parse(element.InnerText);
            }

            element = generalElement["StartAutoCyclingAfterStartup"];
            if (element != null)
            {
                configuration.General.StartAutocyclingAfterStartup = bool.Parse(element.InnerText);
            }

            element = generalElement["WallpaperChangeType"];
            if (element != null)
            {
                configuration.General.WallpaperChangeType = (WallpaperChangeType)Enum.Parse(typeof(WallpaperChangeType), element.InnerText);
            }

            element = generalElement["AutoCycleInterval"];
            if (element != null)
            {
                configuration.General.AutocycleInterval = TimeSpan.Parse(element.InnerText, CultureInfo.InvariantCulture);
            }

            element = generalElement["LastActiveListSize"];
            if (element != null)
            {
                configuration.General.LastActiveListSize = byte.Parse(element.InnerText, CultureInfo.InvariantCulture);
            }

            element = generalElement["CycleAfterDisplaySettingsChanged"];
            if (element != null)
            {
                configuration.General.CycleAfterDisplaySettingsChanged = bool.Parse(element.InnerText);
            }

            element = generalElement["MinimizeOnClose"];
            if (element != null)
            {
                configuration.General.MinimizeOnClose = bool.Parse(element.InnerText);
            }

            element = generalElement["DisplayCycleTimeAsIconOverlay"];
            if (element != null)
            {
                configuration.General.DisplayCycleTimeAsIconOverlay = bool.Parse(element.InnerText);
            }

            element = generalElement["WallpaperDoubleClickAction"];
            if (element != null)
            {
                configuration.General.WallpaperDoubleClickAction = (WallpaperClickAction)Enum.Parse(typeof(WallpaperClickAction), element.InnerText);
            }

            element = generalElement["TrayIconSingleClickAction"];
            if (element != null)
            {
                configuration.General.TrayIconSingleClickAction = (TrayIconClickAction)Enum.Parse(typeof(TrayIconClickAction), element.InnerText);
            }

            element = generalElement["TrayIconDoubleClickAction"];
            if (element != null)
            {
                configuration.General.TrayIconDoubleClickAction = (TrayIconClickAction)Enum.Parse(typeof(TrayIconClickAction), element.InnerText);
            }
            #endregion

            #region <ScreensSettings>
            XmlElement screensSettingsElement = document.DocumentElement["ScreensSettings"];
            if (screensSettingsElement != null)
            {
                int settingsCounter = 0;
                var screensSettings = new List <ScreenSettings>();

                foreach (XmlElement screenSettingsElement in screensSettingsElement)
                {
                    if (screenSettingsElement.Name != "ScreenSettings")
                    {
                        continue;
                    }

                    // Make sure there aren't too many screen settings in the configuration.
                    if (settingsCounter >= Screen.AllScreens.Length)
                    {
                        break;
                    }

                    ScreenSettings screenSettings = new ScreenSettings(settingsCounter);
                    element = screenSettingsElement["CycleRandomly"];
                    if (element != null)
                    {
                        screenSettings.CycleRandomly = bool.Parse(element.InnerText);
                    }

                    element = screenSettingsElement["MarginLeft"];
                    if (element != null)
                    {
                        screenSettings.Margins.Left = int.Parse(element.InnerText, CultureInfo.InvariantCulture);
                    }

                    element = screenSettingsElement["MarginRight"];
                    if (element != null)
                    {
                        screenSettings.Margins.Right = int.Parse(element.InnerText, CultureInfo.InvariantCulture);
                    }

                    element = screenSettingsElement["MarginTop"];
                    if (element != null)
                    {
                        screenSettings.Margins.Top = int.Parse(element.InnerText, CultureInfo.InvariantCulture);
                    }

                    element = screenSettingsElement["MarginBottom"];
                    if (element != null)
                    {
                        screenSettings.Margins.Bottom = int.Parse(element.InnerText, CultureInfo.InvariantCulture);
                    }

                    #region <OverlayTexts>
                    XmlElement overlayTextsElement = screenSettingsElement["OverlayTexts"];
                    if (overlayTextsElement != null)
                    {
                        foreach (XmlElement overlayTextElement in overlayTextsElement)
                        {
                            if (overlayTextElement.Name != "OverlayText")
                            {
                                continue;
                            }
                            WallpaperTextOverlay textOverlay = new WallpaperTextOverlay();

                            element = overlayTextElement["Format"];
                            if (element != null)
                            {
                                textOverlay.Format = element.InnerText;
                            }

                            element = overlayTextElement["Position"];
                            if (element != null)
                            {
                                textOverlay.Position = (TextOverlayPosition)Enum.Parse(typeof(TextOverlayPosition), element.InnerText);
                            }

                            element = overlayTextElement["FontName"];
                            if (element != null)
                            {
                                textOverlay.FontName = element.InnerText;
                            }

                            element = overlayTextElement["FontSize"];
                            if (element != null)
                            {
                                textOverlay.FontSize = float.Parse(element.InnerText, CultureInfo.InvariantCulture);
                            }

                            element = overlayTextElement["FontStyle"];
                            if (element != null)
                            {
                                textOverlay.FontStyle = (FontStyle)Enum.Parse(typeof(FontStyle), element.InnerText);
                            }

                            element = overlayTextElement["ForeColor"];
                            if (element != null)
                            {
                                textOverlay.ForeColor = ColorTranslator.FromHtml(element.InnerText);
                            }

                            element = overlayTextElement["BorderColor"];
                            if (element != null)
                            {
                                textOverlay.BorderColor = ColorTranslator.FromHtml(element.InnerText);
                            }

                            element = overlayTextElement["HorizontalOffset"];
                            if (element != null)
                            {
                                textOverlay.HorizontalOffset = int.Parse(element.InnerText, CultureInfo.InvariantCulture);
                            }

                            element = overlayTextElement["VerticalOffset"];
                            if (element != null)
                            {
                                textOverlay.VerticalOffset = int.Parse(element.InnerText, CultureInfo.InvariantCulture);
                            }

                            screenSettings.TextOverlays.Add(textOverlay);
                        }
                    }
                    #endregion

                    #region <StaticWallpaper>
                    XmlElement staticWallpaperElement = screenSettingsElement["StaticWallpaper"];
                    if (staticWallpaperElement != null)
                    {
                        screenSettings.StaticWallpaper = (Wallpaper)Configuration.GetWallpaperDataFromXmlElement(staticWallpaperElement, typeof(Wallpaper));
                    }
                    #endregion

                    screensSettings.Add(screenSettings);
                    settingsCounter++;
                }

                configuration.General.ScreensSettings = new ScreenSettingsCollection(screensSettings);
            }
            #endregion

            #region <WallpaperCategories>
            XmlElement wallpaperCategoriesElement = document.DocumentElement["WallpaperCategories"];
            if (wallpaperCategoriesElement != null)
            {
                foreach (XmlElement wallpaperCategoryElement in wallpaperCategoriesElement)
                {
                    if (
                        wallpaperCategoryElement.Name != "Category" &&
                        wallpaperCategoryElement.Name != "SynchronizedFolder" &&
                        wallpaperCategoryElement.Name != "WatchedCategory" /* Version 1.1 name for synchronized folders. */
                        )
                    {
                        continue;
                    }

                    element = wallpaperCategoryElement["Name"];
                    if (element == null)
                    {
                        continue;
                    }
                    string categoryName = element.InnerText;

                    WallpaperDefaultSettings defaultSettings = null;
                    element = wallpaperCategoryElement["WallpaperDefaultSettings"];
                    if (element != null)
                    {
                        defaultSettings = (WallpaperDefaultSettings)Configuration.GetWallpaperDataFromXmlElement(element, typeof(WallpaperDefaultSettings));
                    }

                    #region <Wallpapers>
                    List <Wallpaper> wallpapers;

                    XmlElement wallpapersElement = wallpaperCategoryElement["Wallpapers"];
                    if (wallpapersElement != null)
                    {
                        wallpapers = new List <Wallpaper>(wallpapersElement.ChildNodes.Count);

                        foreach (XmlElement wallpaperElement in wallpapersElement)
                        {
                            if (wallpaperElement.Name != "Wallpaper")
                            {
                                continue;
                            }

                            Wallpaper wallpaper = (Wallpaper)Configuration.GetWallpaperDataFromXmlElement(wallpaperElement, typeof(Wallpaper));
                            wallpapers.Add(wallpaper);
                        }
                    }
                    else
                    {
                        wallpapers = new List <Wallpaper>(0);
                    }
                    #endregion

                    bool isSynchronizedFolder = ((wallpaperCategoryElement.Name == "SynchronizedFolder") || (wallpaperCategoryElement.Name == "WatchedCategory"));
                    WallpaperCategory category;
                    if (isSynchronizedFolder)
                    {
                        element = (wallpaperCategoryElement["SynchronizedFolderPath"] ?? wallpaperCategoryElement["WatchedDirectoryPath"]);
                        if (element == null)
                        {
                            continue;
                        }
                        Path synchronizedDirPath = new Path(element.InnerText);

                        if (!Directory.Exists(synchronizedDirPath))
                        {
                            continue;
                        }

                        category = new SynchronizedWallpaperCategory(categoryName, synchronizedDirPath, wallpapers);
                    }
                    else
                    {
                        category = new WallpaperCategory(categoryName, wallpapers);
                    }

                    category.WallpaperDefaultSettings = defaultSettings;
                    configuration.WallpaperCategories.Add(category);
                }
            }
            #endregion

            return(configuration);
        }