2 using System.Diagnostics;
3 using System.Diagnostics.Contracts;
6 using System.ComponentModel.Composition;
7 using Pithos.Interfaces;
9 namespace Pithos.ShellExtensions.Overlays
12 public class IconOverlayBase : IShellIconOverlayIdentifier
14 protected static string PithosPrefix = "0Pithos";
16 public string OverlayName { get; private set; }
17 public string IconPath { get; private set; }
19 [Import(typeof (IStatusChecker))]
20 public IStatusChecker StatusChecker;
21 [Import(typeof (IPithosSettings))]
22 public IPithosSettings Settings;
26 public IconOverlayBase(string iconName)
28 if (String.IsNullOrWhiteSpace(iconName))
29 throw new ArgumentNullException("iconName","Empty iconName");
31 Debug.WriteLine("Icon Overlay Instance created", LogCategories.ShellOverlays);
32 IoC.Current.Compose(this);
35 string overlayName=PithosPrefix + iconName;
36 string iconFile = iconName + "Icon.ico";
39 var iconsFolder = Settings.IconsPath;
40 var fullPath = Path.Combine(iconsFolder, iconFile);
42 OverlayName = overlayName;
49 public int IsMemberOf(string path, uint attributes)
51 if (String.IsNullOrWhiteSpace(path))
52 throw new ArgumentNullException("path","Empty path");
54 Debug.WriteLine(String.Format("ICON Status check for {0} - {1}", path, GetType().Name), LogCategories.ShellOverlays);
56 if (!path.StartsWith(Settings.PithosPath,true,null))
57 return WinError.S_FALSE;
59 var status=StatusChecker.GetFileOverlayStatus(path);
60 var isMember = (PithosPrefix + status == OverlayName);
61 Debug.WriteLine(String.Format("[STATUS] Was {0}, expected {1} for {2}", status, OverlayName,path ), LogCategories.ShellOverlays);
62 return isMember ? WinError.S_OK : WinError.S_FALSE;
65 public int GetOverlayInfo(
66 IntPtr iconFileBuffer,
67 int iconFileBufferSize,
71 Contract.Requires(iconFileBuffer!=IntPtr.Zero);
72 Contract.Requires(iconFileBufferSize>0);
74 if (iconFileBuffer == IntPtr.Zero)
75 throw new ArgumentNullException("iconFileBuffer","iconFileBuffer not initialized");
76 if (iconFileBufferSize<=0)
77 throw new ArgumentException("iconFileBufferSize", "iconFileBufferSize must be greatere than 0");
78 Debug.WriteLine("Looking for icons", LogCategories.ShellOverlays);
79 Debug.WriteLine(string.Format("ICON file {0}", IconPath), LogCategories.ShellOverlays);
81 int bytesCount = System.Text.Encoding.Unicode.GetByteCount(IconPath);
82 Debug.WriteLine(string.Format(" GetOverlayInfo::{0}", bytesCount), LogCategories.ShellOverlays);
84 MarshalHelpers.CopyToBuffer(IconPath, iconFileBuffer, iconFileBufferSize);
86 flags = (uint)(ISIOI.ISIOI_ICONFILE);// | ISIOI.ISIOI_ICONINDEX);
91 public int GetPriority(out int priority)
93 Debug.WriteLine("Checking for priority");
99 public static void RegisterOverlay(Type type,string iconName)
101 Contract.Requires(type != null);
102 Contract.Requires(!String.IsNullOrWhiteSpace(iconName));
105 throw new ArgumentNullException("type", "type can't be null");
106 if (String.IsNullOrWhiteSpace(iconName))
107 throw new ArgumentNullException("iconName", "iconName can't be null");
112 ShellExtReg.RegisterIconOverlayIdentifier(type.GUID, iconName);
114 NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST,
115 IntPtr.Zero, IntPtr.Zero);
116 Debug.WriteLine("Registered icon handler");
120 Debug.WriteLine(ex.Message); // Log the error
121 throw; // Re-throw the exception
126 public static void UnregisterOverlay(Type type,string iconName)
128 Contract.Requires(type!=null);
129 Contract.Requires(!String.IsNullOrWhiteSpace(iconName));
132 throw new ArgumentNullException("type","type can't be null");
133 if (String.IsNullOrWhiteSpace(iconName))
134 throw new ArgumentNullException("iconName","iconName can't be null");
137 ShellExtReg.UnregisterIconOverlayIdentifier(type.GUID, iconName);
138 Debug.WriteLine(String.Format("UnRegistered icon handler {0}:{1}",iconName,type.GUID), LogCategories.ShellOverlays);
143 Trace.TraceError("Failed to unregister overlay {0}:{1} with error {2}",iconName,type.GUID,ex.Message);