/// <summary> /// Forces a resynchronization of the containing <see cref="Wallpaper" /> objects with the physical image files on the /// users hard disk. /// </summary> /// <commondoc select='IDisposable/Methods/All/*' /> public void Resynchronize() { if (this.IsDisposed) { throw new ObjectDisposedException("this"); } string[] existingFiles = Directory.GetFiles(this.SynchronizedDirectoryPath); // Add wallpapers that are on the hard disk but not in this category. for (int i = 0; i < existingFiles.Length; i++) { Path existingPath = new Path(existingFiles[i]); if (this.IndexOfByImagePath(existingPath) == -1) { if (SynchronizedWallpaperCategory.IsImageFileExtensionSupported(existingPath)) { this.AddAsync(existingPath); } } } // Remove wallpapers which are in this category but doesn't exist on the hard disk. for (int i = this.Count - 1; i >= 0; i--) { if ((this[i].ImagePath == Path.None) || (!existingFiles.Contains(this[i].ImagePath))) { base.RemoveItem(i); } } }
/// <summary> /// Handles the <see cref="System.IO.FileSystemWatcher.Changed" /> event of a <see cref="System.IO.FileSystemWatcher" /> /// object. /// </summary> /// <commondoc select='All/Methods/EventHandlers[@Params="Object,+EventArgs"]/*' /> private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e) { Action <Path> code = (filePath) => { // Make sure the item doesn't already exist. if (this.IndexOfByImagePath(filePath) == -1 && SynchronizedWallpaperCategory.IsImageFileExtensionSupported(filePath)) { this.AddAsync(filePath); } }; this.InvokeDispatcher.Invoke(DispatcherPriority.Background, code, new Path(e.FullPath)); }
/// <summary> /// Writes the configuration data in the XML-format into the given given file using serialization. /// </summary> /// <remarks> /// This method uses XML-Serialization to write version 2.0 Wallpaper Manager configuration data. /// </remarks> /// <param name="destStream"> /// The destination <see cref="Stream" /> to write into. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="destStream" /> is <c>null</c>. /// </exception> /// <exception cref="IOException"> /// <paramref name="destStream" /> is not writeable. /// </exception> /// <exception cref="InvalidOperationException"> /// Error while serializing. Refer to the object returned by the <see cref="Exception.InnerException" /> property for /// details. /// </exception> /// <seealso cref="Path">Path Class</seealso> public void Write(Stream destStream) { if (destStream == null) { throw new ArgumentNullException(); } if (!destStream.CanWrite) { throw new IOException(); } XmlDocument document = new XmlDocument(); XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "utf-8", null); document.AppendChild(declaration); XmlElement currentElement = document.CreateElement(Configuration.RootNodeName); currentElement.SetAttribute("Version", Configuration.DataVersion); currentElement.SetAttribute("xmlns", "http://www.WallpaperManager.de.vu/Configuration"); document.AppendChild(currentElement); #region <General> XmlElement generalElement = document.CreateElement("General"); document.DocumentElement.AppendChild(generalElement); currentElement = document.CreateElement("CycleAfterStartup"); currentElement.InnerText = this.General.CycleAfterStartup.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("TerminateAfterStartup"); currentElement.InnerText = this.General.TerminateAfterStartup.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("MinimizeAfterStart"); currentElement.InnerText = this.General.MinimizeAfterStartup.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("StartAutoCyclingAfterStartup"); currentElement.InnerText = this.General.StartAutocyclingAfterStartup.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("WallpaperChangeType"); currentElement.InnerText = this.General.WallpaperChangeType.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("AutoCycleInterval"); currentElement.InnerText = this.General.AutocycleInterval.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("LastActiveListSize"); currentElement.InnerText = this.General.LastActiveListSize.ToString(CultureInfo.InvariantCulture); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("CycleAfterDisplaySettingsChanged"); currentElement.InnerText = this.General.CycleAfterDisplaySettingsChanged.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("MinimizeOnClose"); currentElement.InnerText = this.General.MinimizeOnClose.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("DisplayCycleTimeAsIconOverlay"); currentElement.InnerText = this.General.DisplayCycleTimeAsIconOverlay.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("WallpaperDoubleClickAction"); currentElement.InnerText = this.General.WallpaperDoubleClickAction.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("TrayIconSingleClickAction"); currentElement.InnerText = this.General.TrayIconSingleClickAction.ToString(); generalElement.AppendChild(currentElement); currentElement = document.CreateElement("TrayIconDoubleClickAction"); currentElement.InnerText = this.General.TrayIconDoubleClickAction.ToString(); generalElement.AppendChild(currentElement); #endregion #region <ScreensSettings> XmlElement screensSettingsElement = document.CreateElement("ScreensSettings"); document.DocumentElement.AppendChild(screensSettingsElement); foreach (ScreenSettings screenSettings in this.General.ScreensSettings) { XmlElement screenSettingsElement = document.CreateElement("ScreenSettings"); screensSettingsElement.AppendChild(screenSettingsElement); currentElement = document.CreateElement("CycleRandomly"); currentElement.InnerText = screenSettings.CycleRandomly.ToString(); screenSettingsElement.AppendChild(currentElement); currentElement = document.CreateElement("MarginLeft"); currentElement.InnerText = screenSettings.Margins.Left.ToString(CultureInfo.InvariantCulture); screenSettingsElement.AppendChild(currentElement); currentElement = document.CreateElement("MarginRight"); currentElement.InnerText = screenSettings.Margins.Right.ToString(CultureInfo.InvariantCulture); screenSettingsElement.AppendChild(currentElement); currentElement = document.CreateElement("MarginTop"); currentElement.InnerText = screenSettings.Margins.Top.ToString(CultureInfo.InvariantCulture); screenSettingsElement.AppendChild(currentElement); currentElement = document.CreateElement("MarginBottom"); currentElement.InnerText = screenSettings.Margins.Bottom.ToString(CultureInfo.InvariantCulture); screenSettingsElement.AppendChild(currentElement); #region <OverlayTexts> XmlElement overlayTextsElement = document.CreateElement("OverlayTexts"); screenSettingsElement.AppendChild(overlayTextsElement); foreach (WallpaperTextOverlay overlayText in screenSettings.TextOverlays) { XmlElement overlayTextElement = document.CreateElement("OverlayText"); overlayTextsElement.AppendChild(overlayTextElement); currentElement = document.CreateElement("Format"); currentElement.InnerText = overlayText.Format; overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("Position"); currentElement.InnerText = overlayText.Position.ToString(); overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("FontName"); currentElement.InnerText = overlayText.FontName; overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("FontSize"); currentElement.InnerText = overlayText.FontSize.ToString(CultureInfo.InvariantCulture); overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("FontStyle"); currentElement.InnerText = overlayText.FontStyle.ToString(); overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("ForeColor"); currentElement.InnerText = ColorTranslator.ToHtml(overlayText.ForeColor); overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("BorderColor"); currentElement.InnerText = ColorTranslator.ToHtml(overlayText.BorderColor); overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("HorizontalOffset"); currentElement.InnerText = overlayText.HorizontalOffset.ToString(CultureInfo.InvariantCulture); overlayTextElement.AppendChild(currentElement); currentElement = document.CreateElement("VerticalOffset"); currentElement.InnerText = overlayText.VerticalOffset.ToString(CultureInfo.InvariantCulture); overlayTextElement.AppendChild(currentElement); } #endregion currentElement = document.CreateElement("StaticWallpaper"); Configuration.AddWallpaperDataToXmlElement(document, currentElement, screenSettings.StaticWallpaper); screenSettingsElement.AppendChild(currentElement); } #endregion #region <WallpaperCategories> XmlElement categoriesElement = document.CreateElement("WallpaperCategories"); document.DocumentElement.AppendChild(categoriesElement); foreach (WallpaperCategory wallpaperCategory in this.WallpaperCategories) { SynchronizedWallpaperCategory synchronizedWallpaperCategory = wallpaperCategory as SynchronizedWallpaperCategory; XmlElement categoryElement; if (synchronizedWallpaperCategory != null) { categoryElement = document.CreateElement("SynchronizedFolder"); } else { categoryElement = document.CreateElement("Category"); } categoriesElement.AppendChild(categoryElement); currentElement = document.CreateElement("Name"); currentElement.InnerText = wallpaperCategory.Name; categoryElement.AppendChild(currentElement); if (synchronizedWallpaperCategory != null) { currentElement = document.CreateElement("SynchronizedFolderPath"); currentElement.InnerText = synchronizedWallpaperCategory.SynchronizedDirectoryPath; categoryElement.AppendChild(currentElement); } currentElement = document.CreateElement("WallpaperDefaultSettings"); Configuration.AddWallpaperDataToXmlElement(document, currentElement, wallpaperCategory.WallpaperDefaultSettings); categoryElement.AppendChild(currentElement); XmlElement wallpapersElement = document.CreateElement("Wallpapers"); categoryElement.AppendChild(wallpapersElement); foreach (Wallpaper wallpaper in wallpaperCategory) { currentElement = document.CreateElement("Wallpaper"); Configuration.AddWallpaperDataToXmlElement(document, currentElement, wallpaper); wallpapersElement.AppendChild(currentElement); } } #endregion document.Save(destStream); }
// 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); }