public void TriggerOnFolderStatus(FolderStatus pFolderStatus) { if (this.OnFolderStatus != null) { this.OnFolderStatus(this, new FolderStatusEventArgs(pFolderStatus)); } }
/// <summary> /// Folder icon changer "On Folder Status" event handler /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event arguments</param> private void FolderIconChangerOnFolderStatus(object sender, FolderStatusEventArgs e) { FolderStatus lFolderStatus = e.FolderStatus; string lAction = string.Empty; if (lFolderStatus.IconAlreadyOk) { lAction = FolderIcons.Resources.TheApplication.IconAlready; } else if (lFolderStatus.IconChanged) { lAction = FolderIcons.Resources.TheApplication.IconChanged; } if (lAction.Length > 0) { this.Log.Add(" " + string.Format(lAction, e.FolderStatus.Icon)); } }
/// <summary> /// Trying to find an icon to apply it to a folder /// </summary> /// <param name="pFolderStatus">Folder status</param> protected bool FindIcon(FolderStatus pFolderStatus) { bool lResult = (pFolderStatus.IconPath.Length > 0); if (!lResult) { string lIconKeyword = pFolderStatus.Icon.Trim().ToLower(); if (lIconKeyword.Length > 0) { if (this.Icons.Dictionary.ContainsKey(lIconKeyword)) { Icon lIcon = this.Icons.Dictionary[lIconKeyword]; pFolderStatus.IconPath = this.Icons.IconPath(lIcon); lResult = true; } } } return(lResult); }
/// <summary> /// Changing folder configuration file /// </summary> /// <param name="pBackgroundWorker">Background worker</param> /// <param name="pDirectoryInfo">Directory info</param> /// <param name="pFolderStatus">Folder status</param> protected void ChangeFolderConfigurationFile(DirectoryInfo pDirectoryInfo, FolderStatus pFolderStatus) { if (pFolderStatus.IconPath.Length == 0) { return; } string lIconPropertyValue = Icons.IconConfigurationFilePropertyValue(pFolderStatus.IconPath); string lConfigurationFilePath = Path.Combine(pDirectoryInfo.FullName, FolderIconChanger.ConfigurationFileName); string lConfigurationFileBackupPath = Path.Combine(pDirectoryInfo.FullName, FolderIconChanger.ConfigurationFileBackupName); ConfigurationFileSerialiser lConfigurationFileSerialiser = new ConfigurationFileSerialiser(); ConfigurationFile lConfigurationFile = null; bool lNewConfigurationFile = false; bool lSaveConfigurationFile = false; if (File.Exists(lConfigurationFilePath)) { lConfigurationFile = lConfigurationFileSerialiser.Read(lConfigurationFilePath); } else { lConfigurationFile = new ConfigurationFile(); lNewConfigurationFile = true; } lConfigurationFile.SetSection(FolderIconChanger.ConfigurationFileSecionName); if (lNewConfigurationFile) { lConfigurationFile.SetProperty(FolderIconChanger.ConfigurationFilePropertyName, lIconPropertyValue); lSaveConfigurationFile = true; } else { string lPropertyValue = lConfigurationFile.GetProperty(FolderIconChanger.ConfigurationFilePropertyName); if (lPropertyValue != lIconPropertyValue) { lConfigurationFile.SetProperty(FolderIconChanger.ConfigurationFilePropertyName, lIconPropertyValue); lSaveConfigurationFile = true; } else { pFolderStatus.IconAlreadyOk = true; } } if (lSaveConfigurationFile) { FileAttributes lConfigurationFileAttributes = FileAttributes.Archive | FileAttributes.Hidden | FileAttributes.System; if (!lNewConfigurationFile) { if (File.Exists(lConfigurationFileBackupPath)) { File.Delete(lConfigurationFileBackupPath); } File.Copy(lConfigurationFilePath, lConfigurationFileBackupPath); lConfigurationFileAttributes = File.GetAttributes(lConfigurationFilePath); File.SetAttributes(lConfigurationFilePath, FileAttributes.Normal); } lConfigurationFileSerialiser.Write(lConfigurationFile, lConfigurationFilePath); File.SetAttributes(lConfigurationFilePath, lConfigurationFileAttributes); pFolderStatus.IconChanged = true; } FileAttributes lFolderAttributes = pDirectoryInfo.Attributes; FileAttributes lFolderNewAttributes = lFolderAttributes | FileAttributes.ReadOnly; if (lFolderAttributes != lFolderNewAttributes) { File.SetAttributes(pDirectoryInfo.FullName, lFolderNewAttributes); } }
/// <summary> /// Changing folder icon /// </summary> /// <param name="pBackgroundWorker">Background worker</param> /// <param name="pDirectoryInfo">Directory info</param> /// <param name="pFolderStatus">Folder status</param> protected void ChangeFolderIcon(BackgroundWorker pBackgroundWorker, DirectoryInfo pDirectoryInfo, FolderStatus pFolderStatus) { try { if (this.FindIcon(pFolderStatus)) { this.ChangeFolderConfigurationFile(pDirectoryInfo, pFolderStatus); } pBackgroundWorker.ReportProgress(0, new FolderIconChangerState(FolderIconChangerStateContentType.FolderStatus, pFolderStatus)); } catch (Exception lException) { FolderIconsException lFolderIconsException = new FolderIconsException(lException); pBackgroundWorker.ReportProgress(0, new FolderIconChangerState(FolderIconChangerStateContentType.Error, lFolderIconsException)); } }
/// <summary> /// Processing a folder /// </summary> /// <param name="pBackgroundWorker">Background worker</param> /// <param name="pDrive">Drive</param> /// <param name="pDirectoryInfo">The directory info</param> /// <param name="pParentFolderStatus">Parent folder status</param> /// <param name="pDepth">Depth</param> /// <param name="pSubFolderDepth">Sub-folder depth</param> protected void ProcessFolder(BackgroundWorker pBackgroundWorker, Drive pDrive, DirectoryInfo pDirectoryInfo, FolderStatus pParentFolderStatus, int pDepth, int pSubFolderDepth) { if (!FolderIconChanger.FolderIsToBeProcessed(pDirectoryInfo)) { return; } pBackgroundWorker.ReportProgress(0, new FolderIconChangerState(FolderIconChangerStateContentType.Folder, new Folder() { Name = pDirectoryInfo.FullName })); string lFolderPath = FileSystemToolkit.GetPathWithoutDrive(pDirectoryInfo.FullName); string lFolderPathIdentifier = FileSystemToolkit.GetPathIdentifier(lFolderPath); Folder lFolder = pDrive.Folders.FirstOrDefault <Folder>(pFolder => FileSystemToolkit.GetPathIdentifier(pFolder.Name) == lFolderPathIdentifier); FolderStatus lFolderStatus = new FolderStatus(pDirectoryInfo.FullName); if (pDepth == 0) { lFolderStatus.SubFolderProcessing = SubFolderProcessing.Icons; lFolderStatus.SubFolderDepth = 1; } if (lFolder != null) { lFolderStatus.Icon = lFolder.Icon; lFolderStatus.SubFolderProcessing = lFolder.SubFolderProcessing; lFolderStatus.SubFolderDepth = lFolder.SubFolderDepth; pSubFolderDepth = 0; } else if (pParentFolderStatus != null) { if (pParentFolderStatus.SubFolderProcessing == SubFolderProcessing.SameIcon) { lFolderStatus.Icon = pParentFolderStatus.Icon; lFolderStatus.IconPath = pParentFolderStatus.IconPath; } lFolderStatus.SubFolderProcessing = pParentFolderStatus.SubFolderProcessing; lFolderStatus.SubFolderDepth = pParentFolderStatus.SubFolderDepth; } if (lFolderStatus.Icon.Length == 0) { lFolderStatus.Icon = pDirectoryInfo.Name.Trim().ToLower().Replace(" ", ""); lFolderStatus.IconPath = string.Empty; } if (pDepth > 0) { this.ChangeFolderIcon(pBackgroundWorker, pDirectoryInfo, lFolderStatus); } string lSubFoldersPattern = lFolderPathIdentifier + @"\"; bool lProcessAllSubFolders = ((lFolderStatus.ProcessSubFolders) && ((lFolderStatus.SubFolderDepth < 0) || (lFolderStatus.SubFolderDepth > pSubFolderDepth))); bool lProcessSomeSubFolders = ((lProcessAllSubFolders) || (pDrive.Folders.Any <Folder>(pFolder => pFolder.Name.Trim().ToLower().StartsWith(lSubFoldersPattern)))); bool lProcessSubFolder = false; string lSubFolderPathIdentifier = string.Empty; if ((lProcessAllSubFolders) || (lProcessSomeSubFolders)) { foreach (DirectoryInfo lSubDirectoryInfo in pDirectoryInfo.GetDirectories()) { lProcessSubFolder = lProcessAllSubFolders; if (!lProcessSubFolder) { lSubFolderPathIdentifier = FileSystemToolkit.GetPathIdentifier(FileSystemToolkit.GetPathWithoutDrive(lSubDirectoryInfo.FullName)); lProcessSubFolder = (pDrive.Folders.FirstOrDefault <Folder>(pFolder => FileSystemToolkit.GetPathIdentifier(pFolder.Name) == lSubFolderPathIdentifier) != null); } if (lProcessSubFolder) { this.ProcessFolder(pBackgroundWorker, pDrive, lSubDirectoryInfo, lFolderStatus, pDepth + 1, pSubFolderDepth + 1); } } } }
/// <summary> /// On execute background worker progress changed /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event arguments</param> private void ExecuteBackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e) { if (e.UserState == null) { return; } if (!(e.UserState is FolderIconChangerState)) { return; } FolderIconChangerState lState = (FolderIconChangerState)e.UserState; bool lWaitForIntefaceUpdate = false; switch (lState.ContentType) { case FolderIconChangerStateContentType.Drive: { Drive lDrive = (Drive)lState.Content; this.TriggerOnDrive(lDrive); this.Counters.CurrentlyProcessing = lDrive.ToString(); this.Counters.DrivesProcessed += 1; this.TriggerOnCountersChange(); lWaitForIntefaceUpdate = true; } break; case FolderIconChangerStateContentType.Folder: { Folder lFolder = (Folder)lState.Content; this.TriggerOnFolder(lFolder); this.Counters.CurrentlyProcessing = lFolder.ToString(); this.Counters.FoldersProcessed += 1; this.TriggerOnCountersChange(); lWaitForIntefaceUpdate = true; } break; case FolderIconChangerStateContentType.FolderStatus: { FolderStatus lFolderStatus = (FolderStatus)lState.Content; this.TriggerOnFolderStatus(lFolderStatus); if (lFolderStatus.IconAlreadyOk) { this.Counters.IconsAlreadyOk += 1; } else if (lFolderStatus.IconChanged) { this.Counters.IconsChanged += 1; } this.TriggerOnCountersChange(); lWaitForIntefaceUpdate = true; } break; case FolderIconChangerStateContentType.Error: { this.TriggerOnError((FolderIconsException)lState.Content); this.Counters.Errors += 1; this.TriggerOnCountersChange(); lWaitForIntefaceUpdate = true; } break; } //^^^if (lWaitForIntefaceUpdate) //^^^ Dispatcher.CurrentDispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null); }
/// <summary> /// Standard constructor /// </summary> /// <param name="pFolderStatus">The folder status</param> public FolderStatusEventArgs(FolderStatus pFolderStatus) : base() { this.FolderStatus = pFolderStatus; }