Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistence for the remote database location + some warnings fixed #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/com/_17od/upm/gui/AccountDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private void okButtonAction() {
if (!pAccount.getUserId().equals(userId.getText())) {
accountChanged = true;
}
if (!pAccount.getPassword().equals(password.getText())) {
if (!pAccount.getPassword().equals(password.getPassword())) {
accountChanged = true;
}
if (!pAccount.getUrl().equals(url.getText())) {
Expand All @@ -410,7 +410,7 @@ private void okButtonAction() {

pAccount.setAccountName(accountName.getText().trim());
pAccount.setUserId(userId.getText());
pAccount.setPassword(password.getText());
pAccount.setPassword(new String(password.getPassword()));
pAccount.setUrl(url.getText());
pAccount.setNotes(notes.getText());

Expand Down
74 changes: 46 additions & 28 deletions src/com/_17od/upm/gui/DatabaseActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void windowGainedFocus(WindowEvent e) {
masterPassword.requestFocusInWindow();
}
});
dialog.show();
dialog.setVisible(true);

if (pane.getValue().equals(new Integer(JOptionPane.OK_OPTION))) {
if (!Arrays.equals(masterPassword.getPassword(), confirmedMasterPassword.getPassword())) {
Expand Down Expand Up @@ -191,7 +191,7 @@ public void windowGainedFocus(WindowEvent e) {
masterPassword.requestFocusInWindow();
}
});
dialog.show();
dialog.setVisible(true);

buttonClicked = pane.getValue();
if (buttonClicked.equals(new Integer(JOptionPane.OK_OPTION))) {
Expand Down Expand Up @@ -351,7 +351,7 @@ public void windowGainedFocus(WindowEvent e) {
masterPassword.requestFocusInWindow();
}
});
dialog.show();
dialog.setVisible(true);

if (pane.getValue() != null && pane.getValue().equals(new Integer(JOptionPane.OK_OPTION))) {
password = masterPassword.getPassword();
Expand Down Expand Up @@ -448,7 +448,7 @@ public void addAccount() throws IOException, CryptoException, TransportException
AccountDialog accDialog = new AccountDialog(accInfo, mainWindow, false, accountNames);
accDialog.pack();
accDialog.setLocationRelativeTo(mainWindow);
accDialog.show();
accDialog.setVisible(true);

//If the user press OK then save the new account to the database
if (accDialog.okClicked()) {
Expand Down Expand Up @@ -502,7 +502,7 @@ public void viewAccount() {
AccountDialog accDialog = new AccountDialog(accInfo, mainWindow, true, accountNames);
accDialog.pack();
accDialog.setLocationRelativeTo(mainWindow);
accDialog.show();
accDialog.setVisible(true);
}


Expand All @@ -521,7 +521,7 @@ public void editAccount(String accountName) throws TransportException,
AccountDialog accDialog = new AccountDialog(accInfo, mainWindow, false, accountNames);
accDialog.pack();
accDialog.setLocationRelativeTo(mainWindow);
accDialog.show();
accDialog.setVisible(true);

//If the ok button was clicked then save the account to the database and update the
//listview with the new account name (if it's changed)
Expand Down Expand Up @@ -613,7 +613,7 @@ public void options() {
OptionsDialog oppDialog = new OptionsDialog(mainWindow);
oppDialog.pack();
oppDialog.setLocationRelativeTo(mainWindow);
oppDialog.show();
oppDialog.setVisible(true);

configureAutoLock();

Expand All @@ -630,7 +630,7 @@ public void showAbout() {
AboutDialog aboutDialog = new AboutDialog(mainWindow);
aboutDialog.pack();
aboutDialog.setLocationRelativeTo(mainWindow);
aboutDialog.show();
aboutDialog.setVisible(true);
}


Expand All @@ -645,7 +645,7 @@ public void showDatabaseProperties() throws ProblemReadingDatabaseFile, IOExcept
DatabasePropertiesDialog dbPropsDialog = new DatabasePropertiesDialog(mainWindow, getAccountNames(), database);
dbPropsDialog.pack();
dbPropsDialog.setLocationRelativeTo(mainWindow);
dbPropsDialog.show();
dbPropsDialog.setVisible(true);
if (dbPropsDialog.getDatabaseNeedsSaving()) {
saveDatabase();
}
Expand All @@ -661,14 +661,47 @@ public void showDatabaseProperties() throws ProblemReadingDatabaseFile, IOExcept
}


public void openDatabaseFromURL(String remoteLocation,
String username, String password, String saveDatabaseTo) throws TransportException, IOException, ProblemReadingDatabaseFile, CryptoException
{
openDatabaseFromURL(remoteLocation, username, password, new File(saveDatabaseTo));
}

public void openDatabaseFromURL(String remoteLocation,
String username, String password, File saveDatabaseTo) throws TransportException, IOException, ProblemReadingDatabaseFile, CryptoException
{
// Download the database
Transport transport = Transport.getTransportForURL(new URL(remoteLocation));
File downloadedDatabaseFile = transport.getRemoteFile(remoteLocation, username, password);

// Delete the file is it already exists
if (saveDatabaseTo.exists()) {
saveDatabaseTo.delete();
}

// Save the downloaded database file to the new location
Util.copyFile(downloadedDatabaseFile, saveDatabaseTo);

// Persist the URL options
Preferences.set(Preferences.ApplicationOptions.HTTP_DATABASE_URL, remoteLocation);
Preferences.set(Preferences.ApplicationOptions.HTTP_DATABASE_USER, username);
Preferences.set(Preferences.ApplicationOptions.HTTP_DATABASE_PASSWORD, password);
Preferences.set(Preferences.ApplicationOptions.DB_TO_LOAD_ON_STARTUP, saveDatabaseTo.getAbsolutePath());
Preferences.save();

// Now open the downloaded database
openDatabase(saveDatabaseTo.getAbsolutePath());

}

public void openDatabaseFromURL() throws TransportException, IOException, ProblemReadingDatabaseFile, CryptoException {

// Ask the user for the remote database location
OpenDatabaseFromURLDialog openDBDialog = new OpenDatabaseFromURLDialog(mainWindow);
openDBDialog.pack();
openDBDialog.setLocationRelativeTo(mainWindow);
openDBDialog.show();
openDBDialog.setVisible(true);

if (openDBDialog.getOkClicked()) {
// Get the remote database options
String remoteLocation = openDBDialog.getUrlTextField().getText();
Expand All @@ -677,24 +710,9 @@ public void openDatabaseFromURL() throws TransportException, IOException, Proble

// Ask the user for a location to save the database file to
File saveDatabaseTo = getSaveAsFile(Translator.translate("saveDatabaseAs"));

if (saveDatabaseTo != null) {

// Download the database
Transport transport = Transport.getTransportForURL(new URL(remoteLocation));
File downloadedDatabaseFile = transport.getRemoteFile(remoteLocation, username, password);

// Delete the file is it already exists
if (saveDatabaseTo.exists()) {
saveDatabaseTo.delete();
}

// Save the downloaded database file to the new location
Util.copyFile(downloadedDatabaseFile, saveDatabaseTo);

// Now open the downloaded database
openDatabase(saveDatabaseTo.getAbsolutePath());

if (saveDatabaseTo != null) {
openDatabaseFromURL(remoteLocation, username, password, saveDatabaseTo);
}
}

Expand Down
22 changes: 17 additions & 5 deletions src/com/_17od/upm/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,25 @@ public MainWindow(String title) throws ClassNotFoundException, InstantiationExce

try {
//Load the startup database if it's configured
String url = Preferences.get(Preferences.ApplicationOptions.HTTP_DATABASE_URL);
String username = Preferences.get(Preferences.ApplicationOptions.HTTP_DATABASE_USER);
String password = Preferences.get(Preferences.ApplicationOptions.HTTP_DATABASE_PASSWORD);
String db = Preferences.get(Preferences.ApplicationOptions.DB_TO_LOAD_ON_STARTUP);

if (db != null && !db.equals("")) {
File dbFile = new File(db);
if (!dbFile.exists()) {
dbActions.errorHandler(new Exception(Translator.translate("dbDoesNotExist", db)));
} else {
dbActions.openDatabase(db);

if (url != null && !url.equals(""))
{
dbActions.openDatabaseFromURL(url, username, password, db);
}
else
{
File dbFile = new File(db);
if (!dbFile.exists()) {
dbActions.errorHandler(new Exception(Translator.translate("dbDoesNotExist", db)));
} else {
dbActions.openDatabase(db);
}
}
}
} catch (Exception e) {
Expand Down
3 changes: 3 additions & 0 deletions src/com/_17od/upm/util/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class ApplicationOptions {
public static final String HTTP_PROXY_USERNAME="http.proxy.username";
public static final String HTTP_PROXY_PASSWORD="http.proxy.password";
public static final String HTTPS_ACCEPT_SELFSIGNED_CERTS="https.accept.selfsigned.certs";
public static final String HTTP_DATABASE_USER="http.database.user";
public static final String HTTP_DATABASE_PASSWORD="http.database.password";
public static final String HTTP_DATABASE_URL="http.database.url";

public static final String LOCALE="locale";
}
Expand Down
6 changes: 3 additions & 3 deletions test/com/_17od/upm/database/TestAccountInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testAssemble() throws IOException, ProblemReadingDatabaseFile {
public void testAssembleCharsInFieldLength() throws IOException {
ByteArrayInputStream is = new ByteArrayInputStream("bad input".getBytes());
try {
AccountInformation ai = new AccountInformation(is);
new AccountInformation(is);
fail("Should have got an ProblemReadingDatabaseFile exception now");
} catch (ProblemReadingDatabaseFile e ) {
//ok to get here
Expand All @@ -83,7 +83,7 @@ public void testAssembleCharsInFieldLength() throws IOException {
public void testAssembleBadFieldLength() throws IOException, ProblemReadingDatabaseFile {
ByteArrayInputStream is = new ByteArrayInputStream("0".getBytes());
try {
AccountInformation ai = new AccountInformation(is);
new AccountInformation(is);
fail("Should have got an EOFException exception now");
} catch (EOFException e ) {
//ok to get here
Expand All @@ -94,7 +94,7 @@ public void testAssembleBadFieldLength() throws IOException, ProblemReadingDatab
public void testAssembleBadFieldContents() throws IOException, ProblemReadingDatabaseFile {
ByteArrayInputStream is = new ByteArrayInputStream("0004".getBytes());
try {
AccountInformation ai = new AccountInformation(is);
new AccountInformation(is);
fail("Should have got an EOFException exception now");
} catch (EOFException e ) {
//ok to get here
Expand Down