Skip to content

Commit

Permalink
Bug fixes - batch color change implemented
Browse files Browse the repository at this point in the history
Batch Shortcut amendment implementation started - can amend the color,
'show foreground text' and 'light/dark' for bulk shortcuts.

Bug fixes:

- Fixed crash if trying to get pinned items before creating custom
shortcuts (ProgramData folder didn't exist)
- Force deletion on items that need replacing to work
- Attribute retention in an action rather than using (not sure on best
practice here)
- ColorPanel defaults to Custom correctly
- Added more null checks in various places
  • Loading branch information
Jonno12345 committed Jan 9, 2017
1 parent d8b7453 commit 540c1ff
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 106 deletions.
11 changes: 11 additions & 0 deletions TileIconifier.Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public void SaveConfig()

public void SaveConfig(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return;
}

var dirPath = Path.GetDirectoryName(filePath);
if (dirPath != null && !Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}

using (var xmlFile = new FileStream(filePath, FileMode.Create))
{
var xmlSerializer = new XmlSerializer(typeof (Config));
Expand Down
10 changes: 5 additions & 5 deletions TileIconifier.Core/IconExtractor/IconExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public class IconExtractor

// Resource types for EnumResourceNames().

private static readonly IntPtr RtIcon = (IntPtr) 3;
private static readonly IntPtr RtGroupIcon = (IntPtr) 14;
private static readonly IntPtr _rtIcon = (IntPtr) 3;
private static readonly IntPtr _rtGroupIcon = (IntPtr) 14;

////////////////////////////////////////////////////////////////////////
// Fields
Expand Down Expand Up @@ -137,7 +137,7 @@ private void Initialize(string fileName)
// RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's.
var dir = GetDataFromResource(hModule, RtGroupIcon, name);
var dir = GetDataFromResource(hModule, _rtGroupIcon, name);
// Calculate the size of an entire .icon file.
Expand All @@ -159,7 +159,7 @@ private void Initialize(string fileName)
// Load the picture.
var id = BitConverter.ToUInt16(dir, 6 + 14*i + 12); // GRPICONDIRENTRY.nID
var pic = GetDataFromResource(hModule, RtIcon, (IntPtr) id);
var pic = GetDataFromResource(hModule, _rtIcon, (IntPtr) id);
// Copy GRPICONDIRENTRY to ICONDIRENTRY.
Expand All @@ -182,7 +182,7 @@ private void Initialize(string fileName)
return true;
};
NativeMethods.EnumResourceNames(hModule, RtGroupIcon, callback, IntPtr.Zero);
NativeMethods.EnumResourceNames(hModule, _rtGroupIcon, callback, IntPtr.Zero);

_iconData = tmpData.ToArray();
}
Expand Down
6 changes: 3 additions & 3 deletions TileIconifier.Core/IconExtractor/IconUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace TileIconifier.Core.IconExtractor
{
public static class IconUtil
{
private static readonly GetIconDataDelegate IconDataDelegate;
private static readonly GetIconDataDelegate _iconDataDelegate;

static IconUtil()
{
Expand All @@ -51,7 +51,7 @@ static IconUtil()
if (fi != null) gen.Emit(OpCodes.Ldfld, fi);
gen.Emit(OpCodes.Ret);

IconDataDelegate = (GetIconDataDelegate) dm.CreateDelegate(typeof (GetIconDataDelegate));
_iconDataDelegate = (GetIconDataDelegate) dm.CreateDelegate(typeof (GetIconDataDelegate));
}

/// <summary>
Expand Down Expand Up @@ -180,7 +180,7 @@ public static int GetBitCount(Icon icon)

private static byte[] GetIconData(Icon icon)
{
var data = IconDataDelegate(icon);
var data = _iconDataDelegate(icon);
if (data != null)
{
return data;
Expand Down
6 changes: 3 additions & 3 deletions TileIconifier.Core/Shortcut/State/ShortcutItemState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public bool Equals(ShortcutItemState other)
if (ReferenceEquals(this, other))
return true;

return BackgroundColor == other.BackgroundColor
&& ForegroundText == other.ForegroundText
&& ShowNameOnSquare150X150Logo == other.ShowNameOnSquare150X150Logo
return BackgroundColor == other?.BackgroundColor
&& ForegroundText == other?.ForegroundText
&& ShowNameOnSquare150X150Logo == other?.ShowNameOnSquare150X150Logo
&& MediumImage.Equals(other.MediumImage)
&& SmallImage.Equals(other.SmallImage)
&& UseSystemAccentColor == other.UseSystemAccentColor;
Expand Down
12 changes: 7 additions & 5 deletions TileIconifier.Core/Shortcut/State/ShortcutItemStateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public class ShortcutItemStateController

public void SaveMediumIconMetadata(string filePath)
{
IoUtils.ForceDelete(filePath);
CurrentState.MediumImage.Save(filePath);
}

public void SaveSmallIconMetadata(string filePath)
{
IoUtils.ForceDelete(filePath);
CurrentState.SmallImage.Save(filePath);
}

Expand Down Expand Up @@ -99,15 +101,15 @@ internal void LoadParameters(bool loadFromFile, string visualElementsManifestPat
var parameters = from b in xmlDoc.Descendants("VisualElements")
select new ShortcutItemState
{
BackgroundColor = b.Attribute("BackgroundColor").Value,
ForegroundText = b.Attribute("ForegroundText").Value,
ShowNameOnSquare150X150Logo = b.Attribute("ShowNameOnSquare150x150Logo").Value == "on",
BackgroundColor = b.Attribute("BackgroundColor")?.Value,
ForegroundText = b.Attribute("ForegroundText")?.Value,
ShowNameOnSquare150X150Logo = b.Attribute("ShowNameOnSquare150x150Logo")?.Value == "on",
MediumImage =
mediumImage ?? new ShortcutItemImage(ShortcutConstantsAndEnums.MediumShortcutOutputSize)
{
Bytes =
ImageUtils.LoadFileToByteArray(targetFolderPath +
b.Attribute("Square150x150Logo").Value),
b.Attribute("Square150x150Logo")?.Value),
X = 0,
Y = 0
},
Expand All @@ -116,7 +118,7 @@ internal void LoadParameters(bool loadFromFile, string visualElementsManifestPat
{
Bytes =
ImageUtils.LoadFileToByteArray(targetFolderPath +
b.Attribute("Square70x70Logo").Value),
b.Attribute("Square70x70Logo")?.Value),
X = 0,
Y = 0
}
Expand Down
18 changes: 6 additions & 12 deletions TileIconifier.Core/TileIconify/TileIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void RunIconify()
BuildFilesAndFolders();
SaveMetadata();
SaveIcons();
_shortcutItem.Properties.CommitChanges();
RebuildLnkInStartMenu();
}

Expand Down Expand Up @@ -82,10 +83,9 @@ private void BuildFilesAndFolders()
Directory.CreateDirectory(_shortcutItem.VisualElementsPath);
}

using (new AttributeRetention(_shortcutItem.VisualElementManifestPath))
{
xDoc.Save(_shortcutItem.VisualElementManifestPath);
}
IoUtils.FileActionRetainingAttributes(_shortcutItem.VisualElementManifestPath,
() => xDoc.Save(_shortcutItem.VisualElementManifestPath));

}

private void DeleteFilesAndFolders()
Expand All @@ -106,14 +106,8 @@ private void DeleteFilesAndFolders()

private void SaveMetadata()
{
using (new AttributeRetention(_shortcutItem.MediumImageResizeMetadataPath))
{
_shortcutItem.Properties.SaveMediumIconMetadata(_shortcutItem.MediumImageResizeMetadataPath);
}
using (new AttributeRetention(_shortcutItem.SmallImageResizeMetadataPath))
{
_shortcutItem.Properties.SaveSmallIconMetadata(_shortcutItem.SmallImageResizeMetadataPath);
}
_shortcutItem.Properties.SaveMediumIconMetadata(_shortcutItem.MediumImageResizeMetadataPath);
_shortcutItem.Properties.SaveSmallIconMetadata(_shortcutItem.SmallImageResizeMetadataPath);
}

private void SaveIcons()
Expand Down
32 changes: 11 additions & 21 deletions TileIconifier.Core/Utilities/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,24 @@ public static void ForceDelete(string filePath)
File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);
}
}

/// <summary>
/// Reverts attributes to normal and restores on disposal - prevents Access Denied on readonly/hidden files
/// </summary>
internal class AttributeRetention : IDisposable
{
private readonly string _filePath;
private readonly FileAttributes _storedFileAttributes;

public AttributeRetention(string filePath)
public static void FileActionRetainingAttributes(string filePath, Action action, bool fileMustExist = false)
{
_filePath = filePath;
if (!File.Exists(_filePath))
if (!File.Exists(filePath))
{
if (fileMustExist)
{
throw new FileNotFoundException();
}
return;
}

_storedFileAttributes = File.GetAttributes(_filePath);
File.SetAttributes(_filePath, FileAttributes.Normal);
}
var storedFileAttributes = File.GetAttributes(filePath);
File.SetAttributes(filePath, FileAttributes.Normal);

public void Dispose()
{
if (File.Exists(_filePath))
{
File.SetAttributes(_filePath, _storedFileAttributes);
}
action();

File.SetAttributes(filePath, storedFileAttributes);
}
}
}
1 change: 1 addition & 0 deletions TileIconifier/Controls/IconifierPanel/ColorPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ private void ColorPanel_Load(object sender, EventArgs e)
{
cmbColour.Items.AddRange(_dropDownColors.Select(c => c.Name.ToLower()).ToArray<object>());
cmbColour.Items.Add("Custom");
cmbColour.SelectedItem = "Custom";
}
}

Expand Down
7 changes: 6 additions & 1 deletion TileIconifier/Controls/IconifierPanel/TileIconifierPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public TileIconifierPanel()

public void UpdateControlsToShortcut()
{
if (CurrentShortcutItem == null)
{
return;
}

//disable event handlers whilst updating things programatically
RemoveEventHandlers();

Expand Down Expand Up @@ -335,7 +340,7 @@ private void ColorPanelColorUpdate(object sender, EventArgs eventArgs)
private void UpdateFromColorPanel(ColorPanel usedColorPanel)
{
var result = usedColorPanel.GetColorPanelResult();
if (result == null)
if (result == null || CurrentShortcutItem == null)
{
return;
}
Expand Down
1 change: 0 additions & 1 deletion TileIconifier/Forms/Main/FrmAbout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#endregion

using System;
using System.Diagnostics;
using System.Windows.Forms;
using TileIconifier.Core.Utilities;

Expand Down
Loading

0 comments on commit 540c1ff

Please sign in to comment.