public IList<EntryModel> FindEntries(GroupModel group)
        {
            IList<EntryModel> model = new List<EntryModel> ();
             PwGroup pg = group.Group;
             bool bSubEntries = false;

             PwObjectList<PwEntry> pwlSource = ((pg != null) ? pg.GetEntries (bSubEntries) : new PwObjectList<PwEntry> ());

             m_dtCachedNow = DateTime.Now;

             if (pg != null) {
            foreach (PwEntry pe in pwlSource) {

               EntryModel entry = new EntryModel ();

               entry.Group = group;
               entry.Entry = pe;

               string strMain = GetEntryFieldEx (pe, ColumnType.Title, true);
               entry.Title = strMain;

               if (pe.Expires && (pe.ExpiryTime <= m_dtCachedNow)) {
                  entry.CustomIconId = (int)PwIcon.Expired;
               }

               // get data for the other columns...
               entry.UserName = GetEntryFieldEx (pe, ColumnType.UserName, true);
               entry.Password = GetEntryFieldEx (pe, ColumnType.Password, true);
               entry.Url = GetEntryFieldEx (pe, ColumnType.Url, true);
               entry.Notes = GetEntryFieldEx (pe, ColumnType.Notes, true);

               model.Add (entry);
            }
             }

             return model;
        }
        public bool SaveEntry(EntryModel edited, PwEntry m_pwInitialEntry/*, bool bValidate*/)
        {
            const PwCompareOptions m_cmpOpt = (PwCompareOptions.NullEmptyEquivStd | PwCompareOptions.IgnoreTimes);

             PwEntry originalEntry = edited.Entry;
             //peTarget.History = m_vHistory; // Must be called before CreateBackup()

             bool bCreateBackup = !edited.IsNew;

             if(bCreateBackup)
            originalEntry.CreateBackup(null);

            //         peTarget.IconId = m_pwEntryIcon;
            //         peTarget.CustomIconUuid = m_pwCustomIconID;
            //
            //         if(m_cbCustomForegroundColor.Checked)
            //            peTarget.ForegroundColor = m_clrForeground;
            //         else peTarget.ForegroundColor = Color.Empty;
            //         if(m_cbCustomBackgroundColor.Checked)
            //            peTarget.BackgroundColor = m_clrBackground;
            //         else peTarget.BackgroundColor = Color.Empty;

             //peTarget.OverrideUrl = m_tbOverrideUrl.Text;

            //         List<string> vNewTags = StrUtil.StringToTags(m_tbTags.Text);
            //         peTarget.Tags.Clear();
            //         foreach(string strTag in vNewTags) peTarget.AddTag(strTag);

             originalEntry.Expires = edited.Expires;

             if(originalEntry.Expires)
            originalEntry.ExpiryTime = edited.ExpireDate;

             UpdateEntryStrings(edited, originalEntry.Strings);

            //         peTarget.Strings = m_vStrings;
            //         peTarget.Binaries = m_vBinaries;

            //         m_atConfig.Enabled = m_cbAutoTypeEnabled.Checked;
            //         m_atConfig.ObfuscationOptions = (m_cbAutoTypeObfuscation.Checked ?
            //            AutoTypeObfuscationOptions.UseClipboard :
            //            AutoTypeObfuscationOptions.None);
            //
            //         SaveDefaultSeq();

            //         peTarget.AutoType = m_atConfig;

             originalEntry.Touch(true, false); // Touch *after* backup

            //         if(object.ReferenceEquals(peTarget, m_pwEntry))
            //            m_bTouchedOnce = true;

             StrUtil.NormalizeNewLines(originalEntry.Strings, true);

             bool bUndoBackup = false;
             PwCompareOptions cmpOpt = m_cmpOpt;

             if(bCreateBackup)
            cmpOpt |= PwCompareOptions.IgnoreLastBackup;

             if(originalEntry.EqualsEntry(m_pwInitialEntry, cmpOpt, MemProtCmpMode.CustomOnly))
             {
            // No modifications at all => restore last mod time and undo backup
            originalEntry.LastModificationTime = m_pwInitialEntry.LastModificationTime;
            bUndoBackup = bCreateBackup;
             }
             else if(bCreateBackup)
             {
            // If only history items have been modified (deleted) => undo
            // backup, but without restoring the last mod time
            PwCompareOptions cmpOptNH = (m_cmpOpt | PwCompareOptions.IgnoreHistory);
            if(originalEntry.EqualsEntry(m_pwInitialEntry, cmpOptNH, MemProtCmpMode.CustomOnly))
               bUndoBackup = true;
             }

             if(bUndoBackup)
            originalEntry.History.RemoveAt(originalEntry.History.UCount - 1);

             originalEntry.MaintainBackups(db);

             return true;
        }
        public void UpdateEntryStrings(EntryModel edited, ProtectedStringDictionary m_vStrings /*, bool bGuiToInternal, bool bSetRepeatPw*/)
        {
            //         if(bGuiToInternal)
            //         {
            m_vStrings.Set(PwDefs.TitleField, new ProtectedString(db.MemoryProtection.ProtectTitle, edited.Title));
            m_vStrings.Set(PwDefs.UserNameField, new ProtectedString(db.MemoryProtection.ProtectUserName, edited.UserName));

            byte[] pb = edited.Password.ToUtf8(); //m_icgPassword.GetPasswordUtf8();
            m_vStrings.Set(PwDefs.PasswordField, new ProtectedString(db.MemoryProtection.ProtectPassword, pb));
            MemUtil.ZeroByteArray(pb);

            m_vStrings.Set(PwDefs.UrlField, new ProtectedString(db.MemoryProtection.ProtectUrl, edited.Url));
            m_vStrings.Set(PwDefs.NotesField, new ProtectedString(db.MemoryProtection.ProtectNotes, edited.Notes));
            //         }
            //         else // Internal to GUI
            //         {
            //            m_tbTitle.Text = m_vStrings.ReadSafe(PwDefs.TitleField);
            //            m_tbUserName.Text = m_vStrings.ReadSafe(PwDefs.UserNameField);
            //
            //            byte[] pb = m_vStrings.GetSafe(PwDefs.PasswordField).ReadUtf8();
            //            m_icgPassword.SetPassword(pb, bSetRepeatPw);
            //            MemUtil.ZeroByteArray(pb);
            //
            //            m_tbUrl.Text = m_vStrings.ReadSafe(PwDefs.UrlField);
            //            m_rtNotes.Text = m_vStrings.ReadSafe(PwDefs.NotesField);
            //
            //            int iTopVisible = UIUtil.GetTopVisibleItem(m_lvStrings);
            //            m_lvStrings.Items.Clear();
            //            foreach(KeyValuePair<string, ProtectedString> kvpStr in m_vStrings)
            //            {
            //               if(!PwDefs.IsStandardField(kvpStr.Key))
            //               {
            //                  PwIcon pwIcon = (kvpStr.Value.IsProtected ? m_pwObjectProtected :
            //                     m_pwObjectPlainText);
            //
            //                  ListViewItem lvi = m_lvStrings.Items.Add(kvpStr.Key, (int)pwIcon);
            //
            //                  if(kvpStr.Value.IsProtected)
            //                     lvi.SubItems.Add(PwDefs.HiddenPassword);
            //                  else
            //                  {
            //                     string strValue = StrUtil.MultiToSingleLine(
            //                        kvpStr.Value.ReadString());
            //                     lvi.SubItems.Add(strValue);
            //                  }
            //               }
            //            }
            //            UIUtil.SetTopVisibleItem(m_lvStrings, iTopVisible);
        }
        /// <summary>
        /// Edit the specified source and sender.
        /// </summary>
        /// <param name='source'>
        /// Source.
        /// </param>
        /// <param name='sender'>
        /// Sender.
        /// </param>
        public EntryModel Edit(EntryModel entry, MainWindowController sender)
        {
            NSWindow window = this.Window;

             cancelled = false;

             //var editFields = editForm.Cells;

             if (entry != null)
             {
            originalEntry = entry.Entry.CloneDeep();

            //            m_vStrings = source.Entry.Strings.CloneDeep();
            //            m_vBinaries = source.Entry.Binaries.CloneDeep();
            //            m_vHistory = source.Entry.History.CloneDeep();

            TitleTextField.StringValue = entry.Title;
            UserNameTextField.StringValue = entry.UserName;

            PasswordSecureTextField.StringValue = PwDefs.HiddenPassword;
            PasswordClearTextField.StringValue = entry.Password;

            UrlTextField.StringValue = entry.Url;
            NotesTextField.StringValue = entry.Notes;
            ExpireCheckBox.State = entry.Expires ? NSCellStateValue.On : NSCellStateValue.Off;
            ExpireTimeDatePicker.DateValue = entry.ExpireDate;
             }
             else
             {
            entry = new EntryModel();
            entry.Entry = new PwEntry(true, true);
            entry.IsNew = true;

            // we are adding a new entry,
            // make sure the form fields are empty due to the fact that this controller is recycled
            // each time the user opens the sheet -
            TitleTextField.StringValue = string.Empty;
            UserNameTextField.StringValue = string.Empty;
            PasswordSecureTextField.StringValue = string.Empty;
            PasswordClearTextField.StringValue = string.Empty;
            UrlTextField.StringValue = string.Empty;
            NotesTextField.StringValue = string.Empty;
            ExpireCheckBox.State = NSCellStateValue.Off;
            ExpireTimeDatePicker.DateValue = null;
             }

             NSApp.BeginSheet(window,sender.Window);
             NSApp.RunModalForWindow(window);
             // sheet is up here.....

             // when StopModal is called will continue here ....
             Console.WriteLine("Edit - exit from Edit");
             NSApp.EndSheet(window);
             window.OrderOut(this);

             //EntryModel saved = new EntryModel();
             entry.Title = TitleTextField.StringValue;
             entry.UserName = UserNameTextField.StringValue;
             entry.Password = PasswordSecureTextField.StringValue;
             entry.Url = UrlTextField.StringValue;
             entry.Notes = NotesTextField.StringValue;
             entry.Expires = ExpireCheckBox.State == NSCellStateValue.On;
             entry.ExpireDate = NSDateToDateTime(ExpireTimeDatePicker.DateValue);

             return entry;
        }