Custom icon. PwCustomIcon objects are immutable.
Пример #1
0
		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) == false)
							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);

						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();
		}
        /// <summary>
        /// Downloads one favicon and attaches it to the entry
        /// </summary>
        /// <param name="pwe">The entry for which we want to download the favicon</param>
        private void downloadOneFavicon(PwEntry pwe, ref string message)
        {
            // TODO: create async jobs instead?

            string url = pwe.Strings.ReadSafe("URL");

            // If we have no URL, quit
            if (string.IsNullOrEmpty(url))
                return;

            // If we have a URL with specific scheme that is not http or https, quit
            if (!url.StartsWith("http://") && !url.StartsWith("https://")
                && url.Contains("://"))
                return;

            int dotIndex = url.IndexOf(".");
            if (dotIndex >= 0)
            {
                string protocol = "http";
                string fullURL = url;

                // trim any path data
                int slashDotIndex = url.IndexOf("/", dotIndex);
                if (slashDotIndex >= 0)
                    url = url.Substring(0, slashDotIndex);

                // If there is a protocol/scheme prepended to the URL, strip it off.
                int protocolEndIndex = url.LastIndexOf("/");
                if (protocolEndIndex >= 0)
                {
                    protocol = url.Substring(0,protocolEndIndex-2);
                    url = url.Substring(protocolEndIndex + 1);
                }

                MemoryStream ms = null;
                bool success = getFromFaviconExplicitLocation(url, protocol, fullURL, ref ms, ref message);

                if (!success)
                    success = getFromFaviconStandardLocation(url, protocol, ref ms, ref message);

                if (!success)
                    return;

                // If we found an icon then we don't care whether one particular download method failed.
                message = "";

                byte[] msByteArray = ms.ToArray();

                foreach (PwCustomIcon item in m_host.Database.CustomIcons)
                {
                    // re-use existing custom icon if it's already in the database
                    // (This will probably fail if database is used on
                    // both 32 bit and 64 bit machines - not sure why...)
                    if (KeePassLib.Utility.MemUtil.ArraysEqual(msByteArray, item.ImageDataPng))
                    {
                        pwe.CustomIconUuid = item.Uuid;
                        pwe.Touch(true);
                        m_host.Database.UINeedsIconUpdate = true;
                        return;
                    }
                }

                // Create a new custom icon for use with this entry
                PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true),
                    ms.ToArray());
                m_host.Database.CustomIcons.Add(pwci);
                pwe.CustomIconUuid = pwci.Uuid;
                pwe.Touch(true);
                m_host.Database.UINeedsIconUpdate = true;
            }
        }
Пример #3
0
        private PwUuid GetKeeFoxIcon()
        {
            //return null;

            // {EB9FF2ED-0512-4747-B63E-AFA515A30422}
            PwUuid keeFoxIconUuid = new PwUuid(new byte[] {
                0xeb, 0x9f, 0xf2, 0xed, 0x05, 0x12, 0x47, 0x47,
                0xb6, 0x3e, 0xaf, 0xa5, 0x15, 0xa3, 0x04, 0x22});

            PwCustomIcon icon = null;

            foreach (PwCustomIcon testIcon in _host.Database.CustomIcons)
            {
                if (testIcon.Uuid == keeFoxIconUuid)
                {
                    icon = testIcon;
                    break;
                }
            }

            if (icon == null)
            {
                MemoryStream ms = new MemoryStream();
                global::KeePassRPC.Properties.Resources.KeeFox16.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                // Create a new custom icon for use with this entry
                icon = new PwCustomIcon(keeFoxIconUuid,
                    ms.ToArray());
                _host.Database.CustomIcons.Add(icon);
            }
            return keeFoxIconUuid;


            //string keeFoxIcon = @"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAFfKj/FAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAABpUExURf///wAAAAAAAFpaWl5eXm5ubnh4eICAgIeHh5GRkaCgoKOjo66urq+vr8jIyMnJycvLy9LS0uDg4Ovr6+zs7O3t7e7u7u/v7/X19fb29vf39/j4+Pn5+fr6+vv7+/z8/P39/f7+/v///5goWdMAAAADdFJOUwAxTTRG/kEAAACRSURBVBjTTY2JEoMgDESDaO0h9m5DUZT9/49sCDLtzpB5eQwLkSTkwb0cOBnJksYxiHqORHZG3gFc88WReTzvBFoOMbUCVkN/ATw3CnwHmwLjpYCfYoF5TQphAUztMfp5zsm5phY6MEsV+LapYRPAoC/ooOLxfL33RXQifJjjsnZFWPBniksCbBU+6F4FmV+IvtrgDOmaq+PeAAAAAElFTkSuQmCC";

            //byte[] msByteArray = ms.ToArray();

            //foreach (PwCustomIcon item in _host.Database.CustomIcons)
            //{
            //    *var* t = item.Image.[1][2];
            //    // re-use existing custom icon if it's already in the database
            //    // (This will probably fail if database is used on 
            //    // both 32 bit and 64 bit machines - not sure why...)
            //    if (KeePassLib.Utility.MemUtil.ArraysEqual(msByteArray, item.ImageDataPng))
            //    {
            //        pwe.CustomIconUuid = item.Uuid;
            //        m_host.Database.UINeedsIconUpdate = true;
            //        return;
            //    }
            //}

            //    // Create a new custom icon for use with this entry
            //    PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true),
            //        ms.ToArray());
            //    m_host.Database.CustomIcons.Add(pwci);

            //    return pwci.Uuid;
        }
Пример #4
0
        /// <summary>
        /// converts a string to the relevant icon for this entry
        /// </summary>
        /// <param name="imageData">base64 representation of the image</param>
        /// <param name="customIconUUID">UUID of the generated custom icon; may be Zero</param>
        /// <param name="iconId">PwIcon of the matched standard icon; ignore if customIconUUID != Zero</param>
        /// <returns>true if the supplied imageData was converted into a customIcon 
        /// or matched with a standard icon.</returns>
        private bool base64ToIcon(string imageData, ref PwUuid customIconUUID, ref PwIcon iconId)
        {
            iconId = PwIcon.Key;
            customIconUUID = PwUuid.Zero;

            for (int i = 0; i < _standardIconsBase64.Length; i++)
            {
                string item = _standardIconsBase64[i];
                if (item == imageData)
                {
                    iconId = (PwIcon)i;
                    return true;
                }
            }

            try
            {
                //MemoryStream id = new MemoryStream();
                //icon.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                Image img = KeePass.UI.UIUtil.LoadImage(Convert.FromBase64String(imageData));

                Image imgNew = new Bitmap(img, new Size(16, 16));

                MemoryStream ms = new MemoryStream();
                imgNew.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                byte[] msByteArray = ms.ToArray();

                foreach (PwCustomIcon item in host.Database.CustomIcons)
                {
                    // re-use existing custom icon if it's already in the database
                    // (This will probably fail if database is used on
                    // both 32 bit and 64 bit machines - not sure why...)
                    if (KeePassLib.Utility.MemUtil.ArraysEqual(msByteArray, item.ImageDataPng))
                    {
                        customIconUUID = item.Uuid;
                        host.Database.UINeedsIconUpdate = true;
                        return true;
                    }
                }

                PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true),
                    msByteArray);
                host.Database.CustomIcons.Add(pwci);

                customIconUUID = pwci.Uuid;
                host.Database.UINeedsIconUpdate = true;

                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// Downloads one favicon and attaches it to the entry
        /// </summary>
        /// <param name="pwe">The entry for which we want to download the favicon</param>
        private void downloadOneFavicon(PwEntry pwe, ref string message)
        {
            // TODO: create async jobs instead?

            string url = pwe.Strings.ReadSafe("URL");

            if (string.IsNullOrEmpty(url))
                url = pwe.Strings.ReadSafe("Title");

            // If we still have no URL, quit
            if (string.IsNullOrEmpty(url))
                return;

            // If we have a URL with specific scheme that is not http or https, quit
            if (!url.StartsWith("http://") && !url.StartsWith("https://")
                && url.Contains("://"))
                return;

            int dotIndex = url.IndexOf(".");
            if (dotIndex >= 0)
            {
                Uri fullURI = null;
                try {
                    fullURI = new Uri((url.StartsWith("http://")||url.StartsWith("https://"))?url:"http://"+url,UriKind.Absolute);
                }
                catch (Exception ex)
                {
                    message += url + "\n" + ex.Message;
                    return;
                }

                MemoryStream ms = null;
                Uri lastURI = getFromFaviconExplicitLocation(fullURI, ref ms, ref message);
                bool success = (lastURI != null) && lastURI.OriginalString.Equals("http://success");

                if (!success)
                {
                    success = getFavicon(new Uri((lastURI==null)?fullURI:lastURI,"/favicon.ico"), ref ms, ref message);
                }

                if (!success)
                    return;

                // If we found an icon then we don't care whether one particular download method failed.
                message = "";

                byte[] msByteArray = ms.ToArray();

                foreach (PwCustomIcon item in m_host.Database.CustomIcons)
                {
                    // re-use existing custom icon if it's already in the database
                    // (This will probably fail if database is used on
                    // both 32 bit and 64 bit machines - not sure why...)
                    if (KeePassLib.Utility.MemUtil.ArraysEqual(msByteArray, item.ImageDataPng))
                    {
                        pwe.CustomIconUuid = item.Uuid;
                        pwe.Touch(true);
                        m_host.Database.UINeedsIconUpdate = true;
                        return;
                    }
                }

                // Create a new custom icon for use with this entry
                PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true),
                    ms.ToArray());
                m_host.Database.CustomIcons.Add(pwci);
                pwe.CustomIconUuid = pwci.Uuid;
                pwe.Touch(true);
                m_host.Database.UINeedsIconUpdate = true;
            }
        }
Пример #6
0
        private PwCustomIcon getOrAddCustomIcon(byte[] icon_data)
        {
            if (null == icon_data)
            {
                return null;
            }

            try
            {
                System.Collections.Generic.List<PwCustomIcon> icon_entries = m_host.Database.CustomIcons;

                for (int i = 0; i < icon_entries.Count; i++)
                {
                    string checksum1 = CalculateMD5Hash(icon_entries[i].ImageDataPng);
                    string checksum2 = CalculateMD5Hash(icon_data);

                    if (checksum1 == checksum2)
                    {
                        return icon_entries[i];
                    }
                }

                PwUuid uuid = new PwUuid(true);
                PwCustomIcon custom_icon = new PwCustomIcon(uuid, icon_data);
                m_host.Database.CustomIcons.Add(custom_icon);
                return custom_icon;
            }
            catch (Exception ex)
            {
                showError(ex);
            }

            return null;
        }