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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 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.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using KeePass.Resources;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Utility;
namespace KeePass.Forms
{
public partial class IconPickerForm : Form
{
private ImageList m_ilIcons = null;
private uint m_uNumberOfStandardIcons = 0;
private PwDatabase m_pwDatabase = null;
private uint m_uDefaultIcon = 0;
private PwUuid m_pwDefaultCustomIcon = PwUuid.Zero;
private uint m_uChosenImageID = 0;
private PwUuid m_pwChosenCustomImageUuid = PwUuid.Zero;
public uint ChosenIconId
{
get { return m_uChosenImageID; }
}
public PwUuid ChosenCustomIconUuid
{
get { return m_pwChosenCustomImageUuid; }
}
public IconPickerForm()
{
InitializeComponent();
Program.Translation.ApplyTo(this);
}
public void InitEx(ImageList ilIcons, uint uNumberOfStandardIcons,
PwDatabase pwDatabase, uint uDefaultIcon, PwUuid pwCustomIconUuid)
{
m_ilIcons = ilIcons;
m_uNumberOfStandardIcons = uNumberOfStandardIcons;
m_pwDatabase = pwDatabase;
m_uDefaultIcon = uDefaultIcon;
m_pwDefaultCustomIcon = pwCustomIconUuid;
}
private void OnFormLoad(object sender, EventArgs e)
{
Debug.Assert(m_ilIcons != null); if(m_ilIcons == null) throw new InvalidOperationException();
Debug.Assert(m_pwDatabase != null); if(m_pwDatabase == null) throw new InvalidOperationException();
GlobalWindowManager.AddWindow(this);
this.Icon = new Icon("/usr/share/keepass2/KeePass.ico");
FontUtil.AssignDefaultBold(m_radioStandard);
FontUtil.AssignDefaultBold(m_radioCustom);
m_lvIcons.SmallImageList = m_ilIcons;
for(uint i = 0; i < m_uNumberOfStandardIcons; ++i)
m_lvIcons.Items.Add(i.ToString(), (int)i);
int iFoundCustom = RecreateCustomIconList(false);
if((m_pwDefaultCustomIcon != PwUuid.Zero) && (iFoundCustom >= 0))
{
m_radioCustom.Checked = true;
m_lvCustomIcons.Items[iFoundCustom].Selected = true;
}
else if(m_uDefaultIcon < m_uNumberOfStandardIcons)
{
m_radioStandard.Checked = true;
m_lvIcons.Items[(int)m_uDefaultIcon].Selected = true;
}
else
{
Debug.Assert(false);
m_radioStandard.Checked = true;
}
EnableControlsEx();
}
private void EnableControlsEx()
{
ListView.SelectedIndexCollection lvsic = m_lvCustomIcons.SelectedIndices;
if(m_radioStandard.Checked && (m_lvIcons.SelectedIndices.Count == 1))
m_btnOK.Enabled = true;
else if(m_radioCustom.Checked && (lvsic.Count == 1))
m_btnOK.Enabled = true;
else m_btnOK.Enabled = false;
m_btnCustomRemove.Enabled = (lvsic.Count >= 1);
m_btnCustomExport.Enabled = (lvsic.Count >= 1);
// if(m_bBlockCancel)
// {
// m_btnCancel.Enabled = false;
// if(this.ControlBox) this.ControlBox = false;
// }
}
/// <summary>
/// Recreate the custom icons list view.
/// </summary>
/// <returns>Index of the previous custom icon, if specified.</returns>
private int RecreateCustomIconList(bool bSelectLastIcon)
{
m_lvCustomIcons.Items.Clear();
ImageList ilCustom = UIUtil.BuildImageList(m_pwDatabase.CustomIcons, 16, 16);
m_lvCustomIcons.SmallImageList = ilCustom;
int j = 0, iFoundCustom = -1;
foreach(PwCustomIcon pwci in m_pwDatabase.CustomIcons)
{
ListViewItem lvi = m_lvCustomIcons.Items.Add(j.ToString(), j);
lvi.Tag = pwci.Uuid;
if(pwci.Uuid.EqualsValue(m_pwDefaultCustomIcon))
iFoundCustom = j;
++j;
}
if(bSelectLastIcon && (m_lvCustomIcons.Items.Count > 0))
{
m_lvCustomIcons.Items[m_lvCustomIcons.Items.Count - 1].Selected = true;
m_lvCustomIcons.EnsureVisible(m_lvCustomIcons.Items.Count - 1);
UIUtil.SetFocus(m_lvCustomIcons, this);
}
return iFoundCustom;
}
private void OnBtnOK(object sender, EventArgs e)
{
// SaveChosenIcon();
}
private bool SaveChosenIcon()
{
if(m_radioStandard.Checked)
{
ListView.SelectedIndexCollection lvsi = m_lvIcons.SelectedIndices;
if(lvsi.Count != 1) return false;
m_uChosenImageID = (uint)lvsi[0];
}
else // Custom icon
{
ListView.SelectedListViewItemCollection lvsic = m_lvCustomIcons.SelectedItems;
if(lvsic.Count != 1) return false;
m_pwChosenCustomImageUuid = (PwUuid)lvsic[0].Tag;
}
return true;
}
private void OnIconsItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
m_radioCustom.Checked = false;
m_radioStandard.Checked = true;
EnableControlsEx();
}
private void OnCustomIconsItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
m_radioStandard.Checked = false;
m_radioCustom.Checked = true;
EnableControlsEx();
}
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
GlobalWindowManager.RemoveWindow(this);
}
private void OnStandardRadioCheckedChanged(object sender, EventArgs e)
{
EnableControlsEx();
}
private void OnBtnCustomAdd(object sender, EventArgs e)
{
string strAllSupportedFilter = KPRes.AllSupportedFiles +
@" (*.bmp; *.emf; *.gif; *.ico; *.jpg; *.jpe; *.jpeg; *.jfif; *.jfi; *.jif; *.png; *.tif; *.tiff; *.wmf)" +
@"|*.bmp;*.emf;*.gif;*.ico;*.jpg;*.jpe;*.jpeg;*.jfif;*.jfi;*.jif;*.png;*.tif;*.tiff;*.wmf";
StringBuilder sbFilter = new StringBuilder();
sbFilter.Append(strAllSupportedFilter);
AddFileType(sbFilter, "*.bmp", "Windows Bitmap (*.bmp)");
AddFileType(sbFilter, "*.emf", "Windows Enhanced Metafile (*.emf)");
AddFileType(sbFilter, "*.gif", "Graphics Interchange Format (*.gif)");
AddFileType(sbFilter, "*.ico", "Windows Icon (*.ico)");
AddFileType(sbFilter, "*.jpg;*.jpe;*.jpeg;*.jfif;*.jfi;*.jif", "JPEG (*.jpg; *.jpe; *.jpeg; *.jfif; *.jfi; *.jif)");
AddFileType(sbFilter, "*.png", "Portable Network Graphics (*.png)");
AddFileType(sbFilter, "*.tif;*.tiff", "Tagged Image File Format (*.tif; *.tiff)");
AddFileType(sbFilter, "*.wmf", "Windows Metafile (*.wmf)");
sbFilter.Append(@"|" + KPRes.AllFiles + @" (*.*)|*.*");
OpenFileDialog ofd = UIUtil.CreateOpenFileDialog(KPRes.ImportFileTitle,
sbFilter.ToString(), 1, null, true, true);
if(ofd.ShowDialog() == DialogResult.OK)
{
bool bSelectLastIcon = false;
foreach(string strFile in ofd.FileNames)
{
bool bUnsupportedFormat = false;
try
{
if(!File.Exists(strFile))
throw new FileNotFoundException();
// Image img = Image.FromFile(strFile);
// Image img = Image.FromFile(strFile, false);
// Image img = Bitmap.FromFile(strFile);
// Bitmap img = new Bitmap(strFile);
// Image img = Image.FromFile(strFile);
byte[] pb = File.ReadAllBytes(strFile);
// MemoryStream msSource = new MemoryStream(pb, false);
// Image img = Image.FromStream(msSource);
// msSource.Close();
Image img = UIUtil.LoadImage(pb);
Image imgNew = new Bitmap(img, new Size(16, 16));
MemoryStream ms = new MemoryStream();
imgNew.Save(ms, ImageFormat.Png);
PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true),
ms.ToArray());
m_pwDatabase.CustomIcons.Add(pwci);
ms.Close();
m_pwDatabase.UINeedsIconUpdate = true;
bSelectLastIcon = true;
}
catch(ArgumentException)
{
bUnsupportedFormat = true;
}
catch(System.Runtime.InteropServices.ExternalException)
{
bUnsupportedFormat = true;
}
catch(Exception exImg)
{
MessageService.ShowWarning(strFile, exImg);
}
if(bUnsupportedFormat)
MessageService.ShowWarning(strFile, KPRes.ImageFormatFeatureUnsupported);
}
RecreateCustomIconList(bSelectLastIcon);
}
EnableControlsEx();
}
private static void AddFileType(StringBuilder sbBuffer, string strEnding,
string strName)
{
if(sbBuffer.Length > 0) sbBuffer.Append('|');
sbBuffer.Append(strName);
sbBuffer.Append('|');
sbBuffer.Append(strEnding);
}
private void OnBtnCustomRemove(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection lvsicSel = m_lvCustomIcons.SelectedItems;
List<PwUuid> vUuidsToDelete = new List<PwUuid>();
foreach(ListViewItem lvi in lvsicSel)
{
PwUuid uuidIcon = lvi.Tag as PwUuid;
Debug.Assert(uuidIcon != null);
if(uuidIcon != null) vUuidsToDelete.Add(uuidIcon);
}
m_pwDatabase.DeleteCustomIcons(vUuidsToDelete);
if(vUuidsToDelete.Count > 0)
m_pwDatabase.UINeedsIconUpdate = true;
RecreateCustomIconList(false);
EnableControlsEx();
}
private void OnIconsItemActivate(object sender, EventArgs e)
{
OnIconsItemSelectionChanged(sender, null);
if(!SaveChosenIcon()) return;
this.DialogResult = DialogResult.OK;
}
private void OnCustomIconsItemActivate(object sender, EventArgs e)
{
OnCustomIconsItemSelectionChanged(sender, null);
if(!SaveChosenIcon()) return;
this.DialogResult = DialogResult.OK;
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
// if(m_bBlockCancel && (e.CloseReason == CloseReason.UserClosing) &&
// (this.DialogResult != DialogResult.OK))
// e.Cancel = true;
if(!SaveChosenIcon())
{
e.Cancel = true;
MessageService.ShowWarning(KPRes.PickIcon);
}
}
private void OnBtnCustomSave(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection lvsic = m_lvCustomIcons.SelectedItems;
if((lvsic == null) || (lvsic.Count == 0)) return;
if(lvsic.Count == 1)
{
StringBuilder sbFilter = new StringBuilder();
AddFileType(sbFilter, "*.png", "Portable Network Graphics (*.png)");
// AddFileType(sbFilter, "*.ico", "Windows Icon (*.ico)");
sbFilter.Append(@"|" + KPRes.AllFiles + @" (*.*)|*.*");
SaveFileDialog sfd = UIUtil.CreateSaveFileDialog(KPRes.ExportFileTitle,
KPRes.Export + ".png", sbFilter.ToString(), 1, null, true);
if(sfd.ShowDialog() == DialogResult.OK)
SaveImageFile(lvsic[0], sfd.FileName);
}
else // lvsic.Count >= 2
{
FolderBrowserDialog fbd = UIUtil.CreateFolderBrowserDialog(KPRes.ExportToPrompt);
if(fbd.ShowDialog() == DialogResult.OK)
{
string strDir = UrlUtil.EnsureTerminatingSeparator(
fbd.SelectedPath, false);
int nExportIndex = 0;
foreach(ListViewItem lvi in lvsic)
{
try
{
string strFile;
do
{
strFile = strDir + KPRes.Export +
nExportIndex.ToString() + ".png";
++nExportIndex;
}
while(File.Exists(strFile));
SaveImageFile(lvi, strFile);
}
catch(Exception ex)
{
MessageService.ShowWarning(ex.Message);
}
}
}
}
}
private void SaveImageFile(ListViewItem lvi, string strFile)
{
if((lvi == null) || string.IsNullOrEmpty(strFile)) { Debug.Assert(false); return; }
try
{
PwUuid pwUuid = (lvi.Tag as PwUuid);
if(pwUuid == null) { Debug.Assert(false); return; }
Image img = m_pwDatabase.GetCustomIcon(pwUuid);
if(img == null) { Debug.Assert(false); return; }
// string strExt = UrlUtil.GetExtension(strFile);
ImageFormat fmt = ImageFormat.Png;
// if(strExt.Equals("ico", StrUtil.CaseIgnoreCmp)) fmt = ImageFormat.Icon;
img.Save(strFile, fmt);
}
catch(Exception ex)
{
MessageService.ShowWarning(ex.Message);
}
}
}
}
|