示例#1
0
        void SaveEntry()
        {
            Database          db  = App.Kp2a.GetDb();
            EntryEditActivity act = this;

            if (!ValidateBeforeSaving())
            {
                return;
            }

            PwEntry initialEntry = State.EntryInDatabase.CloneDeep();

            PwEntry newEntry = State.EntryInDatabase;

            //Clone history and re-assign:
            newEntry.History = newEntry.History.CloneDeep();

            //Based on KeePass Desktop
            bool bCreateBackup = (!State.IsNew);

            if (bCreateBackup)
            {
                newEntry.CreateBackup(null);
            }

            if (State.SelectedIcon)
            {
                newEntry.IconId         = State.SelectedIconId;
                newEntry.CustomIconUuid = State.SelectedCustomIconId;
            }             //else the State.EntryInDatabase.Icon

            /* KPDesktop
             *      if(m_cbCustomForegroundColor.Checked)
             *              newEntry.ForegroundColor = m_clrForeground;
             *      else newEntry.ForegroundColor = Color.Empty;
             *      if(m_cbCustomBackgroundColor.Checked)
             *              newEntry.BackgroundColor = m_clrBackground;
             *      else newEntry.BackgroundColor = Color.Empty;
             *
             */

            UpdateEntryFromUi(newEntry);
            newEntry.Binaries = State.Entry.Binaries;
            newEntry.Expires  = State.Entry.Expires;
            if (newEntry.Expires)
            {
                newEntry.ExpiryTime = State.Entry.ExpiryTime;
            }


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

            StrUtil.NormalizeNewLines(newEntry.Strings, true);

            bool             bUndoBackup = false;
            PwCompareOptions cmpOpt      = (PwCompareOptions.NullEmptyEquivStd |
                                            PwCompareOptions.IgnoreTimes);

            if (bCreateBackup)
            {
                cmpOpt |= PwCompareOptions.IgnoreLastBackup;
            }
            if (newEntry.EqualsEntry(initialEntry, cmpOpt, MemProtCmpMode.CustomOnly))
            {
                // No modifications at all => restore last mod time and undo backup
                newEntry.LastModificationTime = initialEntry.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 = (cmpOpt | PwCompareOptions.IgnoreHistory);
                if (newEntry.EqualsEntry(initialEntry, cmpOptNh, MemProtCmpMode.CustomOnly))
                {
                    bUndoBackup = true;
                }
            }
            if (bUndoBackup)
            {
                newEntry.History.RemoveAt(newEntry.History.UCount - 1);
            }

            newEntry.MaintainBackups(db.KpDatabase);

            //if ( newEntry.Strings.ReadSafe (PwDefs.TitleField).Equals(State.Entry.Strings.ReadSafe (PwDefs.TitleField)) ) {
            //	SetResult(KeePass.EXIT_REFRESH);
            //} else {
            //it's safer to always update the title as we might add further information in the title like expiry etc.
            SetResult(KeePass.ExitRefreshTitle);
            //}

            RunnableOnFinish runnable;

            ActionOnFinish closeOrShowError = new ActionOnFinish((success, message) => {
                if (success)
                {
                    Finish();
                }
                else
                {
                    OnFinish.DisplayMessage(this, message);
                }
            });

            ActionOnFinish afterAddEntry = new ActionOnFinish((success, message) =>
            {
                if (success)
                {
                    _appTask.AfterAddNewEntry(this, newEntry);
                }
            }, closeOrShowError);

            if (State.IsNew)
            {
                runnable = AddEntry.GetInstance(this, App.Kp2a, newEntry, State.ParentGroup, afterAddEntry);
            }
            else
            {
                runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError);
            }
            ProgressTask pt = new ProgressTask(App.Kp2a, act, runnable);

            pt.Run();
        }