Esempio n. 1
0
        public void SynchronizeDatabase(Action runAfterSuccess)
        {
            var filestorage = App.Kp2a.GetFileStorage(App.Kp2a.CurrentDb.Ioc);
            RunnableOnFinish task;
            OnFinish         onFinish = new ActionOnFinish(_activity, (success, message, activity) =>
            {
                if (!String.IsNullOrEmpty(message))
                {
                    Toast.MakeText(activity, message, ToastLength.Long).Show();
                }

                // Tell the adapter to refresh it's list
                BaseAdapter adapter = (activity as GroupBaseActivity)?.ListAdapter;
                adapter?.NotifyDataSetChanged();

                if (App.Kp2a.CurrentDb?.OtpAuxFileIoc != null)
                {
                    var task2           = new SyncOtpAuxFile(_activity, App.Kp2a.CurrentDb.OtpAuxFileIoc);
                    task2.OnFinishToRun = new ActionOnFinish(_activity, (b, s, activeActivity) =>
                    {
                        runAfterSuccess();
                    });
                    new ProgressTask(App.Kp2a, activity, task2).Run(true);
                }
                else
                {
                    runAfterSuccess();
                }
            });

            if (filestorage is CachingFileStorage)
            {
                task = new SynchronizeCachedDatabase(_activity, App.Kp2a, onFinish);
            }
            else
            {
                task = new CheckDatabaseForChanges(_activity, App.Kp2a, onFinish);
            }



            var progressTask = new ProgressTask(App.Kp2a, _activity, task);

            progressTask.Run();
        }
Esempio n. 2
0
        internal void AddUrlToEntry(string url, Action finishAction)
        {
            PwEntry initialEntry = Entry.CloneDeep();

            PwEntry newEntry = Entry;

            newEntry.History = newEntry.History.CloneDeep();
            newEntry.CreateBackup(null);

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

            //if there is no URL in the entry, set that field. If it's already in use, use an additional (not existing) field
            if (String.IsNullOrEmpty(newEntry.Strings.ReadSafe(PwDefs.UrlField)))
            {
                newEntry.Strings.Set(PwDefs.UrlField, new ProtectedString(false, url));
            }
            else
            {
                int c = 1;
                while (newEntry.Strings.Get("KP2A_URL_" + c) != null)
                {
                    c++;
                }

                newEntry.Strings.Set("KP2A_URL_" + c, new ProtectedString(false, url));
            }

            //save the entry:

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


            RunnableOnFinish runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError);

            ProgressTask pt = new ProgressTask(App.Kp2a, this, runnable);

            pt.Run();
        }
Esempio n. 3
0
        public override void Run()
        {
            StatusLogger.UpdateMessage(StatusMessage);

            List <PwGroup> touchedGroups            = new List <PwGroup>();
            List <PwGroup> permanentlyDeletedGroups = new List <PwGroup>();

            Android.Util.Log.Debug("KP2A", "Calling PerformDelete..");
            PerformDelete(touchedGroups, permanentlyDeletedGroups);

            _onFinishToRun = new ActionOnFinish(ActiveActivity, (success, message, activity) =>
            {
                if (success)
                {
                    foreach (var g in touchedGroups)
                    {
                        App.DirtyGroups.Add(g);
                    }
                    foreach (var g in permanentlyDeletedGroups)
                    {
                        //remove groups from global lists if present there
                        App.DirtyGroups.Remove(g);
                        Db.GroupsById.Remove(g.Uuid);
                        Db.Elements.Remove(g);
                    }
                }
                else
                {
                    // Let's not bother recovering from a failure to save.  It is too much work.
                    App.Lock(false);
                }
            }, OnFinishToRun);

            // Commit database
            SaveDb save = new SaveDb(Ctx, App, Db, OnFinishToRun, false);

            save.ShowDatabaseIocInStatus = ShowDatabaseIocInStatus;

            save.SetStatusLogger(StatusLogger);
            save.Run();
        }
Esempio n. 4
0
        private void Synchronize()
        {
            var filestorage = App.Kp2a.GetFileStorage(App.Kp2a.GetDb().Ioc);
            RunnableOnFinish task;
            OnFinish         onFinish = new ActionOnFinish(this, (success, message, activity) =>
            {
                if (!String.IsNullOrEmpty(message))
                {
                    Toast.MakeText(activity, message, ToastLength.Long).Show();
                }

                // Tell the adapter to refresh it's list
                BaseAdapter adapter = (BaseAdapter)((GroupBaseActivity)activity)?.ListAdapter;
                adapter?.NotifyDataSetChanged();

                if (App.Kp2a.GetDb().OtpAuxFileIoc != null)
                {
                    var task2 = new SyncOtpAuxFile(this, App.Kp2a.GetDb().OtpAuxFileIoc);
                    new ProgressTask(App.Kp2a, activity, task2).Run(true);
                }
            });

            if (filestorage is CachingFileStorage)
            {
                task = new SynchronizeCachedDatabase(this, App.Kp2a, onFinish);
            }
            else
            {
                task = new CheckDatabaseForChanges(this, App.Kp2a, onFinish);
            }



            var progressTask = new ProgressTask(App.Kp2a, this, task);

            progressTask.Run();
        }
Esempio n. 5
0
        private void Synchronize()
        {
            var filestorage = App.Kp2a.GetFileStorage(App.Kp2a.GetDb().Ioc);
            RunnableOnFinish task;
            OnFinish onFinish = new ActionOnFinish((success, message) =>
            {
                if (!String.IsNullOrEmpty(message))
                    Toast.MakeText(this, message, ToastLength.Long).Show();

                // Tell the adapter to refresh it's list
                BaseAdapter adapter = (BaseAdapter)ListAdapter;
                adapter.NotifyDataSetChanged();

                if (App.Kp2a.GetDb().OtpAuxFileIoc != null)
                {
                    var task2 = new SyncOtpAuxFile(App.Kp2a.GetDb().OtpAuxFileIoc);
                    new ProgressTask(App.Kp2a, this, task2).Run();
                }
            });

            if (filestorage is CachingFileStorage)
            {

                task = new SynchronizeCachedDatabase(this, App.Kp2a, onFinish);
            }
            else
            {

                task = new CheckDatabaseForChanges(this, App.Kp2a, onFinish);
            }

            var progressTask = new ProgressTask(App.Kp2a, this, task);
            progressTask.Run();
        }
Esempio n. 6
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();
        }
Esempio n. 7
0
        public override void Run()
        {
            StatusLogger.UpdateMessage(UiStringKey.DeletingEntry);
            PwDatabase pd = Db.KpDatabase;

            PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);

            bool bUpdateGroupList = false;
            DateTime dtNow = DateTime.Now;
            PwEntry pe = _entry;
            PwGroup pgParent = pe.ParentGroup;
            if(pgParent != null)
            {
                pgParent.Entries.Remove(pe);
                //TODO check if RecycleBin is deleted
                //TODO no recycle bin in KDB

                if ((DeletePermanently) || (!CanRecycle))
                {
                    PwDeletedObject pdo = new PwDeletedObject(pe.Uuid, dtNow);
                    pd.DeletedObjects.Add(pdo);

                    _onFinishToRun = new ActionOnFinish((success, message) =>
                        {
                            if (success)
                            {
                                // Mark parent dirty
                                Db.Dirty.Add(pgParent);
                            }
                            else
                            {
                                // Let's not bother recovering from a failure to save a deleted entry.  It is too much work.
                                App.LockDatabase(false);
                            }
                        }, OnFinishToRun);
                }
                else // Recycle
                {
                    EnsureRecycleBinExists(ref pgRecycleBin, ref bUpdateGroupList);

                    pgRecycleBin.AddEntry(pe, true, true);
                    pe.Touch(false);

                    _onFinishToRun = new ActionOnFinish( (success, message) =>
                                                 {
                        if ( success ) {
                            // Mark previous parent dirty
                            Db.Dirty.Add(pgParent);
                            // Mark new parent dirty
                            Db.Dirty.Add(pgRecycleBin);
                            // mark root dirty if recycle bin was created
                            if (bUpdateGroupList)
                                Db.Dirty.Add(Db.Root);
                        } else {
                            // Let's not bother recovering from a failure to save a deleted entry.  It is too much work.
                            App.LockDatabase(false);
                        }

                    }, OnFinishToRun);
                }
            }

            // Commit database
            SaveDb save = new SaveDb(Ctx, App, OnFinishToRun, false);
            save.SetStatusLogger(StatusLogger);
            save.Run();
        }
Esempio n. 8
0
        internal void AddUrlToEntry(string url, Action finishAction)
        {
            PwEntry initialEntry = Entry.CloneDeep();

            PwEntry newEntry = Entry;
            newEntry.History = newEntry.History.CloneDeep();
            newEntry.CreateBackup(null);

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

            //if there is no URL in the entry, set that field. If it's already in use, use an additional (not existing) field
            if (String.IsNullOrEmpty(newEntry.Strings.ReadSafe(PwDefs.UrlField)))
            {
                newEntry.Strings.Set(PwDefs.UrlField, new ProtectedString(false, url));
            }
            else
            {
                int c = 1;
                while (newEntry.Strings.Get("KP2A_URL_" + c) != null)
                {
                    c++;
                }

                newEntry.Strings.Set("KP2A_URL_" + c, new ProtectedString(false, url));
            }

            //save the entry:

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

            RunnableOnFinish runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError);

            ProgressTask pt = new ProgressTask(App.Kp2a, this, runnable);
            pt.Run();
        }
Esempio n. 9
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 == false) {
                if (State.IsNew) {
                    newEntry.IconId = PwIcon.Key;
                } else {
                    // Keep previous icon, if no new one was selected
                }
            }
            else {
                newEntry.IconId = State.SelectedIconId;
                newEntry.CustomIconUuid = State.SelectedCustomIconId;
            }
            /* 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();
        }