Exemplo n.º 1
0
        private void OnManageAutoExportClicked(object sender, EventArgs e)
        {
            if (ReferenceEquals(_host, null) || ReferenceEquals(_host.Database, null) || !_host.Database.IsOpen)
            {
                return;
            }

            KeePassLib.Collections.PwObjectList <KeePassLib.PwEntry> entries = new KeePassLib.Collections.PwObjectList <KeePassLib.PwEntry>();
            _host.Database.RootGroup.FindEntriesByTag(Tag, entries, true);
            ExportManager exportManager = new ExportManager(entries.Select(ConvertToExportItem));

            exportManager.ShowDialog();

            //Remove deleted entry
            bool hasDelete = false;

            foreach (KeePassLib.PwEntry entry in entries)
            {
                string uuid = entry.Uuid.ToHexString();
                if (exportManager.Exports.Any(ei => !string.IsNullOrEmpty(ei.Uuid) && string.Equals(ei.Uuid, uuid, StringComparison.InvariantCultureIgnoreCase)))
                {
                    continue;
                }

                hasDelete = true;
                KeePassLib.PwDeletedObject deletedObject = new KeePassLib.PwDeletedObject(entry.Uuid, DateTime.Now);
                _host.Database.DeletedObjects.Add(deletedObject);
            }

            //Create new entry
            bool hasNew = false;

            KeePassLib.PwGroup savingGroup = FindOrCreatePluginGroup();
            foreach (ExportItem exportItem in exportManager.Exports)
            {
                if (!string.IsNullOrEmpty(exportItem.Uuid))
                {
                    continue;
                }

                hasNew = true;
                KeePassLib.PwEntry newExportEntry = new KeePassLib.PwEntry(true, true);
                newExportEntry.Strings.Set(KeePassLib.PwDefs.TitleField, new KeePassLib.Security.ProtectedString(false, Path.GetFileName(exportItem.Path.LocalPath)));
                newExportEntry.Strings.Set(KeePassLib.PwDefs.UrlField, new KeePassLib.Security.ProtectedString(false, exportItem.Path.ToString()));
                newExportEntry.IconId = KeePassLib.PwIcon.Disk;
                newExportEntry.Tags   = new List <string>()
                {
                    Tag
                };
                newExportEntry.Strings.Set(KeePassLib.PwDefs.PasswordField, exportItem.Password);
                savingGroup.AddEntry(newExportEntry, true);
            }

            if (hasDelete || hasNew) //Perform action on database
            {
                _host.Database.MergeIn(_host.Database, KeePassLib.PwMergeMethod.Synchronize);

                //Refresh GUI (Event is launched by main UI thread)
                _host.MainWindow.UpdateUI(false, null, true, savingGroup, true, null, true);
                _host.MainWindow.RefreshEntriesList();
            }
        }