private void DeleteTemplate(Template template)
        {
            int     foundEntries  = 0;
            PwEntry singlePwEntry = null;

            foreach (var pwGroup in _allGroups)
            {
                foreach (var pwEntry in pwGroup.GetEntries(false))
                {
                    string ser = pwEntry.Strings.Get(PwDefs.NotesField).ReadString();
                    if (ser != null)
                    {
                        Template entryTemplate = (Template)
                                                 SerializationDotNet2.Xml.Deserializer.DeserializeObjectFromString(
                            EncodingEx.Base64.Decoder.DecodeString(Encoding.UTF8, ser), typeof(Template));

                        if (entryTemplate.UTID == template.UTID)
                        {
                            singlePwEntry = pwEntry;
                            foundEntries++;
                        }
                        else if (entryTemplate.BoundUrl.Value.Value.Contains(template.BoundUrl.Value.Value) ||
                                 template.BoundUrl.Value.Value.Contains(entryTemplate.BoundUrl.Value.Value))
                        {
                            foundEntries++;
                        }
                    }
                }
            }

            if (singlePwEntry == null)
            {
                MessageBox.Show("Could not find any template to delete, sorry");
                return;
            }
            if (foundEntries > 1)
            {
                MessageBox.Show("There are too many templates that match on the bound URL or share the same UTID");
                return;
            }
            PwGroup pwgroup = singlePwEntry.ParentGroup;

            pwgroup.Entries.Remove(singlePwEntry);

            KeePassPasswordChangerExt.RefreshUiGroup(pwgroup);
            //KeePassPasswordChangerExt.SaveCurrentDb();
            RefreshUI();
        }
        private void buttonImportTemplates_Click(object sender, EventArgs e)
        {
            try
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                folderBrowserDialog.Description = "Select a destination folder for your " +
                                                  (_isPrivateMode ? "private" : "public") +
                                                  " templates to export to (subfolder will be created)";
                folderBrowserDialog.ShowNewFolderButton = true;
                //folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyDocuments;
                folderBrowserDialog.SelectedPath = "Exported " + (_isPrivateMode ? "Private" : "Public") + " Templates";
                int count = 0;
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    if (Directory.Exists(folderBrowserDialog.SelectedPath))
                    {
                        foreach (var file in Directory.GetFiles(folderBrowserDialog.SelectedPath))
                        {
                            if (!file.EndsWith(Options.ExportExtension))
                            {
                                continue;
                            }
                            try
                            {
                                string content = File.ReadAllText(file, Encoding.UTF8);
                                try
                                {
                                    string decoded = EncodingEx.Base64.Decoder.DecodeString(Encoding.UTF8, content);
                                    try
                                    {
                                        Template template = (Template)
                                                            SerializationDotNet2.Xml.Deserializer.DeserializeObjectFromString(decoded,
                                                                                                                              typeof(Template));
                                        bool    foundAnotherEntry = false;
                                        PwEntry oldEntry          = null;
                                        foreach (var pwGroup in _allGroups)
                                        {
                                            foreach (var pwEntry in pwGroup.GetEntries(false))
                                            {
                                                string ser = pwEntry.Strings.Get(PwDefs.NotesField).ReadString();
                                                if (ser != null)
                                                {
                                                    Template entryTemplate = (Template)
                                                                             SerializationDotNet2.Xml.Deserializer
                                                                             .DeserializeObjectFromString(
                                                        EncodingEx.Base64.Decoder.DecodeString(Encoding.UTF8,
                                                                                               ser), typeof(Template));
                                                    if (
                                                        entryTemplate.BoundUrl.Value.Value.Contains(
                                                            template.BoundUrl.Value.Value) ||
                                                        template.BoundUrl.Value.Value.Contains(
                                                            entryTemplate.BoundUrl.Value.Value))
                                                    {
                                                        if (entryTemplate.UTID == template.UTID)
                                                        {
                                                            if (template.TemplateVersion > entryTemplate.TemplateVersion)
                                                            {
                                                                oldEntry = pwEntry;
                                                                continue;
                                                            }
                                                        }
                                                        MessageBox.Show("Cannot add template '" + template.Name +
                                                                        "' (UTID: '" + template.UTID +
                                                                        "') because of the existing template '" +
                                                                        entryTemplate.Name + "' in the group '" +
                                                                        pwGroup.Name + "', which maps on this URI.");
                                                        foundAnotherEntry = true;
                                                    }
                                                    if (entryTemplate.UTID == template.UTID)
                                                    {
                                                        if (template.TemplateVersion > entryTemplate.TemplateVersion)
                                                        {
                                                            oldEntry = pwEntry;
                                                            continue;
                                                        }
                                                        MessageBox.Show("Cannot add template '" + template.Name +
                                                                        "' Version '" + template.TemplateVersion +
                                                                        "' (UTID: '" + template.UTID +
                                                                        ")' because of the existing template '" +
                                                                        entryTemplate.Name + "' has with Version '" +
                                                                        template.TemplateVersion +
                                                                        "' a higher^or equal number in the group '" +
                                                                        pwGroup.Name + "', which has the same UTID");
                                                        foundAnotherEntry = true;
                                                    }
                                                }
                                            }
                                        }
                                        if (foundAnotherEntry)
                                        {
                                            continue;
                                        }

                                        if (_isPrivateMode)
                                        {
                                            DialogResult dialogResult = MessageBox.Show(
                                                "Do you want to export the template '" + template.Name + "' Version '" +
                                                template.TemplateVersion.ToString() + "'", "Export Template?",
                                                MessageBoxButtons.YesNoCancel,
                                                MessageBoxIcon.Question);
                                            if (dialogResult == DialogResult.No)
                                            {
                                                continue;
                                            }
                                            if (dialogResult == DialogResult.Cancel)
                                            {
                                                break;
                                            }
                                        }

                                        PwEntry entry = new PwEntry(true, true);
                                        entry.Strings.Set(PwDefs.NotesField, new ProtectedString(true, content));
                                        entry.Strings.Set(PwDefs.TitleField,
                                                          new ProtectedString(true,
                                                                              template.Name + " Version " + template.TemplateVersion + " UTID: " +
                                                                              template.UTID));
                                        entry.Strings.Set(PwDefs.UrlField,
                                                          new ProtectedString(true, template.BoundUrl.Value.Value));
                                        _editPwGroup.AddEntry(entry, true);
                                        KeePassPasswordChangerExt.RefreshUiEntry(entry);
                                        if (oldEntry != null)
                                        {
                                            PwGroup pwGroup = oldEntry.ParentGroup;
                                            pwGroup.Entries.Remove(oldEntry);
                                            KeePassPasswordChangerExt.RefreshUiGroup(pwGroup);
                                        }
                                        //KeePassPasswordChangerExt.SaveCurrentDb();
                                        count++;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("Could not deserialize plaintext\n\n" + ex.Message + "\n\n" +
                                                        ex.StackTrace);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Could not base 64 decode file\n\n" + ex.Message + "\n\n" +
                                                    ex.StackTrace);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Could not read file\n\n" + ex.Message + "\n\n" +
                                                ex.StackTrace);
                            }
                        }
                    }
                }
                MessageBox.Show("Completed importing " + count + " Templates", "Completed");
                if (count > 0)
                {
                    RefreshUI();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
            }
        }