2
0

Update directory to mpw.d and fix issue that caused only one user to be visible in the drop-down.

This commit is contained in:
Maarten Billemont 2014-12-28 14:46:20 -05:00
parent 9f10bcdec4
commit c03199f7e5
11 changed files with 144 additions and 28 deletions

View File

@ -1,12 +1,17 @@
package com.lyndir.masterpassword.model; package com.lyndir.masterpassword.model;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import java.util.Objects;
/** /**
* @author lhunath, 2014-08-20 * @author lhunath, 2014-08-20
*/ */
public class User { public class User {
private String name; private String name;
private Avatar avatar; private Avatar avatar;
public User(final String name, final Avatar avatar) { public User(final String name, final Avatar avatar) {
this.name = name; this.name = name;
@ -20,4 +25,19 @@ public class User {
public Avatar getAvatar() { public Avatar getAvatar() {
return avatar; return avatar;
} }
@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof User && name.equals( ((User) obj).name );
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return strf( "{User: %s}", name );
}
} }

View File

@ -48,4 +48,9 @@ public class ModelSite extends Site {
MPUserFileManager.get().save(); MPUserFileManager.get().save();
} }
} }
public void use() {
model.updateLastUsed();
MPUserFileManager.get().save();
}
} }

View File

@ -4,6 +4,7 @@ import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import com.lyndir.lhunath.opal.system.util.ObjectUtils;
import com.lyndir.masterpassword.MasterKey; import com.lyndir.masterpassword.MasterKey;
import com.lyndir.masterpassword.model.*; import com.lyndir.masterpassword.model.*;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -76,7 +77,8 @@ public class ModelUser extends User {
@Override @Override
public void addSite(final Site site) { public void addSite(final Site site) {
model.addSite( new MPSite( site.getSiteName(), site.getSiteType(), site.getSiteCounter() ) ); model.addSite( new MPSite( model, site.getSiteName(), site.getSiteType(), site.getSiteCounter() ) );
model.updateLastUsed();
MPUserFileManager.get().save(); MPUserFileManager.get().save();
} }

View File

@ -3,7 +3,9 @@ package com.lyndir.masterpassword.gui;
import static com.lyndir.lhunath.opal.system.util.StringUtils.*; import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.lyndir.masterpassword.MasterKey; import com.lyndir.masterpassword.MasterKey;
import com.lyndir.masterpassword.model.MPUser;
import java.security.KeyException; import java.security.KeyException;
import java.util.Objects;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -38,17 +40,22 @@ public abstract class User {
return key; return key;
} }
public abstract Iterable<Site> findSitesByName(final String siteName);
public abstract void addSite(final Site site);
@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof User && Objects.equals( getFullName(), ((User) obj).getFullName() );
}
@Override @Override
public int hashCode() { public int hashCode() {
return getFullName().hashCode(); return Objects.hashCode( getFullName() );
} }
@Override @Override
public String toString() { public String toString() {
return getFullName(); return getFullName();
} }
public abstract Iterable<Site> findSitesByName(final String siteName);
public abstract void addSite(final Site site);
} }

View File

@ -27,6 +27,13 @@
<version>GIT-SNAPSHOT</version> <version>GIT-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.0-rc1</version>
<scope>provided</scope>
</dependency>
<!-- TESTING --> <!-- TESTING -->
<dependency> <dependency>
<groupId>org.testng</groupId> <groupId>org.testng</groupId>

View File

@ -4,6 +4,7 @@ import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf; import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import com.lyndir.masterpassword.*; import com.lyndir.masterpassword.*;
import java.util.Objects;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.Instant; import org.joda.time.Instant;
@ -17,7 +18,8 @@ public class MPSite {
public static final MPSiteType DEFAULT_TYPE = MPSiteType.GeneratedLong; public static final MPSiteType DEFAULT_TYPE = MPSiteType.GeneratedLong;
public static final int DEFAULT_COUNTER = 1; public static final int DEFAULT_COUNTER = 1;
private int mpVersion; private final MPUser user;
private int mpVersion;
private Instant lastUsed; private Instant lastUsed;
private String siteName; private String siteName;
private MPSiteType siteType; private MPSiteType siteType;
@ -25,11 +27,12 @@ public class MPSite {
private int uses; private int uses;
private String loginName; private String loginName;
public MPSite(final String siteName) { public MPSite(final MPUser user, final String siteName) {
this( siteName, DEFAULT_TYPE, DEFAULT_COUNTER ); this( user, siteName, DEFAULT_TYPE, DEFAULT_COUNTER );
} }
public MPSite(final String siteName, final MPSiteType siteType, final int siteCounter) { public MPSite(final MPUser user, final String siteName, final MPSiteType siteType, final int siteCounter) {
this.user = user;
this.mpVersion = MasterKey.ALGORITHM; this.mpVersion = MasterKey.ALGORITHM;
this.lastUsed = new Instant(); this.lastUsed = new Instant();
this.siteName = siteName; this.siteName = siteName;
@ -37,8 +40,9 @@ public class MPSite {
this.siteCounter = siteCounter; this.siteCounter = siteCounter;
} }
protected MPSite(final int mpVersion, final Instant lastUsed, final String siteName, final MPSiteType siteType, final int siteCounter, protected MPSite(final MPUser user, final int mpVersion, final Instant lastUsed, final String siteName, final MPSiteType siteType, final int siteCounter,
final int uses, final String loginName, final String importContent) { final int uses, final String loginName, final String importContent) {
this.user = user;
this.mpVersion = mpVersion; this.mpVersion = mpVersion;
this.lastUsed = lastUsed; this.lastUsed = lastUsed;
this.siteName = siteName; this.siteName = siteName;
@ -56,6 +60,10 @@ public class MPSite {
return masterKey.encode( siteName, siteType, siteCounter, variant, context ); return masterKey.encode( siteName, siteType, siteCounter, variant, context );
} }
public MPUser getUser() {
return user;
}
@Nullable @Nullable
protected String exportContent() { protected String exportContent() {
return null; return null;
@ -73,8 +81,9 @@ public class MPSite {
return lastUsed; return lastUsed;
} }
public void setLastUsed(final Instant lastUsed) { public void updateLastUsed() {
this.lastUsed = lastUsed; lastUsed = new Instant();
user.updateLastUsed();
} }
public String getSiteName() { public String getSiteName() {
@ -116,4 +125,19 @@ public class MPSite {
public void setLoginName(final String loginName) { public void setLoginName(final String loginName) {
this.loginName = loginName; this.loginName = loginName;
} }
@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof MPSite && Objects.equals( siteName, ((MPSite) obj).siteName );
}
@Override
public int hashCode() {
return Objects.hashCode( siteName );
}
@Override
public String toString() {
return strf( "{MPSite: %s}", siteName );
}
} }

View File

@ -1,5 +1,10 @@
package com.lyndir.masterpassword.model; package com.lyndir.masterpassword.model;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import java.util.Objects;
/** /**
* @author lhunath, 14-12-07 * @author lhunath, 14-12-07
*/ */
@ -14,4 +19,19 @@ public class MPSiteResult {
public MPSite getSite() { public MPSite getSite() {
return site; return site;
} }
@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof MPSiteResult && Objects.equals( site, ((MPSiteResult) obj).site );
}
@Override
public int hashCode() {
return Objects.hashCode( site );
}
@Override
public String toString() {
return strf( "{MPSiteResult: %s}", site );
}
} }

View File

@ -4,7 +4,6 @@ import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.io.CharStreams; import com.google.common.io.CharStreams;
import com.lyndir.lhunath.opal.system.CodeUtils; import com.lyndir.lhunath.opal.system.CodeUtils;
import com.lyndir.lhunath.opal.system.logging.Logger; import com.lyndir.lhunath.opal.system.logging.Logger;
@ -123,7 +122,8 @@ public class MPSiteUnmarshaller {
MPSite site; MPSite site;
switch (importFormat) { switch (importFormat) {
case 0: case 0:
site = new MPSite( ConversionUtils.toIntegerNN( siteMatcher.group( 4 ).replace( ":", "" ) ), // site = new MPSite( user, //
ConversionUtils.toIntegerNN( siteMatcher.group( 4 ).replace( ":", "" ) ), //
rfc3339.parseDateTime( siteMatcher.group( 1 ) ).toInstant(), // rfc3339.parseDateTime( siteMatcher.group( 1 ) ).toInstant(), //
siteMatcher.group( 5 ), // siteMatcher.group( 5 ), //
MPSiteType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ), MPSiteType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),
@ -134,7 +134,8 @@ public class MPSiteUnmarshaller {
break; break;
case 1: case 1:
site = new MPSite( ConversionUtils.toIntegerNN( siteMatcher.group( 4 ).replace( ":", "" ) ), // site = new MPSite( user, //
ConversionUtils.toIntegerNN( siteMatcher.group( 4 ).replace( ":", "" ) ), //
rfc3339.parseDateTime( siteMatcher.group( 1 ) ).toInstant(), // rfc3339.parseDateTime( siteMatcher.group( 1 ) ).toInstant(), //
siteMatcher.group( 7 ), // siteMatcher.group( 7 ), //
MPSiteType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ), MPSiteType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),

View File

@ -1,5 +1,7 @@
package com.lyndir.masterpassword.model; package com.lyndir.masterpassword.model;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.lyndir.lhunath.opal.system.CodeUtils; import com.lyndir.lhunath.opal.system.CodeUtils;
@ -11,7 +13,7 @@ import org.joda.time.*;
/** /**
* @author lhunath, 14-12-07 * @author lhunath, 14-12-07
*/ */
public class MPUser { public class MPUser implements Comparable<MPUser> {
private final String fullName; private final String fullName;
private final Collection<MPSite> sites = Sets.newHashSet(); private final Collection<MPSite> sites = Sets.newHashSet();
@ -98,4 +100,28 @@ public class MPUser {
public Iterable<MPSite> getSites() { public Iterable<MPSite> getSites() {
return sites; return sites;
} }
@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof MPUser && Objects.equals( fullName, ((MPUser) obj).fullName );
}
@Override
public int hashCode() {
return Objects.hashCode( fullName );
}
@Override
public String toString() {
return strf( "{MPUser: %s}", fullName );
}
@Override
public int compareTo(final MPUser o) {
int comparison = lastUsed.compareTo( o.lastUsed );
if (comparison == 0)
comparison = fullName.compareTo( o.fullName );
return comparison;
}
} }

View File

@ -15,7 +15,17 @@ public class MPUserFileManager extends MPUserManager {
@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
private static final Logger logger = Logger.get( MPUserFileManager.class ); private static final Logger logger = Logger.get( MPUserFileManager.class );
private static final MPUserFileManager instance = create( new File( System.getProperty( "user.home" ), ".mpwrc" ) ); private static final File mpwd = new File( System.getProperty( "user.home" ), ".mpw.d" );
private static final MPUserFileManager instance;
static {
File mpwrc = new File( System.getProperty( "user.home" ), ".mpwrc" );
if (mpwrc.exists() && !mpwd.exists())
if (!mpwrc.renameTo( mpwd ))
logger.err( "Couldn't migrate: %s -> %s", mpwrc, mpwd );
instance = create( mpwd );
}
private final File userFilesDirectory; private final File userFilesDirectory;

View File

@ -1,7 +1,6 @@
package com.lyndir.masterpassword.model; package com.lyndir.masterpassword.model;
import com.google.common.collect.FluentIterable; import com.google.common.collect.*;
import com.google.common.collect.Maps;
import java.util.*; import java.util.*;
@ -23,12 +22,7 @@ public abstract class MPUserManager {
} }
public SortedSet<MPUser> getUsers() { public SortedSet<MPUser> getUsers() {
return FluentIterable.from( usersByName.values() ).toSortedSet( new Comparator<MPUser>() { return FluentIterable.from( usersByName.values() ).toSortedSet( Ordering.natural() );
@Override
public int compare(final MPUser user1, final MPUser user2) {
return user1.getLastUsed().compareTo( user2.getLastUsed() );
}
} );
} }
public void addUser(final MPUser user) { public void addUser(final MPUser user) {