From 978b758079499f71a748fdc3e34b0588ead95ffb Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sat, 28 Jul 2018 19:56:20 -0400 Subject: [PATCH] New user fixes. --- .../gui/util/CollectionListModel.java | 2 +- .../gui/view/UserContentPanel.java | 5 +- .../model/impl/MPBasicQuestion.java | 2 +- .../model/impl/MPBasicSite.java | 2 +- .../model/impl/MPBasicUser.java | 2 +- .../model/impl/MPFlatUnmarshaller.java | 153 +++++++++--------- .../masterpassword/model/impl/MPJSONFile.java | 13 +- .../model/impl/MPJSONUnmarshaller.java | 6 +- 8 files changed, 93 insertions(+), 92 deletions(-) diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java index cc357770..17c67416 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java @@ -90,7 +90,7 @@ public class CollectionListModel extends AbstractListModel @Override @SuppressWarnings({ "unchecked", "SuspiciousMethodCalls" }) public synchronized void setSelectedItem(@Nullable final Object newSelectedItem) { - if (!Objects.equals( selectedItem, newSelectedItem ) && model.contains( newSelectedItem )) { + if (!Objects.equals( selectedItem, newSelectedItem )) { selectedItem = (E) newSelectedItem; fireContentsChanged( this, -1, -1 ); diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java index 5fecd091..795bb580 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java @@ -431,8 +431,9 @@ public class UserContentPanel extends JPanel implements FilesPanel.Listener, MPU Toolkit.getDefaultToolkit().getSystemClipboard().setContents( clipboardContents, null ); Res.ui( () -> { - Window window = SwingUtilities.windowForComponent( this ); - window.dispatchEvent( new WindowEvent( window, WindowEvent.WINDOW_CLOSING ) ); + Window window = SwingUtilities.windowForComponent( UserContentPanel.this ); + if (window != null) + window.dispatchEvent( new WindowEvent( window, WindowEvent.WINDOW_CLOSING ) ); } ); } ); } diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java index 430f6ab7..d22f576d 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java @@ -88,7 +88,7 @@ public abstract class MPBasicQuestion extends Changeable implements MPQuestion { @Override public boolean equals(final Object obj) { - return (this == obj) || ((obj instanceof MPQuestion) && Objects.equals( getKeyword(), ((MPQuestion) obj).getKeyword() )); + return this == obj; } @Override diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java index f79d4c6d..f4face78 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java @@ -204,7 +204,7 @@ public abstract class MPBasicSite, Q extends MPQuestion> ext @Override public boolean equals(final Object obj) { - return (this == obj) || ((obj instanceof MPSite) && Objects.equals( getSiteName(), ((MPSite) obj).getSiteName() )); + return obj == this; } @Override diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java index 3e7642a9..2c73f915 100755 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java @@ -226,7 +226,7 @@ public abstract class MPBasicUser> extends Changeabl @Override public boolean equals(final Object obj) { - return (this == obj) || ((obj instanceof MPUser) && Objects.equals( getFullName(), ((MPUser) obj).getFullName() )); + return this == obj; } @Override diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java index 976f5858..68bef9ff 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java @@ -104,89 +104,90 @@ public class MPFlatUnmarshaller implements MPUnmarshaller { throws IOException, MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException, MPAlgorithmException { user.ignoreChanges(); - try (Reader reader = new InputStreamReader( new FileInputStream( user.getFile() ), Charsets.UTF_8 )) { - byte[] keyID = null; - String fullName = null; - int mpVersion = 0, importFormat = 0, avatar = 0; - boolean clearContent = false, headerStarted = false, headerEnded = false; - MPResultType defaultType = null; + if (user.getFile().exists()) + try (Reader reader = new InputStreamReader( new FileInputStream( user.getFile() ), Charsets.UTF_8 )) { + byte[] keyID = null; + String fullName = null; + int mpVersion = 0, importFormat = 0, avatar = 0; + boolean clearContent = false, headerStarted = false, headerEnded = false; + MPResultType defaultType = null; - //noinspection HardcodedLineSeparator - for (final String line : CharStreams.readLines( reader )) - // Header delimitor. - if (line.startsWith( "##" )) { - if (!headerStarted) - // Starts the header. - headerStarted = true; - else - // Ends the header. - headerEnded = true; - } + //noinspection HardcodedLineSeparator + for (final String line : CharStreams.readLines( reader )) + // Header delimitor. + if (line.startsWith( "##" )) { + if (!headerStarted) + // Starts the header. + headerStarted = true; + else + // Ends the header. + headerEnded = true; + } - // Comment. - else if (line.startsWith( "#" )) { - if (headerStarted && !headerEnded) { - // In header. - Matcher headerMatcher = headerFormat.matcher( line ); - if (headerMatcher.matches()) { - String name = headerMatcher.group( 1 ), value = headerMatcher.group( 2 ); - if ("Format".equalsIgnoreCase( name )) - importFormat = ConversionUtils.toIntegerNN( value ); - else if ("Passwords".equalsIgnoreCase( name )) - clearContent = "visible".equalsIgnoreCase( value ); + // Comment. + else if (line.startsWith( "#" )) { + if (headerStarted && !headerEnded) { + // In header. + Matcher headerMatcher = headerFormat.matcher( line ); + if (headerMatcher.matches()) { + String name = headerMatcher.group( 1 ), value = headerMatcher.group( 2 ); + if ("Format".equalsIgnoreCase( name )) + importFormat = ConversionUtils.toIntegerNN( value ); + else if ("Passwords".equalsIgnoreCase( name )) + clearContent = "visible".equalsIgnoreCase( value ); + } } } - } - // No comment. - else if (headerEnded) { - Matcher siteMatcher = unmarshallFormats[importFormat].matcher( line ); - if (!siteMatcher.matches()) { - logger.wrn( "Couldn't parse line: %s, skipping.", line ); - continue; + // No comment. + else if (headerEnded) { + Matcher siteMatcher = unmarshallFormats[importFormat].matcher( line ); + if (!siteMatcher.matches()) { + logger.wrn( "Couldn't parse line: %s, skipping.", line ); + continue; + } + + MPFileSite site; + switch (importFormat) { + case 0: + site = new MPFileSite( user, // + siteMatcher.group( 5 ), MPAlgorithm.Version.fromInt( ConversionUtils.toIntegerNN( + colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(), + user.getAlgorithm().mpw_default_counter(), + MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ), + clearContent? null: siteMatcher.group( 6 ), + null, null, null, ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ), + MPConstants.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() ); + if (clearContent) + site.setSitePassword( site.getResultType(), siteMatcher.group( 6 ) ); + break; + + case 1: + site = new MPFileSite( user, // + siteMatcher.group( 7 ), MPAlgorithm.Version.fromInt( ConversionUtils.toIntegerNN( + colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(), + UnsignedInteger.valueOf( colon.matcher( siteMatcher.group( 5 ) ).replaceAll( "" ) ), + MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ), + clearContent? null: siteMatcher.group( 8 ), + MPResultType.GeneratedName, clearContent? null: siteMatcher.group( 6 ), null, + ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ), + MPConstants.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() ); + if (clearContent) { + site.setSitePassword( site.getResultType(), siteMatcher.group( 8 ) ); + site.setLoginName( MPResultType.StoredPersonal, siteMatcher.group( 6 ) ); + } + break; + + default: + throw new MPMarshalException( "Unexpected format: " + importFormat ); + } + + user.addSite( site ); } - MPFileSite site; - switch (importFormat) { - case 0: - site = new MPFileSite( user, // - siteMatcher.group( 5 ), MPAlgorithm.Version.fromInt( ConversionUtils.toIntegerNN( - colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(), - user.getAlgorithm().mpw_default_counter(), - MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ), - clearContent? null: siteMatcher.group( 6 ), - null, null, null, ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ), - MPConstants.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() ); - if (clearContent) - site.setSitePassword( site.getResultType(), siteMatcher.group( 6 ) ); - break; - - case 1: - site = new MPFileSite( user, // - siteMatcher.group( 7 ), MPAlgorithm.Version.fromInt( ConversionUtils.toIntegerNN( - colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(), - UnsignedInteger.valueOf( colon.matcher( siteMatcher.group( 5 ) ).replaceAll( "" ) ), - MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ), - clearContent? null: siteMatcher.group( 8 ), - MPResultType.GeneratedName, clearContent? null: siteMatcher.group( 6 ), null, - ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ), - MPConstants.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() ); - if (clearContent) { - site.setSitePassword( site.getResultType(), siteMatcher.group( 8 ) ); - site.setLoginName( MPResultType.StoredPersonal, siteMatcher.group( 6 ) ); - } - break; - - default: - throw new MPMarshalException( "Unexpected format: " + importFormat ); - } - - user.addSite( site ); - } - - if (user == null) - throw new MPMarshalException( "No full header found in import file." ); - } + if (user == null) + throw new MPMarshalException( "No full header found in import file." ); + } user.setComplete(); user.endChanges(); diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java index 29b585f3..a387ed72 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java @@ -60,10 +60,10 @@ public class MPJSONFile extends MPJSONAnyObject { objectMapper.setVisibility( PropertyAccessor.FIELD, JsonAutoDetect.Visibility.NON_PRIVATE ); } - public MPJSONFile() { + MPJSONFile() { } - public MPJSONFile(final MPFileUser modelUser) + MPJSONFile(final MPFileUser modelUser) throws MPAlgorithmException, MPKeyUnavailableException { // Section: "export" @@ -135,7 +135,7 @@ public class MPJSONFile extends MPJSONAnyObject { } } - public MPFileUser readUser(final File file) { + MPFileUser readUser(final File file) { MPAlgorithm algorithm = ifNotNullElse( user.algorithm, MPAlgorithm.Version.CURRENT ).getAlgorithm(); return new MPFileUser( @@ -147,10 +147,8 @@ public class MPJSONFile extends MPJSONAnyObject { ); } - public void readSites(final MPFileUser user) + void readSites(final MPFileUser user) throws MPIncorrectMasterPasswordException, MPKeyUnavailableException, MPAlgorithmException { - user.ignoreChanges(); - for (final Map.Entry siteEntry : sites.entrySet()) { String siteName = siteEntry.getKey(); Site fileSite = siteEntry.getValue(); @@ -184,9 +182,6 @@ public class MPJSONFile extends MPJSONAnyObject { user.addSite( site ); } - - user.setComplete(); - user.endChanges(); } // -- Data diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONUnmarshaller.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONUnmarshaller.java index 2d44cc02..10c870dc 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONUnmarshaller.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONUnmarshaller.java @@ -56,7 +56,11 @@ public class MPJSONUnmarshaller implements MPUnmarshaller { throws IOException, MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException, MPAlgorithmException { try { - objectMapper.readValue( user.getFile(), MPJSONFile.class ).readSites( user ); + user.ignoreChanges(); + if (user.getFile().exists()) + objectMapper.readValue( user.getFile(), MPJSONFile.class ).readSites( user ); + user.setComplete(); + user.endChanges(); } catch (final JsonParseException e) { throw new MPMarshalException( "Couldn't parse JSON.", e );