From c38920f238afd06e48a599532e79468b9f1481e3 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sat, 14 Jun 2014 23:33:00 -0400 Subject: [PATCH] Improve reliability with regards to bad config files. --- .../ConfigAuthenticationPanel.java | 27 ++++++++++++------- .../lhunath/masterpassword/UnlockFrame.java | 22 ++------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/ConfigAuthenticationPanel.java b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/ConfigAuthenticationPanel.java index e49adcdc..36f3244f 100644 --- a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/ConfigAuthenticationPanel.java +++ b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/ConfigAuthenticationPanel.java @@ -1,5 +1,7 @@ package com.lyndir.lhunath.masterpassword; +import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse; + import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -8,6 +10,7 @@ import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Iterator; +import java.util.NoSuchElementException; import javax.swing.*; @@ -45,13 +48,9 @@ public class ConfigAuthenticationPanel extends AuthenticationPanel implements It return (User) userField.getSelectedItem(); } - @Override public String getHelpText() { - return "" - + "Reads users from ~/.mpw

" - + "Use the following syntax:
" - + "My Name:mymasterpassword

" - + "Make sure the read permissions
to the file are safe!"; + return "Reads users from ~/.mpw, the following syntax applies:\nUser Name:masterpassword" + + "\n\nEnsure the file's permissions make it only readable by you!"; } public static boolean hasConfigUsers() { @@ -75,11 +74,21 @@ public class ConfigAuthenticationPanel extends AuthenticationPanel implements It return Iterables.toArray( users.build(), User.class ); } catch (FileNotFoundException e) { - return null; + JOptionPane.showMessageDialog( this, "First create the config file at:\n" + mpwConfig.getAbsolutePath() + + "\n\nIt should contain a line for each user of the following format:" + + "\nUser Name:masterpassword" + + "\n\nEnsure the file's permissions make it only readable by you!", // + "Config File Not Found", JOptionPane.WARNING_MESSAGE ); + return new User[0]; } - catch (IOException e) { + catch (IOException | NoSuchElementException e) { e.printStackTrace(); - return null; + String error = ifNotNullElse( e.getLocalizedMessage(), ifNotNullElse( e.getMessage(), e.toString() ) ); + JOptionPane.showMessageDialog( this, // + "Problem reading config file:\n" + mpwConfig.getAbsolutePath() // + + "\n\n" + error, // + "Config File Not Readable", JOptionPane.WARNING_MESSAGE ); + return new User[0]; } } diff --git a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/UnlockFrame.java b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/UnlockFrame.java index ef15a188..10f0b889 100644 --- a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/UnlockFrame.java +++ b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/lhunath/masterpassword/UnlockFrame.java @@ -97,26 +97,8 @@ public class UnlockFrame extends JFrame { typeHelp.addActionListener( new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { - final JDialog dialog = new JDialog( UnlockFrame.this, "Help", true ); - dialog.setContentPane( new JPanel( new BorderLayout( 8, 8 ) ) { - { - setBorder( new EmptyBorder( 8, 8, 8, 8 ) ); - } - } ); - dialog.add( new JLabel( authenticationPanel.getHelpText() ), BorderLayout.CENTER ); - dialog.add( new JButton( "OK" ) { - { - addActionListener( new ActionListener() { - @Override - public void actionPerformed(final ActionEvent e) { - dialog.dispose(); - } - } ); - } - }, BorderLayout.SOUTH ); - dialog.pack(); - dialog.setLocationRelativeTo( UnlockFrame.this ); - dialog.setVisible( true ); + JOptionPane.showMessageDialog( UnlockFrame.this, authenticationPanel.getHelpText(), "Help", + JOptionPane.INFORMATION_MESSAGE ); } } ); if (authenticationPanel.getHelpText() == null) {