diff --git a/src/com/keepassdroid/database/PwDatabase.java b/src/com/keepassdroid/database/PwDatabase.java index c10f0c6c6..776016e6f 100644 --- a/src/com/keepassdroid/database/PwDatabase.java +++ b/src/com/keepassdroid/database/PwDatabase.java @@ -253,7 +253,6 @@ public abstract class PwDatabase { byte[] bKey; try { - // TODO: Need to make this UTF-8 in kdb4 bKey = key.getBytes(encoding); } catch (UnsupportedEncodingException e) { assert false; @@ -279,5 +278,54 @@ public abstract class PwDatabase { public abstract boolean appSettingsEnabled(); public abstract PwEncryptionAlgorithm getEncAlgorithm(); + + public void addGroupTo(PwGroup newGroup, PwGroup parent) { + // Add group to parent group + if ( parent == null ) { + parent = rootGroup; + } + + parent.childGroups.add(newGroup); + newGroup.setParent(parent); + } + + public void removeGroupFrom(PwGroup remove, PwGroup parent) { + // Remove group from parent group + parent.childGroups.remove(remove); + } + + public void addEntryTo(PwEntry newEntry, PwGroup parent) { + // Add entry to parent + parent.childEntries.add(newEntry); + } + + public void removeEntryFrom(PwEntry remove, PwGroup parent) { + // Remove entry for parent + parent.childEntries.remove(remove); + } + + public abstract PwGroupId newGroupId(); + + /** + * Determine if an id number is already in use + * + * @param id + * ID number to check for + * @return True if the ID is used, false otherwise + */ + protected boolean isGroupIdUsed(PwGroupId id) { + List groups = getGroups(); + + for (int i = 0; i < groups.size(); i++) { + PwGroup group =groups.get(i); + if (group.getId().equals(id)) { + return true; + } + } + + return false; + } + + public abstract PwGroup createGroup(); } diff --git a/src/com/keepassdroid/database/PwDatabaseV3.java b/src/com/keepassdroid/database/PwDatabaseV3.java index 5cf457174..9eed090f0 100644 --- a/src/com/keepassdroid/database/PwDatabaseV3.java +++ b/src/com/keepassdroid/database/PwDatabaseV3.java @@ -28,8 +28,6 @@ package com.keepassdroid.database; // Java import java.io.IOException; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; import java.util.List; import java.util.Random; @@ -53,7 +51,7 @@ public class PwDatabaseV3 extends PwDatabase { // all entries public List entries = new ArrayList(); // all groups - private List groups = new ArrayList(); + public List groups = new ArrayList(); // Algorithm used to encrypt the database public PwEncryptionAlgorithm algorithm; public int numKeyEncRounds; @@ -140,14 +138,6 @@ public class PwDatabaseV3 extends PwDatabase { return name; } - public void addGroup(PwGroupV3 group) { - groups.add(group); - } - - public void addEntry(PwEntry entry) { - entries.add(entry); - } - public void constructTree(PwGroupV3 currentGroup) { // I'm in root if (currentGroup == null) { @@ -185,78 +175,33 @@ public class PwDatabaseV3 extends PwDatabase { return; } - public PwGroupV3 newGroup(String name, PwGroupV3 parent) { - // Initialize group - PwGroupV3 group = new PwGroupV3(); - - group.parent = parent; - group.groupId = newGroupId(); - group.imageId = 0; - group.name = name; - - Date now = Calendar.getInstance().getTime(); - group.tCreation = new PwDate(now); - group.tLastAccess = new PwDate(now); - group.tLastMod = new PwDate(now); - group.tExpire = new PwDate(PwGroupV3.NEVER_EXPIRE); - - group.level = parent.level + 1; - - group.childEntries = new ArrayList(); - group.setGroups(new ArrayList()); - - // Add group PwDatabaseV3 and Parent - parent.childGroups.add(group); - groups.add(group); - - return group; - } - + /* public void removeGroup(PwGroupV3 group) { group.parent.childGroups.remove(group); groups.remove(group); } + */ /** * Generates an unused random group id * * @return new group id */ - private int newGroupId() { - boolean foundUnusedId = false; - int newId = 0; + @Override + public PwGroupIdV3 newGroupId() { + PwGroupIdV3 newId = new PwGroupIdV3(0); Random random = new Random(); - while (!foundUnusedId) { - newId = random.nextInt(); + while (true) { + newId = new PwGroupIdV3(random.nextInt()); - if (!isGroupIdUsed(newId)) { - foundUnusedId = true; - } + if (!isGroupIdUsed(newId)) break; } return newId; } - /** - * Determine if an id number is already in use - * - * @param id - * ID number to check for - * @return True if the ID is used, false otherwise - */ - private boolean isGroupIdUsed(int id) { - for (int i = 0; i < groups.size(); i++) { - PwGroupV3 group = (PwGroupV3) groups.get(i); - if (group.groupId == id) { - return true; - } - } - - return false; - } - public byte[] getMasterKey(String key, String keyFileName) throws InvalidKeyFileException, IOException { assert (key != null && keyFileName != null); @@ -296,4 +241,43 @@ public class PwDatabaseV3 extends PwDatabase { return true; } + @Override + public void addEntryTo(PwEntry newEntry, PwGroup parent) { + super.addEntryTo(newEntry, parent); + + // Add entry to root entries + entries.add(newEntry); + + } + + @Override + public void addGroupTo(PwGroup newGroup, PwGroup parent) { + super.addGroupTo(newGroup, parent); + + // Add group to root groups + groups.add(newGroup); + + } + + @Override + public void removeEntryFrom(PwEntry remove, PwGroup parent) { + super.removeEntryFrom(remove, parent); + + // Remove entry from root entry + entries.remove(remove); + } + + @Override + public void removeGroupFrom(PwGroup remove, PwGroup parent) { + super.removeGroupFrom(remove, parent); + + // Remove group from root entry + groups.remove(remove); + } + + @Override + public PwGroup createGroup() { + return new PwGroupV3(); + } + } diff --git a/src/com/keepassdroid/database/PwDatabaseV4.java b/src/com/keepassdroid/database/PwDatabaseV4.java index 67799bfac..6dd8da615 100644 --- a/src/com/keepassdroid/database/PwDatabaseV4.java +++ b/src/com/keepassdroid/database/PwDatabaseV4.java @@ -34,6 +34,8 @@ import com.keepassdroid.database.exception.InvalidKeyFileException; public class PwDatabaseV4 extends PwDatabase { + public static final Date DEFAULT_NOW = new Date(); + public UUID dataCipher; public PwCompressionAlgorithm compressionAlgorithm; public long numKeyEncRounds; @@ -153,4 +155,22 @@ public class PwDatabaseV4 extends PwDatabase { return PwEncryptionAlgorithm.Rjindal; } + @Override + public PwGroupIdV4 newGroupId() { + PwGroupIdV4 id = new PwGroupIdV4(UUID_ZERO); + + while (true) { + id = new PwGroupIdV4(UUID.randomUUID()); + + if (!isGroupIdUsed(id)) break; + } + + return id; + } + + @Override + public PwGroup createGroup() { + return new PwGroupV4(); + } + } diff --git a/src/com/keepassdroid/database/PwGroup.java b/src/com/keepassdroid/database/PwGroup.java index 7c0b73be1..5e04c6185 100644 --- a/src/com/keepassdroid/database/PwGroup.java +++ b/src/com/keepassdroid/database/PwGroup.java @@ -28,10 +28,13 @@ import java.util.List; public abstract class PwGroup { public List childGroups = new ArrayList(); public List childEntries = new ArrayList(); + public String name = ""; public abstract PwGroup getParent(); + public abstract void setParent(PwGroup parent); public abstract PwGroupId getId(); + public abstract void setId(PwGroupId id); public abstract String getName(); @@ -54,4 +57,9 @@ public abstract class PwGroup { Collections.sort(childEntries, new PwEntry.EntryNameComparator()); } + public void initNewGroup(String nm, PwGroupId newId) { + setId(newId); + name = nm; + } + } diff --git a/src/com/keepassdroid/database/PwGroupIdV3.java b/src/com/keepassdroid/database/PwGroupIdV3.java index a6c3cb427..1dc33284a 100644 --- a/src/com/keepassdroid/database/PwGroupIdV3.java +++ b/src/com/keepassdroid/database/PwGroupIdV3.java @@ -24,6 +24,9 @@ public class PwGroupIdV3 extends PwGroupId { return i.hashCode(); } + public int getId() { + return id; + } } diff --git a/src/com/keepassdroid/database/PwGroupIdV4.java b/src/com/keepassdroid/database/PwGroupIdV4.java index e12e781c9..20f2a6c6e 100644 --- a/src/com/keepassdroid/database/PwGroupIdV4.java +++ b/src/com/keepassdroid/database/PwGroupIdV4.java @@ -24,5 +24,9 @@ public class PwGroupIdV4 extends PwGroupId { public int hashCode() { return uuid.hashCode(); } + + public UUID getId() { + return uuid; + } } diff --git a/src/com/keepassdroid/database/PwGroupV3.java b/src/com/keepassdroid/database/PwGroupV3.java index ba53ecab4..554ced546 100644 --- a/src/com/keepassdroid/database/PwGroupV3.java +++ b/src/com/keepassdroid/database/PwGroupV3.java @@ -30,6 +30,7 @@ Copyright 2006 Bill Zwicky package com.keepassdroid.database; +import java.util.Calendar; import java.util.Date; import java.util.List; @@ -59,7 +60,6 @@ public class PwGroupV3 extends PwGroup { public int groupId; public int imageId; - public String name; public PwDate tCreation; public PwDate tLastMod; @@ -85,6 +85,12 @@ public class PwGroupV3 extends PwGroup { return new PwGroupIdV3(groupId); } + @Override + public void setId(PwGroupId id) { + PwGroupIdV3 id3 = (PwGroupIdV3) id; + groupId = id3.getId(); + } + @Override public String getName() { return name; @@ -95,4 +101,24 @@ public class PwGroupV3 extends PwGroup { return tLastMod.getJDate(); } + @Override + public void setParent(PwGroup prt) { + parent = (PwGroupV3) prt; + level = parent.level + 1; + + } + + @Override + public void initNewGroup(String nm, PwGroupId newId) { + super.initNewGroup(nm, newId); + + imageId = 0; + Date now = Calendar.getInstance().getTime(); + tCreation = new PwDate(now); + tLastAccess = new PwDate(now); + tLastMod = new PwDate(now); + tExpire = new PwDate(PwGroupV3.NEVER_EXPIRE); + + } + } diff --git a/src/com/keepassdroid/database/PwGroupV4.java b/src/com/keepassdroid/database/PwGroupV4.java index 694361751..187001605 100644 --- a/src/com/keepassdroid/database/PwGroupV4.java +++ b/src/com/keepassdroid/database/PwGroupV4.java @@ -25,22 +25,23 @@ import java.util.UUID; public class PwGroupV4 extends PwGroup implements ITimeLogger { + public static final int FOLDER_ICON = 48; + public PwGroupV4 parent = null; - public UUID uuid; - public String name; - public String notes; - public int iconId; - public UUID customIconUuid; - public boolean isExpanded; - public String defaultAutoTypeSequence; - public Boolean enableAutoType; - public Boolean enableSearching; - public UUID lastTopVisibleEntry; + public UUID uuid = PwDatabaseV4.UUID_ZERO; + public String notes = ""; + public int iconId = FOLDER_ICON; + public UUID customIconUuid = PwDatabaseV4.UUID_ZERO; + public boolean isExpanded = true; + public String defaultAutoTypeSequence = ""; + public Boolean enableAutoType = null; + public Boolean enableSearching = null; + public UUID lastTopVisibleEntry = PwDatabaseV4.UUID_ZERO; private Date parentGroupLastMod; - private Date creation; - private Date lastMod; - private Date lastAccess; - private Date expireDate; + private Date creation = PwDatabaseV4.DEFAULT_NOW; + private Date lastMod = PwDatabaseV4.DEFAULT_NOW; + private Date lastAccess = PwDatabaseV4.DEFAULT_NOW; + private Date expireDate = PwDatabaseV4.DEFAULT_NOW; private boolean expires = false; private long usageCount = 0; @@ -109,6 +110,12 @@ public class PwGroupV4 extends PwGroup implements ITimeLogger { return new PwGroupIdV4(uuid); } + @Override + public void setId(PwGroupId id) { + PwGroupIdV4 id4 = (PwGroupIdV4) id; + uuid = id4.getId(); + } + @Override public String getName() { return name; @@ -190,4 +197,10 @@ public class PwGroupV4 extends PwGroup implements ITimeLogger { expires = exp; } + @Override + public void setParent(PwGroup prt) { + parent = (PwGroupV4) prt; + + } + } diff --git a/src/com/keepassdroid/database/edit/AddEntry.java b/src/com/keepassdroid/database/edit/AddEntry.java index 35ffaf341..611538840 100644 --- a/src/com/keepassdroid/database/edit/AddEntry.java +++ b/src/com/keepassdroid/database/edit/AddEntry.java @@ -21,21 +21,15 @@ package com.keepassdroid.database.edit; import com.keepassdroid.Database; import com.keepassdroid.database.PwEntry; -import com.keepassdroid.database.PwEntryV3; import com.keepassdroid.database.PwGroup; import com.keepassdroid.search.SearchDbHelper; -public abstract class AddEntry extends RunnableOnFinish { +public class AddEntry extends RunnableOnFinish { protected Database mDb; private PwEntry mEntry; public static AddEntry getInstance(Database db, PwEntry entry, OnFinish finish) { - if ( entry instanceof PwEntryV3 ) { - return new AddEntryV3(db, (PwEntry) entry, finish); - } else { - // TODO: Implement me - throw new RuntimeException("Not implemented yet."); - } + return new AddEntry(db, entry, finish); } protected AddEntry(Database db, PwEntry entry, OnFinish finish) { @@ -47,11 +41,9 @@ public abstract class AddEntry extends RunnableOnFinish { mFinish = new AfterAdd(mFinish); } - public abstract void addEntry(); - @Override public void run() { - addEntry(); + mDb.pm.addEntryTo(mEntry, mEntry.getParent()); // Commit to disk SaveDB save = new SaveDB(mDb, mFinish); @@ -72,8 +64,7 @@ public abstract class AddEntry extends RunnableOnFinish { // Mark parent group dirty mDb.dirty.add(parent); - - + // Add entry to global mDb.entries.put(mEntry.getUUID(), mEntry); @@ -85,12 +76,7 @@ public abstract class AddEntry extends RunnableOnFinish { helper.close(); } } else { - // Remove from group - mEntry.getParent().childEntries.remove(mEntry); - - // Remove from manager - mDb.pm.getEntries().remove(mEntry); - + mDb.pm.removeEntryFrom(mEntry, mEntry.getParent()); } super.run(); diff --git a/src/com/keepassdroid/database/edit/AddEntryV3.java b/src/com/keepassdroid/database/edit/AddEntryV3.java deleted file mode 100644 index b89a2b1aa..000000000 --- a/src/com/keepassdroid/database/edit/AddEntryV3.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2010 Brian Pellin. - * - * This file is part of KeePassDroid. - * - * KeePassDroid is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * KeePassDroid is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with KeePassDroid. If not, see . - * - */ -package com.keepassdroid.database.edit; - -import com.keepassdroid.Database; -import com.keepassdroid.database.PwDatabase; -import com.keepassdroid.database.PwEntry; -import com.keepassdroid.database.PwGroupV3; - -class AddEntryV3 extends AddEntry { - - private PwEntry mEntry; - - protected AddEntryV3(Database db, PwEntry entry, OnFinish finish) { - super(db, entry, finish); - - mEntry = entry; - } - - - public void addEntry() { - PwGroupV3 parent = (PwGroupV3) mEntry.getParent(); - - // Add entry to group - parent.childEntries.add(mEntry); - - // Add entry to PwDatabaseV3 - PwDatabase pm = (PwDatabase) mDb.pm; - pm.getEntries().add(mEntry); - - // Sort entries - parent.sortEntriesByName(); - - } - -} diff --git a/src/com/keepassdroid/database/edit/AddGroup.java b/src/com/keepassdroid/database/edit/AddGroup.java index 48b1e20e9..dee06a1ef 100644 --- a/src/com/keepassdroid/database/edit/AddGroup.java +++ b/src/com/keepassdroid/database/edit/AddGroup.java @@ -20,30 +20,73 @@ package com.keepassdroid.database.edit; import com.keepassdroid.Database; +import com.keepassdroid.database.PwDatabase; +import com.keepassdroid.database.PwDatabaseV3; import com.keepassdroid.database.PwGroup; -import com.keepassdroid.database.PwGroupV3; -public abstract class AddGroup extends RunnableOnFinish { +public class AddGroup extends RunnableOnFinish { protected Database mDb; + private String mName; + private PwGroup mGroup; + private PwGroup mParent; protected boolean mDontSave; + public static AddGroup getInstance(Database db, String name, PwGroup parent, OnFinish finish, boolean dontSave) { - if ( parent instanceof PwGroupV3 ) { - return new AddGroupV3(db, name, (PwGroupV3) parent, finish, dontSave); - } else { - throw new RuntimeException("Not yet implemented"); - } + return new AddGroup(db, name, parent, finish, dontSave); } - protected AddGroup(Database db, OnFinish finish, boolean dontSave) { + + private AddGroup(Database db, String name, PwGroup parent, OnFinish finish, boolean dontSave) { super(finish); mDb = db; + mName = name; + mParent = parent; mDontSave = dontSave; + mFinish = new AfterAdd(mFinish); } + + @Override + public void run() { + PwDatabase pm = (PwDatabaseV3) mDb.pm; + + // Generate new group + mGroup = pm.createGroup(); + mGroup.initNewGroup(mName, pm.newGroupId()); + pm.addGroupTo(mGroup, mParent); + + //mParent.sortGroupsByName(); + + // Commit to disk + SaveDB save = new SaveDB(mDb, mFinish, mDontSave); + save.run(); + } + + private class AfterAdd extends OnFinish { - public abstract void run(); + public AfterAdd(OnFinish finish) { + super(finish); + } + + @Override + public void run() { + + if ( mSuccess ) { + // Mark parent group dirty + mDb.dirty.add(mParent); + + // Add group to global list + mDb.groups.put(mGroup.getId(), mGroup); + } else { + mDb.pm.removeGroupFrom(mGroup, mParent); + } + + super.run(); + } + + } } diff --git a/src/com/keepassdroid/database/edit/AddGroupV3.java b/src/com/keepassdroid/database/edit/AddGroupV3.java deleted file mode 100644 index d77611edc..000000000 --- a/src/com/keepassdroid/database/edit/AddGroupV3.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.keepassdroid.database.edit; - -import com.keepassdroid.Database; -import com.keepassdroid.database.PwDatabaseV3; -import com.keepassdroid.database.PwGroupV3; - -public class AddGroupV3 extends AddGroup { - private Database mDb; - private String mName; - private PwGroupV3 mParent; - private PwGroupV3 mGroup; - private boolean mDontSave; - - protected AddGroupV3(Database db, String name, PwGroupV3 parent, OnFinish finish, boolean dontSave) { - super(db, finish, dontSave); - - mDb = db; - mName = name; - mParent = parent; - mDontSave = dontSave; - - mFinish = new AfterAdd(mFinish); - } - - @Override - public void run() { - PwDatabaseV3 pm = (PwDatabaseV3) mDb.pm; - - // Generate new group - mGroup = pm.newGroup(mName, mParent); - - mParent.sortGroupsByName(); - - // Commit to disk - SaveDB save = new SaveDB(mDb, mFinish, mDontSave); - save.run(); - } - - private class AfterAdd extends OnFinish { - - public AfterAdd(OnFinish finish) { - super(finish); - } - - @Override - public void run() { - - if ( mSuccess ) { - // Mark parent group dirty - mDb.dirty.add(mParent); - - // Add group to global list - mDb.groups.put(mGroup.getId(), mGroup); - } else { - PwDatabaseV3 pm = (PwDatabaseV3) mDb.pm; - pm.removeGroup(mGroup); - } - - super.run(); - } - - } - -} diff --git a/src/com/keepassdroid/database/load/ImporterV3.java b/src/com/keepassdroid/database/load/ImporterV3.java index 9988f631f..d456bdb79 100644 --- a/src/com/keepassdroid/database/load/ImporterV3.java +++ b/src/com/keepassdroid/database/load/ImporterV3.java @@ -232,7 +232,7 @@ public class ImporterV3 extends Importer { if( fieldType == 0xFFFF ) { // End-Group record. Save group and count it. - newManager.addGroup( newGrp ); + newManager.groups.add(newGrp); newGrp = new PwGroupV3(); i++; } @@ -250,7 +250,7 @@ public class ImporterV3 extends Importer { if( fieldType == 0xFFFF ) { // End-Group record. Save group and count it. - newManager.addEntry( newEnt ); + newManager.entries.add(newEnt); newEnt = new PwEntryV3(); i++; }