New user fixes.
This commit is contained in:
		@@ -90,7 +90,7 @@ public class CollectionListModel<E> extends AbstractListModel<E>
 | 
			
		||||
    @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 );
 | 
			
		||||
 
 | 
			
		||||
@@ -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 ) );
 | 
			
		||||
                } );
 | 
			
		||||
            } );
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -204,7 +204,7 @@ public abstract class MPBasicSite<U extends MPUser<?>, 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
 | 
			
		||||
 
 | 
			
		||||
@@ -226,7 +226,7 @@ public abstract class MPBasicUser<S extends MPBasicSite<?, ?>> 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
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
@@ -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<String, Site> 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
 | 
			
		||||
 
 | 
			
		||||
@@ -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 );
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user