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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
using KeePass.Native;
using KeePass.Util;
using KeePassLib;
using KeePassLib.Delegates;
using KeePassLib.Utility;
using NativeLib = KeePassLib.Native.NativeLib;
namespace KeePass.UI
{
internal static class FileIcons
{
private const char FiTypePath = 'P';
private const char FiTypeExt = 'E';
private const string FiImageListTag = "FileIcons";
// Images may be used/owned by other components
private static readonly Dictionary<string, Image> g_dCache =
new Dictionary<string, Image>();
private static string GetCacheItemKey(char chType, string strID, Size sz)
{
StringBuilder sb = new StringBuilder();
NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;
sb.Append(chType);
// sb.Append(':');
sb.Append((strID ?? string.Empty).Trim());
sb.Append('_');
sb.Append(sz.Width.ToString(nfi));
sb.Append('x');
sb.Append(sz.Height.ToString(nfi));
return sb.ToString();
}
private static Size GetSizeOrSmall(Size? osz)
{
return (osz ?? UIUtil.GetSmallIconSize());
}
public static Image GetImageForName(string strName, Size? osz)
{
if(strName == null) { Debug.Assert(false); strName = string.Empty; }
string strExt = UrlUtil.GetExtension(strName);
return GetImageForExtension(strExt, osz);
}
public static Image GetImageForExtension(string strExt, Size? osz)
{
strExt = (strExt ?? string.Empty).Trim().ToLowerInvariant();
Size sz = GetSizeOrSmall(osz);
string strKey = GetCacheItemKey(FiTypeExt, strExt, sz);
Image img;
if(g_dCache.TryGetValue(strKey, out img)) return img;
try
{
if(strExt.Length != 0)
{
string strTemp = Program.TempFilesPool.GetTempFileName(strExt);
File.WriteAllBytes(strTemp, MemUtil.EmptyByteArray);
img = GetImageForPath(strTemp, sz, false, false);
Program.TempFilesPool.Delete(strTemp);
}
}
catch(Exception) { Debug.Assert(false); }
if(img == null)
img = GetImageForPath(string.Empty, sz, true, false);
g_dCache[strKey] = img;
return img;
}
public static Image GetImageForPath(string strPath, Size? osz,
bool bAllowCache, bool bNewImageObject)
{
if(strPath == null) { Debug.Assert(false); strPath = string.Empty; }
Size sz = GetSizeOrSmall(osz);
Image img = null;
bool bImgIsNew = false;
string strKey = GetCacheItemKey(FiTypePath, strPath, sz);
if(bAllowCache && g_dCache.TryGetValue(strKey, out img))
{
if(bNewImageObject) img = (img.Clone() as Image);
return img;
}
Debug.Assert(img == null);
if(strPath.Length == 0)
{
img = Properties.Resources.B16x16_Binary;
bImgIsNew = false;
}
try
{
if((img == null) && !NativeLib.IsUnix())
{
string strDisplayName;
NativeMethods.SHGetFileInfo(strPath, sz.Width, sz.Height,
out img, out strDisplayName);
bImgIsNew = true;
}
}
catch(Exception) { Debug.Assert(false); }
if((img == null) && !MonoWorkarounds.IsRequired(100003))
{
img = UIUtil.GetFileIcon(strPath, sz.Width, sz.Height);
bImgIsNew = true;
}
if(img == null)
{
BinaryDataClass bdc = BinaryDataClassifier.ClassifyUrl(strPath);
if((bdc == BinaryDataClass.Text) || (bdc == BinaryDataClass.RichText))
img = Properties.Resources.B16x16_ASCII;
else if(bdc == BinaryDataClass.Image)
img = Properties.Resources.B16x16_Spreadsheet;
else if(bdc == BinaryDataClass.WebDocument)
img = Properties.Resources.B16x16_HTML;
else if(strPath.EndsWith(".exe", StrUtil.CaseIgnoreCmp))
img = Properties.Resources.B16x16_Make_KDevelop;
else
img = Properties.Resources.B16x16_Binary;
bImgIsNew = false;
}
if(img == null) { Debug.Assert(false); return null; }
if((img.Width != sz.Width) || (img.Height != sz.Height))
{
Image imgSc = GfxUtil.ScaleImage(img, sz.Width, sz.Height,
ScaleTransformFlags.UIIcon);
if(bImgIsNew) img.Dispose();
img = imgSc;
}
if(bAllowCache) g_dCache[strKey] = img;
if(bNewImageObject && (bAllowCache || !bImgIsNew))
img = (img.Clone() as Image);
return img;
}
internal static void UpdateImages(ListView lv,
GFunc<ListViewItem, string> fItemToName)
{
if(lv == null) { Debug.Assert(false); return; }
Size sz = GetSizeOrSmall(null);
ImageList ilToDispose = lv.SmallImageList;
lv.BeginUpdate();
List<Image> lImages = new List<Image>();
foreach(ListViewItem lvi in lv.Items)
{
if(lvi == null) { Debug.Assert(false); continue; }
string strName;
if(fItemToName != null) strName = fItemToName(lvi);
else strName = lvi.Text;
Image img = GetImageForName(strName, sz);
if(img == null) { Debug.Assert(false); continue; }
int iImage = -1;
for(int i = 0; i < lImages.Count; ++i)
{
if(object.ReferenceEquals(lImages[i], img))
{
iImage = i;
break;
}
}
if(iImage < 0)
{
iImage = lImages.Count;
lImages.Add(img);
}
lvi.ImageIndex = iImage;
}
if(lImages.Count != 0)
{
ImageList il = UIUtil.BuildImageListUnscaled(lImages,
sz.Width, sz.Height);
#if DEBUG
il.Tag = FiImageListTag;
#endif
lv.SmallImageList = il;
}
else if(ilToDispose != null)
lv.SmallImageList = null; // Release previous ImageList
lv.EndUpdate();
if(ilToDispose != null)
{
Debug.Assert((ilToDispose.Tag as string) == FiImageListTag);
ilToDispose.Dispose();
}
}
internal static void ReleaseImages(ListView lv)
{
if(lv == null) { Debug.Assert(false); return; }
ImageList il = lv.SmallImageList;
if(il == null) return;
lv.BeginUpdate();
foreach(ListViewItem lvi in lv.Items) lvi.ImageIndex = -1;
lv.SmallImageList = null;
lv.EndUpdate();
Debug.Assert((il.Tag as string) == FiImageListTag);
il.Dispose();
}
}
}
|