Statistics
| Branch: | Revision:

root / trunk / Pithos.ShellExtensions / Overlays / IconOverlayBase.cs @ 7e26c075

History | View | Annotate | Download (5.5 kB)

1
using System;
2
using System.Diagnostics;
3
using System.Diagnostics.Contracts;
4
using System.IO;
5

    
6
using System.ComponentModel.Composition;
7
using System.Linq;
8
using Pithos.Interfaces;
9

    
10
namespace Pithos.ShellExtensions.Overlays
11
{
12

    
13
    public class IconOverlayBase : IShellIconOverlayIdentifier
14
    {
15
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos.IconOverlay");
16

    
17
        protected static string PithosPrefix = "0Pithos";
18

    
19
        public string OverlayName { get; private set; }
20
        public string IconPath { get; private set; }
21

    
22
        [Import(typeof (IStatusChecker))] 
23
        public IStatusChecker StatusChecker;
24
        [Import(typeof (IPithosSettings))] 
25
        public IPithosSettings Settings;
26

    
27

    
28

    
29
        public IconOverlayBase(string iconName)
30
        {
31
            if (String.IsNullOrWhiteSpace(iconName))
32
                throw new ArgumentNullException("iconName","Empty iconName");
33

    
34
            Debug.WriteLine("Icon Overlay Instance created", LogCategories.ShellOverlays);
35
            IoC.Current.Compose(this);
36
            
37

    
38
            string overlayName=PithosPrefix + iconName;
39
            string iconFile = iconName + "Icon.ico";
40

    
41

    
42
            var iconsFolder = Settings.IconsPath;
43
            var fullPath = Path.Combine(iconsFolder, iconFile);
44

    
45
            OverlayName = overlayName;
46
            IconPath = fullPath;
47

    
48
            
49
        }
50

    
51

    
52
        public int IsMemberOf(string path, uint attributes)
53
        {
54
            if (String.IsNullOrWhiteSpace(path))
55
                throw new ArgumentNullException("path","Empty path");
56

    
57
            Debug.WriteLine(String.Format("ICON Status check for {0} - {1}", path, GetType().Name), LogCategories.ShellOverlays);
58
            
59
            if (!Settings.Accounts.Any(account=>path.StartsWith(account.RootPath,StringComparison.InvariantCultureIgnoreCase)))
60
                return WinError.S_FALSE;
61

    
62
            var status=StatusChecker.GetFileOverlayStatus(path);
63
            var isMember = (PithosPrefix + status == OverlayName);
64
            Debug.WriteLine(String.Format("[STATUS] Was {0}, expected {1} for {2}", status, OverlayName,path ), LogCategories.ShellOverlays);
65
            return isMember ? WinError.S_OK : WinError.S_FALSE;
66
        }
67

    
68
        public int GetOverlayInfo(
69
            IntPtr iconFileBuffer,
70
            int iconFileBufferSize,
71
            out int iconIndex,
72
            out uint flags)
73
        {
74
            Contract.Requires(iconFileBuffer!=IntPtr.Zero);
75
            Contract.Requires(iconFileBufferSize>0);
76

    
77
            if (iconFileBuffer == IntPtr.Zero)
78
                throw new ArgumentNullException("iconFileBuffer","iconFileBuffer not initialized");
79
            if (iconFileBufferSize<=0)
80
                throw new ArgumentException("iconFileBufferSize", "iconFileBufferSize must be greatere than 0");
81
            Debug.WriteLine("Looking for icons", LogCategories.ShellOverlays);
82
            Debug.WriteLine(string.Format("ICON file {0}", IconPath), LogCategories.ShellOverlays);
83
            
84
            int bytesCount = System.Text.Encoding.Unicode.GetByteCount(IconPath);
85
            Debug.WriteLine(string.Format(" GetOverlayInfo::{0}", bytesCount), LogCategories.ShellOverlays);
86

    
87
            MarshalHelpers.CopyToBuffer(IconPath, iconFileBuffer, iconFileBufferSize);
88

    
89
            flags = (uint)(ISIOI.ISIOI_ICONFILE);// | ISIOI.ISIOI_ICONINDEX);
90
            iconIndex = 0;
91
            return WinError.S_OK;
92
        }
93

    
94
        public int  GetPriority(out int priority)
95
        {
96
            Debug.WriteLine("Checking for priority");
97
            priority = 0;
98
            return WinError.S_OK;
99
        }
100

    
101
        
102
        public static void RegisterOverlay(Type type,string iconName)
103
        {
104
            Contract.Requires(type != null);
105
            Contract.Requires(!String.IsNullOrWhiteSpace(iconName));
106

    
107
            if (type == null)
108
                throw new ArgumentNullException("type", "type can't be null");
109
            if (String.IsNullOrWhiteSpace(iconName))
110
                throw new ArgumentNullException("iconName", "iconName can't be null");
111

    
112
            try
113
            {
114
                
115
                ShellExtReg.RegisterIconOverlayIdentifier(type.GUID, iconName);
116

    
117
                NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST,
118
                                             IntPtr.Zero, IntPtr.Zero);
119
                Debug.WriteLine("Registered icon handler");
120
            }
121
            catch (Exception ex)
122
            {
123
                Debug.WriteLine(ex.Message); // Log the error
124
                throw;  // Re-throw the exception
125
            }
126
        }
127

    
128
        
129
        public static void UnregisterOverlay(Type type,string iconName)
130
        {
131
            Contract.Requires(type!=null);
132
            Contract.Requires(!String.IsNullOrWhiteSpace(iconName));
133

    
134
            if (type==null)
135
                throw new ArgumentNullException("type","type can't be null");
136
            if (String.IsNullOrWhiteSpace(iconName))
137
                throw new ArgumentNullException("iconName","iconName can't be null");
138
            try
139
            {                
140
                ShellExtReg.UnregisterIconOverlayIdentifier(type.GUID, iconName);
141
                Debug.WriteLine(String.Format("UnRegistered icon handler {0}:{1}",iconName,type.GUID), LogCategories.ShellOverlays);
142
            }
143
            catch (Exception ex)
144
            {
145
                //Log and rethrow
146
                Log.ErrorFormat("Failed to unregister overlay {0}:{1} with error {2}",iconName,type.GUID,ex.Message); 
147
                throw;  
148
            }
149
        }
150
    }
151

    
152
}