Statistics
| Branch: | Revision:

root / trunk / Pithos.ShellExtensions / Overlays / IconOverlayBase.cs @ ac3444de

History | View | Annotate | Download (6.2 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 Microsoft.Win32;
9
using Pithos.Interfaces;
10

    
11
namespace Pithos.ShellExtensions.Overlays
12
{
13

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

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

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

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

    
28

    
29

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

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

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

    
42

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

    
46
            OverlayName = overlayName;
47
            IconPath = fullPath;
48

    
49
            
50
        }
51

    
52

    
53
        private static bool? _isExplorer = false;
54

    
55

    
56
        private bool IsExplorer
57
        {
58
            get
59
            {
60
                if (!_isExplorer.HasValue)
61
                {
62
                    var moduleName = Process.GetCurrentProcess().MainModule.ModuleName;
63
                    _isExplorer = (moduleName == "explorer.exe");
64
                }
65
                return _isExplorer.Value;
66
            }
67
        }
68

    
69
        public int IsMemberOf(string path, uint attributes)
70
        {
71
            var showOnlyInExplorer=(int)Registry.LocalMachine.GetValue(@"SOFTWARE\GRNet\Pithos\ShowOnlyInExplorer", false);
72
            if (showOnlyInExplorer!=0 && !IsExplorer)
73
                return WinError.S_FALSE;
74

    
75

    
76
            if (String.IsNullOrWhiteSpace(path))
77
                throw new ArgumentNullException("path","Empty path");
78

    
79
            Debug.WriteLine(String.Format("ICON Status check for {0} - {1}", path, GetType().Name), LogCategories.ShellOverlays);
80
            
81
            if (!Settings.Accounts.Any(account=>account.RootPath!=null && path.StartsWith(account.RootPath,StringComparison.InvariantCultureIgnoreCase)))
82
                return WinError.S_FALSE;
83

    
84
            var status=StatusChecker.GetFileOverlayStatus(path);
85
            var isMember = (PithosPrefix + status == OverlayName);
86
            Debug.WriteLine(String.Format("[STATUS] Was {0}, expected {1} for {2}", status, OverlayName,path ), LogCategories.ShellOverlays);
87
            return isMember ? WinError.S_OK : WinError.S_FALSE;
88
        }
89

    
90
        public int GetOverlayInfo(
91
            IntPtr iconFileBuffer,
92
            int iconFileBufferSize,
93
            out int iconIndex,
94
            out uint flags)
95
        {
96
            Contract.Requires(iconFileBuffer!=IntPtr.Zero);
97
            Contract.Requires(iconFileBufferSize>0);
98

    
99
            if (iconFileBuffer == IntPtr.Zero)
100
                throw new ArgumentNullException("iconFileBuffer","iconFileBuffer not initialized");
101
            if (iconFileBufferSize<=0)
102
                throw new ArgumentException("iconFileBufferSize", "iconFileBufferSize must be greatere than 0");
103
            Debug.WriteLine("Looking for icons", LogCategories.ShellOverlays);
104
            Debug.WriteLine(string.Format("ICON file {0}", IconPath), LogCategories.ShellOverlays);
105
            
106
            int bytesCount = System.Text.Encoding.Unicode.GetByteCount(IconPath);
107
            Debug.WriteLine(string.Format(" GetOverlayInfo::{0}", bytesCount), LogCategories.ShellOverlays);
108

    
109
            MarshalHelpers.CopyToBuffer(IconPath, iconFileBuffer, iconFileBufferSize);
110

    
111
            flags = (uint)(ISIOI.ISIOI_ICONFILE);// | ISIOI.ISIOI_ICONINDEX);
112
            iconIndex = 0;
113
            return WinError.S_OK;
114
        }
115

    
116
        public int  GetPriority(out int priority)
117
        {
118
            Debug.WriteLine("Checking for priority");
119
            priority = 0;
120
            return WinError.S_OK;
121
        }
122

    
123
        
124
        public static void RegisterOverlay(Type type,string iconName)
125
        {
126
            Contract.Requires(type != null);
127
            Contract.Requires(!String.IsNullOrWhiteSpace(iconName));
128

    
129
            if (type == null)
130
                throw new ArgumentNullException("type", "type can't be null");
131
            if (String.IsNullOrWhiteSpace(iconName))
132
                throw new ArgumentNullException("iconName", "iconName can't be null");
133

    
134
            try
135
            {
136
                
137
                ShellExtReg.RegisterIconOverlayIdentifier(type.GUID, iconName);
138

    
139
                NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST,
140
                                             IntPtr.Zero, IntPtr.Zero);
141
                Debug.WriteLine("Registered icon handler");
142
            }
143
            catch (Exception ex)
144
            {
145
                Debug.WriteLine(ex.Message); // Log the error
146
                throw;  // Re-throw the exception
147
            }
148
        }
149

    
150
        
151
        public static void UnregisterOverlay(Type type,string iconName)
152
        {
153
            Contract.Requires(type!=null);
154
            Contract.Requires(!String.IsNullOrWhiteSpace(iconName));
155

    
156
            if (type==null)
157
                throw new ArgumentNullException("type","type can't be null");
158
            if (String.IsNullOrWhiteSpace(iconName))
159
                throw new ArgumentNullException("iconName","iconName can't be null");
160
            try
161
            {                
162
                ShellExtReg.UnregisterIconOverlayIdentifier(type.GUID, iconName);
163
                Debug.WriteLine(String.Format("UnRegistered icon handler {0}:{1}",iconName,type.GUID), LogCategories.ShellOverlays);
164
            }
165
            catch (Exception ex)
166
            {
167
                //Log and rethrow
168
                Log.ErrorFormat("Failed to unregister overlay {0}:{1} with error {2}",iconName,type.GUID,ex.Message); 
169
                throw;  
170
            }
171
        }
172
    }
173

    
174
}