Beispiel #1
0
        private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Progress information
            ProgressInfo progress = new ProgressInfo(entries.Length);

            // Custom icons that will be added to the database
            PwCustomIcon[] icons = new PwCustomIcon[entries.Length];

            // Set up proxy information for all WebClients
            FaviconDownloader.Proxy = Util.GetKeePassProxy();

            using (ManualResetEvent waiter = new ManualResetEvent(false))
            {
                for (int j = 0; j < entries.Length; j++)
                {
                    PwEntry entry = entries[j];
                    ThreadPool.QueueUserWorkItem(delegate(object notUsed)
                    {
                        // Checks whether the user pressed the cancel button or the close button
                        if (!logger.ContinueWork())
                        {
                            e.Cancel = true;
                        }
                        else
                        {
                            int i = Interlocked.Increment(ref progress.Current) - 1;

                            // Fields
                            string url = entry.Strings.ReadSafe(PwDefs.UrlField);

                            if (url == string.Empty)
                            {
                                // If the user wants to use the title field, let's give it a try
                                if (YetAnotherFaviconDownloaderExt.Config.GetUseTitleField())
                                {
                                    url = entry.Strings.ReadSafe(PwDefs.TitleField);
                                }
                            }

                            // Empty URL field
                            if (url == string.Empty)
                            {
                                // Can't find an icon
                                Interlocked.Increment(ref progress.NotFound);
                            }
                            else
                            {
                                Util.Log("Downloading: {0}", url);

                                using (FaviconDownloader fd = new FaviconDownloader())
                                {
                                    try
                                    {
                                        // Download favicon
                                        byte[] data = fd.GetIcon(url);
                                        Util.Log("Icon downloaded with success");

                                        // Hash icon data (avoid duplicates)
                                        byte[] hash = Util.HashData(data);

                                        // Creates an icon only if your UUID does not exist
                                        PwUuid uuid = new PwUuid(hash);
                                        if (!pluginHost.Database.CustomIcons.Exists(delegate(PwCustomIcon x) { return(x.Uuid.Equals(uuid)); }))
                                        {
                                            // Add icon
                                            icons[i] = new PwCustomIcon(uuid, data);

                                            #region For KeePass 2.48+ only
                                            if (PwDefs.FileVersion64 >= 0x0002003000000000UL)
                                            {
                                                var pwCustomIconType = icons[i].GetType();

                                                // Name the icon
                                                var nameProperty = pwCustomIconType.GetProperty("Name");
                                                if (nameProperty != null)
                                                {
                                                    // Since the URL was valid, we just force a valid scheme prefix to be able to get the Host
                                                    var host = fd.GetValidHost(url);
                                                    nameProperty.SetValue(icons[i], "yafd-" + host);
                                                }

                                                // Update last modification time
                                                var lastModificationTimeProperty = pwCustomIconType.GetProperty("LastModificationTime");
                                                if (lastModificationTimeProperty != null)
                                                {
                                                    lastModificationTimeProperty.SetValue(icons[i], DateTime.UtcNow);
                                                }
                                            }
                                            #endregion
                                        }

                                        // Check if icon is the same
                                        if (entry.CustomIconUuid.Equals(uuid))
                                        {
                                            // Avoid updating the entry
                                            entries[i] = null;
                                        }
                                        else
                                        {
                                            // Associate with this entry
                                            entry.CustomIconUuid = uuid;
                                        }

                                        // Icon downloaded with success
                                        Interlocked.Increment(ref progress.Success);
                                    }
                                    catch (FaviconDownloaderException ex)
                                    {
                                        Util.Log("Failed to download favicon");

                                        if (ex.Status == FaviconDownloaderExceptionStatus.NotFound)
                                        {
                                            // Can't find an icon
                                            Interlocked.Increment(ref progress.NotFound);
                                        }
                                        else
                                        {
                                            // Some other error (network, etc)
                                            Interlocked.Increment(ref progress.Error);
                                        }

                                        // Avoid updating the entry
                                        entries[i] = null;
                                    }
                                }
                            }
                        }

                        // Notifies that all downloads are finished
                        if (Interlocked.Decrement(ref progress.Remaining) == 0)
                        {
                            waiter.Set();
                        }
                    });
                }

                // Wait until the downloads are finished
                int lastValue = 0;
                do
                {
                    // Update progress only when needed
                    if (lastValue != progress.Remaining)
                    {
                        ReportProgress(progress);
                        lastValue = progress.Remaining;
                    }
                } while (!waiter.WaitOne(100));
            }

            // Progress 100%
            ReportProgress(progress);
            status = string.Format("YAFD: Success: {0} / Not Found: {1} / Error: {2}.", progress.Success, progress.NotFound, progress.Error);

            // Prevents inserting duplicate icons
            MergeCustomIcons(icons);

            // Refresh icons on database
            pluginHost.Database.UINeedsIconUpdate = true;

            // Waits long enough until we can see the output
            Thread.Sleep(3000);
        }
        private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            PwEntry[] entries = e.Argument as PwEntry[];

            // Progress information
            ProgressInfo progress = new ProgressInfo(entries.Length);

            // Custom icons that will be added to the database
            PwCustomIcon[] icons = new PwCustomIcon[entries.Length];

            // Set up proxy information for all WebClients
            FaviconDownloader.Proxy = Util.GetKeePassProxy();

            using (ManualResetEvent waiter = new ManualResetEvent(false))
            {
                for (int j = 0; j < entries.Length; j++)
                {
                    PwEntry entry = entries[j];
                    ThreadPool.QueueUserWorkItem(delegate(object notUsed)
                    {
                        // Checks whether the user pressed the cancel button or the close button
                        if (!logger.ContinueWork())
                        {
                            e.Cancel = true;
                        }
                        else
                        {
                            int i = Interlocked.Increment(ref progress.Current) - 1;

                            // Fields
                            string url = entry.Strings.ReadSafe(PwDefs.UrlField);

                            if (url == string.Empty)
                            {
                                url = entry.Strings.ReadSafe(PwDefs.TitleField);
                            }

                            // Empty URL field
                            if (url == string.Empty)
                            {
                                // Can't find an icon
                                Interlocked.Increment(ref progress.NotFound);
                            }
                            else
                            {
                                Util.Log("Downloading: {0}", url);

                                using (FaviconDownloader fd = new FaviconDownloader())
                                {
                                    try
                                    {
                                        // Download favicon
                                        byte[] data = fd.GetIcon(url);
                                        Util.Log("Icon downloaded with success");

                                        // Hash icon data (avoid duplicates)
                                        byte[] hash = Util.HashData(data);

                                        // Creates an icon only if your UUID does not exist
                                        PwUuid uuid = new PwUuid(hash);
                                        if (!pluginHost.Database.CustomIcons.Exists(delegate(PwCustomIcon x) { return(x.Uuid.Equals(uuid)); }))
                                        {
                                            // Add icon
                                            icons[i] = new PwCustomIcon(uuid, data);
                                        }

                                        // Associate with this entry
                                        entry.CustomIconUuid = uuid;

                                        // Save it
                                        entry.Touch(true, false);

                                        // Icon downloaded with success
                                        Interlocked.Increment(ref progress.Success);
                                    }
                                    catch (FaviconDownloaderException ex)
                                    {
                                        Util.Log("Failed to download favicon");

                                        if (ex.Status == FaviconDownloaderExceptionStatus.NotFound)
                                        {
                                            // Can't find an icon
                                            Interlocked.Increment(ref progress.NotFound);
                                        }
                                        else
                                        {
                                            // Some other error (network, etc)
                                            Interlocked.Increment(ref progress.Error);
                                        }
                                    }
                                }
                            }
                        }

                        // Notifies that all downloads are finished
                        if (Interlocked.Decrement(ref progress.Remaining) == 0)
                        {
                            waiter.Set();
                        }
                    });
                }

                // Wait until the downloads are finished
                int lastValue = 0;
                do
                {
                    // Update progress only when needed
                    if (lastValue != progress.Remaining)
                    {
                        ReportProgress(progress);
                        lastValue = progress.Remaining;
                    }
                } while (!waiter.WaitOne(100));
            }

            // Progress 100%
            ReportProgress(progress);
            status = String.Format("YAFD: Success: {0} / Not Found: {1} / Error: {2}.", progress.Success, progress.NotFound, progress.Error);

            // Prevents inserting duplicate icons
            MergeCustomIcons(icons);

            // Refresh icons on database
            pluginHost.Database.UINeedsIconUpdate = true;

            // Waits long enough until we can see the output
            Thread.Sleep(3000);
        }