aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.FileSystem/FileItem.cs
blob: cbc90ce06cefbaf63c15cc279268ae7386e727df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tango.Core.IO;

namespace Tango.FileSystem
{
    public class FileItem : FileSystemItem
    {
        private static Dictionary<String, BitmapSource> iconCache;
        private static Dictionary<String, BitmapSource> smallIconCache;
        private static Dictionary<String, String> typeDescriptionCache;

        static FileItem()
        {
            iconCache = new Dictionary<string, BitmapSource>();
            smallIconCache = new Dictionary<string, BitmapSource>();
            typeDescriptionCache = new Dictionary<string, string>();
        }

        public FileItem()
        {
            Type = FileSystemItemType.File;
        }

        public String Extension
        {
            get { return System.IO.Path.GetExtension(Path); }
        }

        private BitmapSource _icon;
        public BitmapSource Icon
        {
            get
            {
                if (_icon == null)
                {
                    _icon = GetFileIcon();
                }

                return _icon;
            }
        }

        private BitmapSource _smallIcon;
        public BitmapSource SmallIcon
        {
            get
            {
                if (_smallIcon == null)
                {
                    _smallIcon = GetSmallFileIcon();
                }

                return _smallIcon;
            }
        }

        public override string Description
        {
            get
            {
                if (typeDescriptionCache.ContainsKey(Extension))
                {
                    return typeDescriptionCache[Extension];
                }
                else
                {
                    var tempFile = TemporaryManager.Default.CreateFile(Extension);
                    var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellFile.FromFilePath(tempFile);
                    var text = shellFile.Properties.System.ItemTypeText.Value.ToStringSafe();
                    shellFile.Dispose();
                    tempFile.Delete();
                    typeDescriptionCache.Add(Extension, text);
                    return text;
                }
            }
        }

        protected BitmapSource GetFileIcon()
        {
            if (iconCache.ContainsKey(Extension))
            {
                return iconCache[Extension];
            }
            else
            {
                var tempFile = TemporaryManager.Default.CreateFile(Extension);
                var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellFile.FromFilePath(tempFile);
                var source = shellFile.Thumbnail.MediumBitmapSource;
                shellFile.Dispose();
                tempFile.Delete();
                iconCache.Add(Extension, source);
                return source;
            }
        }

        private BitmapSource GetSmallFileIcon()
        {
            if (smallIconCache.ContainsKey(Extension))
            {
                return smallIconCache[Extension];
            }
            else
            {
                var tempFile = TemporaryManager.Default.CreateFile(Extension);
                var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellFile.FromFilePath(tempFile);
                var source = shellFile.Thumbnail.SmallBitmapSource;
                shellFile.Dispose();
                tempFile.Delete();
                smallIconCache.Add(Extension, source);
                return source;
            }
        }

        private BitmapSource IconToBitmapSource(Icon icon)
        {
            var imageSource = Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            return imageSource;
        }
    }
}