All files
[pithos-ms-client] / trunk / Pithos.ShellExtensions / Overlays / IconOverlayBase.cs
1 using System;
2 using System.Diagnostics;
3 using System.Diagnostics.Contracts;
4 using System.IO;
5
6 using System.ComponentModel.Composition;
7 using Pithos.Interfaces;
8
9 namespace Pithos.ShellExtensions.Overlays
10 {
11
12     public class IconOverlayBase : IShellIconOverlayIdentifier
13     {
14         protected static string PithosPrefix = "0Pithos";
15
16         public string OverlayName { get; private set; }
17         public string IconPath { get; private set; }
18
19         [Import(typeof (IStatusChecker))] 
20         public IStatusChecker StatusChecker;
21         [Import(typeof (IPithosSettings))] 
22         public IPithosSettings Settings;
23
24
25
26         public IconOverlayBase(string iconName)
27         {
28             Trace.Write("Icon Overlay Instance created");
29             IoC.Current.Compose(this);
30             
31
32             string overlayName=PithosPrefix + iconName;
33             string iconFile = iconName + "Icon.ico";
34
35
36             var iconsFolder = Settings.IconsPath;
37             var fullPath = Path.Combine(iconsFolder, iconFile);
38
39             OverlayName = overlayName;
40             IconPath = fullPath;
41
42             
43         }
44
45
46         public int IsMemberOf(string path, uint attributes)
47         {
48             if (String.IsNullOrWhiteSpace(path))
49                 throw new ArgumentNullException("Empty path");
50
51             Trace.Write(String.Format("ICON Status check for {0} - {1}",path,GetType().Name));
52             
53             if (!path.StartsWith(Settings.PithosPath,true,null))
54                 return WinError.S_FALSE;
55
56             var status=StatusChecker.GetFileOverlayStatus(path);
57             var isMember = (PithosPrefix + status == OverlayName);
58             Trace.Write(String.Format("Status check for {0} is {1}",path,isMember));
59             return isMember ? WinError.S_OK : WinError.S_FALSE;
60         }
61
62         public int GetOverlayInfo(
63             IntPtr iconFileBuffer,
64             int iconFileBufferSize,
65             out int iconIndex,
66             out uint flags)
67         {
68             Trace.Write("Looking for icons");
69
70             Trace.WriteLine(string.Format("ICON file {0}", IconPath));
71             
72             int bytesCount = System.Text.Encoding.Unicode.GetByteCount(IconPath);
73
74             Trace.WriteLine(string.Format(" GetOverlayInfo::{0}",bytesCount));
75
76             MarshalHelpers.CopyToBuffer(IconPath, iconFileBuffer, iconFileBufferSize);
77
78             flags = (uint)(ISIOI.ISIOI_ICONFILE);// | ISIOI.ISIOI_ICONINDEX);
79             iconIndex = 0;
80             return WinError.S_OK;
81         }
82
83         public int  GetPriority(out int priority)
84         {
85             Trace.Write("Checking for priority");
86             priority = 0;
87             return WinError.S_OK;
88         }
89
90         
91         public static void RegisterOverlay(Type t,string iconName)
92         {
93             try
94             {
95                 string overlayName = PithosPrefix + iconName;
96                 ShellExtReg.RegisterIconOverlayIdentifier(t.GUID, overlayName);
97
98                 NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST,
99                                              IntPtr.Zero, IntPtr.Zero);
100                 Trace.Write("Registered icon handler");
101             }
102             catch (Exception ex)
103             {
104                 Trace.WriteLine(ex.Message); // Log the error
105                 throw;  // Re-throw the exception
106             }
107         }
108
109         
110         public static void UnregisterOverlay(Type t,string iconName)
111         {
112             try
113             {
114                 string overlayName = PithosPrefix + iconName;
115                 ShellExtReg.UnregisterIconOverlayIdentifier(t.GUID, overlayName);
116                 Trace.Write("UnRegistered icon handler");
117             }
118             catch (Exception ex)
119             {
120                 Trace.WriteLine(ex.Message); // Log the error
121                 throw;  // Re-throw the exception
122             }
123         }
124     }
125
126 }