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,5 +1,10 @@
package com.lyndir.masterpassword.model;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import java.util.Objects;
/**
* @author lhunath, 2014-08-20
*/
@ -20,4 +25,19 @@ public class User {
public Avatar getAvatar() {
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();
}
}
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.collect.FluentIterable;
import com.lyndir.lhunath.opal.system.util.ObjectUtils;
import com.lyndir.masterpassword.MasterKey;
import com.lyndir.masterpassword.model.*;
import javax.annotation.Nullable;
@ -76,7 +77,8 @@ public class ModelUser extends User {
@Override
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();
}

View File

@ -3,7 +3,9 @@ package com.lyndir.masterpassword.gui;
import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.lyndir.masterpassword.MasterKey;
import com.lyndir.masterpassword.model.MPUser;
import java.security.KeyException;
import java.util.Objects;
import javax.annotation.Nonnull;
@ -38,17 +40,22 @@ public abstract class User {
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
public int hashCode() {
return getFullName().hashCode();
return Objects.hashCode( getFullName() );
}
@Override
public String toString() {
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>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.0-rc1</version>
<scope>provided</scope>
</dependency>
<!-- TESTING -->
<dependency>
<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 com.lyndir.masterpassword.*;
import java.util.Objects;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
import org.joda.time.Instant;
@ -17,6 +18,7 @@ public class MPSite {
public static final MPSiteType DEFAULT_TYPE = MPSiteType.GeneratedLong;
public static final int DEFAULT_COUNTER = 1;
private final MPUser user;
private int mpVersion;
private Instant lastUsed;
private String siteName;
@ -25,11 +27,12 @@ public class MPSite {
private int uses;
private String loginName;
public MPSite(final String siteName) {
this( siteName, DEFAULT_TYPE, DEFAULT_COUNTER );
public MPSite(final MPUser user, final String siteName) {
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.lastUsed = new Instant();
this.siteName = siteName;
@ -37,8 +40,9 @@ public class MPSite {
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) {
this.user = user;
this.mpVersion = mpVersion;
this.lastUsed = lastUsed;
this.siteName = siteName;
@ -56,6 +60,10 @@ public class MPSite {
return masterKey.encode( siteName, siteType, siteCounter, variant, context );
}
public MPUser getUser() {
return user;
}
@Nullable
protected String exportContent() {
return null;
@ -73,8 +81,9 @@ public class MPSite {
return lastUsed;
}
public void setLastUsed(final Instant lastUsed) {
this.lastUsed = lastUsed;
public void updateLastUsed() {
lastUsed = new Instant();
user.updateLastUsed();
}
public String getSiteName() {
@ -116,4 +125,19 @@ public class MPSite {
public void setLoginName(final String 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;
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
import java.util.Objects;
/**
* @author lhunath, 14-12-07
*/
@ -14,4 +19,19 @@ public class MPSiteResult {
public MPSite getSite() {
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.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.io.CharStreams;
import com.lyndir.lhunath.opal.system.CodeUtils;
import com.lyndir.lhunath.opal.system.logging.Logger;
@ -123,7 +122,8 @@ public class MPSiteUnmarshaller {
MPSite site;
switch (importFormat) {
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(), //
siteMatcher.group( 5 ), //
MPSiteType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),
@ -134,7 +134,8 @@ public class MPSiteUnmarshaller {
break;
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(), //
siteMatcher.group( 7 ), //
MPSiteType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),

View File

@ -1,5 +1,7 @@
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.Sets;
import com.lyndir.lhunath.opal.system.CodeUtils;
@ -11,7 +13,7 @@ import org.joda.time.*;
/**
* @author lhunath, 14-12-07
*/
public class MPUser {
public class MPUser implements Comparable<MPUser> {
private final String fullName;
private final Collection<MPSite> sites = Sets.newHashSet();
@ -98,4 +100,28 @@ public class MPUser {
public Iterable<MPSite> getSites() {
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")
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;

View File

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