Added restore functionality to GUI.
authorJimmy Christensen <dusted@dusted.dk>
Thu, 26 Mar 2015 04:59:22 +0000 (06:59 +0200)
committerJimmy Christensen <dusted@dusted.dk>
Thu, 26 Mar 2015 04:59:22 +0000 (06:59 +0200)
Now missing: format, re-encrypt.

src/fkgui/Backup.java
src/fkgui/FkActionEventListener.java
src/fkgui/FkManager.java
src/fkgui/MainWin.java
src/fkgui/NewAccountDialog.java
src/fkgui/SaveSettingsDialog.java

index da2558b..4a322b6 100644 (file)
@@ -1,15 +1,12 @@
 package fkgui;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.io.IOException;
-import java.nio.file.Files;
-
-import org.eclipse.core.runtime.Path;
 import org.eclipse.swt.widgets.Dialog;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Composite;
@@ -22,7 +19,9 @@ import org.eclipse.swt.layout.FormAttachment;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Button;
 
-import com.sun.org.apache.bcel.internal.generic.CPInstruction;
+import org.eclipse.wb.swt.SWTResourceManager;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.swt.widgets.ProgressBar;
 
 public class Backup extends Dialog implements FkActionEventListener {
 
@@ -44,11 +43,22 @@ public class Backup extends Dialog implements FkActionEventListener {
         */
        private boolean restoreBackup;
        private File backupFile;
-       private String fileName;
        private Backup mySelf; 
        private Composite cmpBackupFileSelect;
-       
-       
+       private Composite cmpWaiting;
+       private Composite cmpStatus;
+       Animation animation;
+       Label lblFileName;
+       Button btnBackup;
+       Label lblMsg;
+       private Text txtFileName;
+       private Button btnBrowse;
+       private String fileName;
+       private Label lblStatusVerified;
+       private Button btnStatusOk;
+       private ProgressBar progressBar=null;
+
+
        public Object open(boolean _restoreBackup) {
                restoreBackup=_restoreBackup;
                createContents();
@@ -77,93 +87,281 @@ public class Backup extends Dialog implements FkActionEventListener {
         */
        private void createContents() {
                shell = new Shell(getParent(), SWT.DIALOG_TRIM);
-               shell.setSize(510, 506);
+               shell.setSize(512, 302);
                shell.setText(getText());
                shell.setLayout(new FormLayout());
                
+               //createStatusView("test");
+               //createWaitingScreen("Press the button now to take a backup of your FinalKey.");
+               //createBackupFileSelection();
+
+               createBackupFileSelection();
+               
+       }
+
+       private void createWaitingScreen(String msg) {
+               
+               cmpWaiting = new Composite(shell, SWT.NONE);
+               FormData fd_cmpWaiting = new FormData();
+               fd_cmpWaiting.top = new FormAttachment(0);
+               fd_cmpWaiting.left = new FormAttachment(0);
+               fd_cmpWaiting.bottom = new FormAttachment(0, 277);
+               fd_cmpWaiting.right = new FormAttachment(0, 510);
+               cmpWaiting.setLayoutData(fd_cmpWaiting);
+               
+               Label lblMsg = new Label(cmpWaiting, SWT.NONE);
+               lblMsg.setBounds(48, 10, 393, 43);
+               
+               lblMsg.setText(msg);
+               
+               animation = new Animation(cmpWaiting, SWT.NONE, 4);
+               animation.setBounds(10, 10, 32, 32);
+
+               animation.setVisible(true);
+               animation.setPlaying(true);
+               
+       }
+
+       private void createBackupFileSelection() {
                cmpBackupFileSelect = new Composite(shell, SWT.NONE);
                cmpBackupFileSelect.setLayout(new FormLayout());
                FormData fd_cmpBackupFileSelect = new FormData();
-               fd_cmpBackupFileSelect.bottom = new FormAttachment(0, 481);
-               fd_cmpBackupFileSelect.right = new FormAttachment(0, 506);
+               fd_cmpBackupFileSelect.bottom = new FormAttachment(0, 277);
+               fd_cmpBackupFileSelect.right = new FormAttachment(0, 510);
                fd_cmpBackupFileSelect.top = new FormAttachment(0);
                fd_cmpBackupFileSelect.left = new FormAttachment(0);
                cmpBackupFileSelect.setLayoutData(fd_cmpBackupFileSelect);
-               
-               Label lblNewLabel = new Label(cmpBackupFileSelect, SWT.NONE);
+
+               lblMsg = new Label(cmpBackupFileSelect, SWT.NONE);
                FormData fd_lblNewLabel = new FormData();
-               fd_lblNewLabel.top = new FormAttachment(0, 10);
+               fd_lblNewLabel.right = new FormAttachment(100, -91);
                fd_lblNewLabel.left = new FormAttachment(0, 10);
-               fd_lblNewLabel.bottom = new FormAttachment(0, 184);
-               fd_lblNewLabel.right = new FormAttachment(0, 496);
-               lblNewLabel.setLayoutData(fd_lblNewLabel);
-               lblNewLabel.setText("About backup:\nIt is a good idea to backup your FinalKey so you don't\nlose all your logins if you lose your FinalKey or it breaks.\n\nSecurity:\nThe FinalKey backup files are encrypted, but you should\nnever keep them on your computer or in a place where\nsomeone else may steal them. Keep your backup on\nan offline storage medium in a safe place.\n");
-               
-               Button btnBrowse = new Button(cmpBackupFileSelect, SWT.NONE);
+               lblMsg.setLayoutData(fd_lblNewLabel);
+
+               if(!restoreBackup)
+               {
+                       lblMsg.setText("About backup:\nIt is a good idea to backup your FinalKey so you don't\nlose all your logins if you lose your FinalKey or it breaks.\n\nSecurity:\nThe FinalKey backup files are encrypted, but you should\nnever keep them on your computer or in a place where\nsomeone else may steal them. Keep your backup on\nan offline storage medium in a safe place.\n");
+               } else {
+                       lblMsg.setText("Important! Read  first!\nThis will overwrite all data on your FinalKey with the data\nfrom the backup-file you select!\n\nThe password needed to unlock your FinalKey will be\nthe password that was used at the time that the backup\nwas made. If you do not know the password of the\nbackup file, your FinalKey is useless.\n");
+               }
+
+               btnBackup = new Button(cmpBackupFileSelect, SWT.NONE);
+               if(!restoreBackup)
+               {
+                       btnBackup.setText("Backup");
+                       btnBackup.setImage(SWTResourceManager.getImage(Backup.class, "/fkgui/gfx/backup.png"));
+               } else {
+                       btnBackup.setText("Restore");
+                       btnBackup.setImage(SWTResourceManager.getImage(Backup.class, "/fkgui/gfx/restore.png"));
+               }
+               FormData fd_btnBackup = new FormData();
+               fd_btnBackup.bottom = new FormAttachment(100, -10);
+               fd_btnBackup.right = new FormAttachment(100, -10);
+               fd_btnBackup.top = new FormAttachment(0, 234);
+               btnBackup.setLayoutData(fd_btnBackup);
+               btnBackup.setEnabled(false);
+
+
+               if(!restoreBackup)
+               {
+                       btnBackup.addSelectionListener( new SelectionAdapter() {
+                               public void widgetSelected(SelectionEvent e) {
+
+                                       fileName=txtFileName.getText();
+
+                                       try {
+
+                                               backupFile = new File(fileName);
+
+                                               if(backupFile.createNewFile())
+                                               {
+                                                       MessageBox m = new MessageBox(shell, SWT.ICON_INFORMATION);
+                                                       m.setText("Ready to backup");
+                                                       m.setMessage("When you press OK, The FinalKey will start blinking, you then have 5 seconds to press the button to allow backup.");
+                                                       shell.setEnabled(false);
+                                                       m.open();
+                                                       FkManager.getInstance().backup(backupFile, mySelf);
+                                                       shell.setEnabled(true);
+
+                                               } else {
+                                                       MessageBox m = new MessageBox(shell, SWT.ICON_ERROR);
+                                                       m.setText("File exists");
+                                                       m.setMessage("Will not overwrite an existing file.\nChose a new name or delete the existing file.");
+                                                       shell.setEnabled(false);
+                                                       m.open();
+                                                       shell.setEnabled(true);
+                                               }
+
+                                       } catch(Exception ex)
+                                       {
+                                               fileName="";
+                                               txtFileName.setText("");
+                                               btnBackup.setEnabled(false);
+                                               MessageBox m = new MessageBox(shell, SWT.ICON_ERROR);
+                                               m.setText("Could not create file.");
+                                               m.setMessage("The file could not be created: "+ex.getLocalizedMessage() );
+                                               shell.setEnabled(false);
+                                               m.open();
+                                               shell.setEnabled(true);
+                                       }
+
+                               }
+
+                       });
+               } else {
+                       btnBackup.addSelectionListener( new SelectionAdapter() {
+                               public void widgetSelected(SelectionEvent e) {
+
+                                       fileName=txtFileName.getText();
+
+
+                                       try {
+
+                                               backupFile = new File(fileName);
+
+                                               if( backupFile.canRead() )
+                                               {
+                                                       MessageBox m = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO);
+                                                       m.setText("Ready to Restore");
+                                                       m.setMessage("Press yes to restore the backup onto the Finalkey. You will have 5 seconds to press the button on your FinalKey to allow restore.\nIf you do not want to overwrite your FinalKey, press No to abort.");
+                                                       shell.setEnabled(false);
+                                                       if( m.open() == SWT.YES )
+                                                       {
+                                                               FkManager.getInstance().restore(backupFile, mySelf);
+                                                       } else {
+                                                               m = new MessageBox(shell, SWT.ICON_INFORMATION);
+                                                               m.setText("Aborted");
+                                                               m.setMessage("Restore was aborted.");
+                                                               m.open();
+                                                               shell.close();
+                                                       }
+
+                                               } else {
+                                                       MessageBox m = new MessageBox(shell, SWT.ICON_ERROR);
+                                                       m.setText("File exists");
+                                                       m.setMessage("Will not overwrite an existing file.\nChose a new name or delete the existing file.");
+                                                       shell.setEnabled(false);
+                                                       m.open();
+                                                       shell.setEnabled(true);
+                                               }
+
+                                       } catch(Exception ex)
+                                       {
+                                               fileName="";
+                                               txtFileName.setText("");
+                                               btnBackup.setEnabled(false);
+                                               MessageBox m = new MessageBox(shell, SWT.ICON_ERROR);
+                                               m.setText("Could not create file.");
+                                               m.setMessage("The file could not be created: "+ex.getLocalizedMessage() );
+                                               shell.setEnabled(false);
+                                               m.open();
+                                               shell.setEnabled(true);
+                                       }
+
+                               }
+
+                       });
+
+               } //Restore
+               fd_btnBackup.left = new FormAttachment(0, 371);
+
+               Label lblThisSavesThe = new Label(cmpBackupFileSelect, SWT.NONE);
+               fd_lblNewLabel.top = new FormAttachment(lblThisSavesThe, 18);
+               lblThisSavesThe.setFont(SWTResourceManager.getFont("Sans", 10, SWT.BOLD));
+               FormData fd_lblThisSavesThe = new FormData();
+               fd_lblThisSavesThe.top = new FormAttachment(0, 10);
+               fd_lblThisSavesThe.left = new FormAttachment(lblMsg, 0, SWT.LEFT);
+               fd_lblThisSavesThe.bottom = new FormAttachment(100, -245);
+               fd_lblThisSavesThe.right = new FormAttachment(100, -68);
+               lblThisSavesThe.setLayoutData(fd_lblThisSavesThe);
+
+               if(!restoreBackup)
+               {
+                       lblThisSavesThe.setText("Save the contents of FinalKey to a file");
+               } else {
+                       lblThisSavesThe.setText("Restore the contents of a file onto FinalKey");
+               }
+
+               lblFileName = new Label(cmpBackupFileSelect, SWT.NONE);
+               fd_lblNewLabel.bottom = new FormAttachment(100, -81);
+               lblFileName.setAlignment(SWT.RIGHT);
+               lblFileName.setText("File:");
+               FormData fd_lblFileName = new FormData();
+               fd_lblFileName.top = new FormAttachment(lblMsg, 49);
+               fd_lblFileName.bottom = new FormAttachment(100, -10);
+               fd_lblFileName.left = new FormAttachment(0, 10);
+               lblFileName.setLayoutData(fd_lblFileName);
+
+               txtFileName = new Text(cmpBackupFileSelect, SWT.BORDER);
+               fd_lblFileName.right = new FormAttachment(100, -463);
+               FormData fd_txtFileName = new FormData();
+               fd_txtFileName.top = new FormAttachment(lblMsg, 49);
+               fd_txtFileName.bottom = new FormAttachment(100, -10);
+               fd_txtFileName.left = new FormAttachment(lblFileName, 6);
+               txtFileName.setLayoutData(fd_txtFileName);
+               txtFileName.addListener(SWT.CHANGED, new Listener() {
+
+                       @Override
+                       public void handleEvent(Event arg0) {
+                               if( txtFileName.getText().length() > 0 )
+                               {
+                                       btnBackup.setEnabled(true);
+                               } else {
+                                       btnBackup.setEnabled(false);
+                               }
+                       }
+               });
+
+               btnBrowse = new Button(cmpBackupFileSelect, SWT.NONE);
+               fd_txtFileName.right = new FormAttachment(btnBrowse, -6);
                FormData fd_btnBrowse = new FormData();
-               fd_btnBrowse.right = new FormAttachment(100, -10);
-               fd_btnBrowse.top = new FormAttachment(100, -53);
-               fd_btnBrowse.bottom = new FormAttachment(100, -10);
+               fd_btnBrowse.top = new FormAttachment(lblMsg, 49);
+               fd_btnBrowse.bottom = new FormAttachment(btnBackup, 0, SWT.BOTTOM);
+               fd_btnBrowse.right = new FormAttachment(btnBackup, -6);
                btnBrowse.setLayoutData(fd_btnBrowse);
-               btnBrowse.setText("Browse ...");
-               
+               btnBrowse.setText("...");
                btnBrowse.addSelectionListener( new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent e) {
                                FileDialog fd = new FileDialog(shell, SWT.SAVE );
                                String[] ext = { "*.fkb", "*.*"  };
                                String[] extN = { "FinalKey Backup Files", "All Files"};
+
+                               //Try to start the selection dialog in the existing information, if it is correct and exist
+                               fileName = txtFileName.getText();
+                               if( fileName.length() != 0 )
+                               {
+                                       File testFile = new File(fileName);
+                                       String dir = testFile.getParent();
+                                       if( dir != null )
+                                       {
+                                               fd.setFilterPath(dir);
+
+                                               if( (!restoreBackup && !testFile.isFile()) || (restoreBackup && testFile.isFile()) )
+                                               {
+                                                       fd.setFileName(testFile.getName());
+                                               }
+                                       }
+                               }
+
+                               if(restoreBackup)
+                               {
+                                       fd.setFileName("");
+                               }
+
                                fd.setFilterExtensions( ext );
                                fd.setFilterNames(extN);
-                               
+
                                fileName = fd.open();
-                               System.out.println("FileName:" +fileName);
-                               backupFile = new File(fileName);
-                               if( !backupFile.exists() && !backupFile.isDirectory())
-                               { 
-                                       try {
-                                               backupFile.createNewFile();
-                                               
-                                               MessageBox m = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
-                                               m.setText("Ready to take backup?");
-                                               m.setMessage("When you press Yes, the FinalKey will blink, and you have 5 seconds to press the button to allow backup. Save backup now ?");
-                                               
-                                               if( m.open() == SWT.YES )
-                                               {
-                                                       FkManager.getInstance().backup(backupFile, mySelf);
-                                                       
-                                               } else {
-                                                       backupFile.delete();
-                                               }
 
-                                       } catch (IOException e1) {
-                                               MessageBox m = new MessageBox(shell, SWT.ERROR);
-                                               m.setText("Error");
-                                               m.setMessage("Could not create the file, do you have permissions to write there? Is there enough free space?");
-                                               m.open();
-                                               fileName=null;
-                                       }
-                               } else {
-                                       MessageBox m = new MessageBox(shell, SWT.ERROR);
-                                       m.setText("Error");
-                                       m.setMessage("Will not overwrite an existing file, select another file-name or delete the existing file first.");
-                                       m.open();
-                                       fileName=null;
+                               if( fileName != null )
+                               {
+
+                                       System.out.println("FileName: " + fileName);
+                                       txtFileName.setText(fileName);
+                                       btnBackup.setEnabled(true);
                                }
-                               
-                               System.out.println(fileName);
                        }
-                       
                });
-               
-               Label lblPressbrowseAnd = new Label(cmpBackupFileSelect, SWT.NONE);
-               fd_btnBrowse.left = new FormAttachment(0, 408);
-               FormData fd_lblPressbrowseAnd = new FormData();
-               fd_lblPressbrowseAnd.right = new FormAttachment(btnBrowse, -6);
-               fd_lblPressbrowseAnd.bottom = new FormAttachment(100, -10);
-               fd_lblPressbrowseAnd.left = new FormAttachment(0, 10);
-               fd_lblPressbrowseAnd.top = new FormAttachment(100, -77);
-               lblPressbrowseAnd.setLayoutData(fd_lblPressbrowseAnd);
-               lblPressbrowseAnd.setText("Press \"Browse\" to select where to save the backup.");
 
        }
 
@@ -171,15 +369,30 @@ public class Backup extends Dialog implements FkActionEventListener {
        public void fkActionEvent(FkActionEvent event) {
                MessageBox m;
                System.out.println(event);
-               
+
                //Handle errors differently when we are taking bacup
                if( !restoreBackup )
                {
                        switch( event.type )
                        {
                        case ACTION_ABORTED:
+                               backupFile.delete();
+                               m = new MessageBox(shell, SWT.ICON_WARNING| SWT.YES| SWT.NO);
+                               m.setText("Backup failed");
+                               m.setMessage("You did not press the button within 5 seconds, or you held down the button to abort the backup. Try again ?");
+
+                               if( m.open() == SWT.YES )
+                               {
+                                       cmpWaiting.dispose();
+                                       createBackupFileSelection();
+                                       shell.layout();
+                               } else {
+                                       shell.close();
+                               }
                                break;
                        case ACTION_ERROR:
+                               lblStatusVerified.setText("Valid: No - Validation failed.");
+
                                m = new MessageBox(shell, SWT.ICON_ERROR| SWT.YES| SWT.NO);
                                m.setText("Checksum Error");
                                m.setMessage("There were one or more errors when verifying the backup data, this may indicate a bad connection or a hardware-malfunction on the FinalKey.\nThe Backup File can NOT be restored onto the FinalKey, but it may be manually repaired by someone with a hex-editor and too much free-time. Do you want to delete the corrupted file ?");
@@ -200,30 +413,33 @@ public class Backup extends Dialog implements FkActionEventListener {
 
                                }
                                shell.close();
-                               
+
                                break;
                        case ACTION_OKAY:
-                               //TODO:
+                               lblStatusVerified.setText("Valid: Yes");
+                               progressBar.setSelection(66000);
                                m=new MessageBox(shell, SWT.ICON_INFORMATION);
                                m.setText("Backup successful");
-                               m.setMessage("Your backup was verified and saved.");
+                               m.setMessage("Backup was verified and saved.");
                                m.open();
                                shell.close();
                                break;
                        case ACTION_WAITING:
-                               //TODO:
-                               //Make visible icon
-                               //Write text "waiting for button press"
+                               cmpBackupFileSelect.dispose();
+                               createWaitingScreen("Press the FinalKey button to allow backup.\nPress and hold, or wait, to abort.");
+                               shell.layout();
                                break;
                        case ACTION_WORKING:
-                               //TODO:
-                               //Switch to status-view (writing)
-                               cmpBackupFileSelect.dispose();
+                               cmpWaiting.dispose();
+                               createStatusView("Backing up...");
+                               shell.layout();
+                               shell.setEnabled(false);
                                break;
                        case PROGRESS_UPDATE:
-                               
-                               break;
-                       case STATE_ERROR:
+                               if(progressBar!= null )
+                               {
+                                       progressBar.setSelection( Integer.parseInt( event.data ) );
+                               }
                                break;
                        case UNEXPECTED_ACTION_RESULT_ERROR:
                                break;
@@ -231,8 +447,123 @@ public class Backup extends Dialog implements FkActionEventListener {
                                break;
                        
                        }
+               } else {
+                       boolean disconnect=false;
+                       switch(event.type)
+                       {
+                       case ACTION_ABORTED:
+                               m = new MessageBox(shell, SWT.ICON_ERROR);
+                               m.setText("Restore aborted");
+                               m.setMessage("You did not press the button within 5 second, restore aborted.");
+                               m.open();
+                               shell.close();
+                               break;
+                       case ACTION_ERROR:
+                               disconnect=true;
+                               m = new MessageBox(shell, SWT.ICON_ERROR);
+                               m.setText("Restore Error");
+                               m.setMessage("Something went wrong, the backup was not fully restored, error: "+event.data);
+                               m.open();
+                               break;
+                       case ACTION_OKAY:
+                               lblStatusVerified.setText("Restore complete.");
+                               lblStatusVerified.setVisible(true);
+                               m = new MessageBox(shell, SWT.ICON_INFORMATION);
+                               m.setText("Restore success");
+                               m.setMessage("The backup has been restored to your FinalKey.\nPress the FinalKey button now, so the blinking stops.");
+                               FkManager.getInstance().disconnect();
+                               m.open();
+                               shell.close();
+                               break;
+                       case ACTION_WAITING:
+                               shell.setEnabled(false);
+                               cmpBackupFileSelect.dispose();
+                               createWaitingScreen("Press the FinalKey button to allo restore.\nPress and hold, or wait, to abort.");
+                               shell.layout();
+                               break;
+                       case ACTION_WORKING:
+                               cmpWaiting.dispose();
+                               createStatusView("Writing data to FinalKey...");
+                               lblStatusVerified.setVisible(false);
+                               shell.layout();
+                               break;
+                       case FILE_ERROR:
+                               m = new MessageBox(shell, SWT.ICON_ERROR);
+                               m.setText("Restore Aborted");
+                               m.setMessage("There was a problem with the backup file: "+event.data);
+                               m.open();
+                               shell.setEnabled(true);
+                               break;
+                       case PROGRESS_UPDATE:
+                               progressBar.setSelection(Integer.parseInt(event.data));
+                               break;
+                       default:
+                               break;
+
+                       }
+
+                       if(event.type == FkActionEventType.STATE_ERROR )
+                       {
+                               m = new MessageBox(shell, SWT.ICON_ERROR);
+                               m.setText("State error");
+                               m.setMessage("The FinalKey was in an unexpected state, operation failed.");
+                               m.open();
+                               disconnect=true;
+                       }
+
+                       if(disconnect)
+                       {
+                               FkManager.getInstance().disconnect();
+                               shell.close();
+                       }
+
                }
-               
+
        }
 
+       private void createStatusView(String msg) {
+               cmpStatus = new Composite(shell, SWT.NONE);
+               cmpStatus.setLayout(new FormLayout());
+               FormData fd_cmpStatus = new FormData();
+               fd_cmpStatus.bottom = new FormAttachment(0, 277);
+               fd_cmpStatus.right = new FormAttachment(0, 510);
+               fd_cmpStatus.top = new FormAttachment(0);
+               fd_cmpStatus.left = new FormAttachment(0);
+               cmpStatus.setLayoutData(fd_cmpStatus);
+
+               Label lblStatusMsg = new Label(cmpStatus, SWT.NONE);
+               FormData fd_lblStatusMsg = new FormData();
+               fd_lblStatusMsg.right = new FormAttachment(0, 500);
+               fd_lblStatusMsg.top = new FormAttachment(0, 10);
+               fd_lblStatusMsg.left = new FormAttachment(0, 10);
+               lblStatusMsg.setLayoutData(fd_lblStatusMsg);
+               lblStatusMsg.setText(msg);
+
+               progressBar = new ProgressBar(cmpStatus, SWT.NONE);
+               progressBar.setToolTipText("Progress");
+               progressBar.setMaximum(66000);
+               FormData fd_progressBar = new FormData();
+               fd_progressBar.right = new FormAttachment(lblStatusMsg, 0, SWT.RIGHT);
+               fd_progressBar.top = new FormAttachment(lblStatusMsg, 6);
+               fd_progressBar.left = new FormAttachment(0, 10);
+               progressBar.setLayoutData(fd_progressBar);
+
+               lblStatusVerified = new Label(cmpStatus, SWT.NONE);
+               FormData fd_lblStatusVerified = new FormData();
+               fd_lblStatusVerified.right = new FormAttachment(lblStatusMsg, 0, SWT.RIGHT);
+               fd_lblStatusVerified.top = new FormAttachment(progressBar, 6);
+               fd_lblStatusVerified.left = new FormAttachment(lblStatusMsg, 0, SWT.LEFT);
+               lblStatusVerified.setLayoutData(fd_lblStatusVerified);
+               lblStatusVerified.setText("Valid: Not verified yet");
+
+               btnStatusOk = new Button(cmpStatus, SWT.NONE);
+               fd_lblStatusVerified.bottom = new FormAttachment(btnStatusOk, -6);
+               FormData fd_btnStatusOk = new FormData();
+               fd_btnStatusOk.bottom = new FormAttachment(100, -10);
+               fd_btnStatusOk.right = new FormAttachment(100, -10);
+               btnStatusOk.setLayoutData(fd_btnStatusOk);
+               btnStatusOk.setText("OK");
+               btnStatusOk.setVisible(false);
+
+       }
 }
index f44ee32..e4e44ac 100644 (file)
@@ -23,6 +23,6 @@ public interface FkActionEventListener {
                        return("Event {\n  Type: "+type+"\n  Data: "+data+"\n  Account: "+((acc!=null)?acc.toString():"null")+"\n  Action:"+action+"\n}");
                }
        }
-       public enum FkActionEventType { ACTION_ABORTED, ACTION_OKAY, ACTION_ERROR, ACTION_WAITING, ACTION_WORKING, STATE_ERROR, UNEXPECTED_ACTION_RESULT_ERROR, PROGRESS_UPDATE };
+       public enum FkActionEventType { ACTION_ABORTED, ACTION_OKAY, ACTION_ERROR, ACTION_WAITING, ACTION_WORKING, STATE_ERROR, UNEXPECTED_ACTION_RESULT_ERROR, PROGRESS_UPDATE, FILE_ERROR };
        public void fkActionEvent( FkActionEvent event );
 }
index 8d95a4c..8e232e5 100644 (file)
@@ -3,8 +3,9 @@ package fkgui;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.nio.charset.StandardCharsets;
+import java.io.IOException;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Vector;
@@ -736,7 +737,6 @@ public class FkManager implements ActionListener {
                        
                        int timeOut=0;
                        String data="";
-                       String msg = "";
 
 
                        try {
@@ -855,7 +855,6 @@ public class FkManager implements ActionListener {
                        
                        int timeOut=0;
                        String data="";
-                       String msg = "";
 
 
                        try {
@@ -989,23 +988,6 @@ public class FkManager implements ActionListener {
                        delegate=d;
                }
                
-               /*
-                * 
-                * uint8_t crc8(const uint8_t *addr, uint8_t len)
-               {
-                       uint8_t crc = 0;
-
-                       while (len--) {
-                               uint8_t inbyte = *addr++;
-                               for (uint8_t i = 8; i; i--) {
-                                       uint8_t mix = (crc ^ inbyte) & 0x01;
-                                       crc >>= 1;
-                                       if (mix) crc ^= 0x8C;
-                                       inbyte >>= 1;
-                               }
-                       }
-                       return crc;
-               }*/
                
                byte crc8( byte[] dat, int begin, int len )
                {
@@ -1074,7 +1056,7 @@ public class FkManager implements ActionListener {
                                                }
 
 
-                                               data += new String(bin.toArray());
+                                               data = new String(bin.toArray());
 
                                                if( state==0 && data.contains("[RDY]\r\n") )
                                                {
@@ -1161,11 +1143,9 @@ public class FkManager implements ActionListener {
                                                                                        }
                                                                                }
                                                                        }
-                                                                       
-                                                                       
+
                                                                }
-                                                               
-                                                               
+
                                                                break;
                                                        } else {
                                                                Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.PROGRESS_UPDATE, ""+bin.size() ,null,'e') );
@@ -1178,10 +1158,9 @@ public class FkManager implements ActionListener {
                                        }
                                }
                                Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "Timed out" ,null,'e') );
-                               
-                               
+
                        } catch (Exception e) { 
-                               
+
                        }
                }
        }
@@ -1190,8 +1169,176 @@ public class FkManager implements ActionListener {
                BackupTask bt = new BackupTask(f, delegate );
                new Thread(bt).start();
        }
-       
-       
 
+       private class RestoreTask implements Runnable
+       {
+
+               private FkActionEventListener delegate;
+               private File file;
+
+               public RestoreTask(File f, FkActionEventListener d )
+               {
+                       file=f;
+                       delegate=d;
+               }
+
+
+               @Override
+               public void run() {
+
+                       int state=0;
+                       long timeStamp=System.currentTimeMillis();
+
+                       String data="";
+
+                       byte[] bin = new byte[66000];
+
+                       boolean fileOk=false;
+                       FileInputStream in=null;
+                       try {
+                               in = new FileInputStream(file);
+
+                               int len = in.read(bin);
+                               if( len == 66000 )
+                               {
+                                       fileOk=true;
+                               }
+
+                       } catch (Exception e1) {
+                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.FILE_ERROR, e1.getLocalizedMessage() ,null,'i') );
+                               return;
+                       } finally {
+                               try {
+                                       if( in != null )
+                                       {
+                                               in.close();
+                                       }
+                               } catch (IOException e) {}
+                       }
+
+                       String verify = new String( bin ).substring(0,9);
+
+                       if( !fileOk  || verify.compareTo("[FinalKey") != 0)
+                       {
+                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.FILE_ERROR, "Not a FinalKey backup file." ,null,'i') );
+                               fileOk=false;
+                       }
+
+                       if( !fileOk )
+                       {
+                               return;
+                       }
+
+                       try {
+
+                               //First check that we get a prompt
+                               if( !checkState() )
+                               {
+                                       Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.STATE_ERROR, "EXCEPTION",null,'i') );
+                                       return;
+                               }
+
+                               //Drain any previous input.
+                               while( com.serialPort.getInputBufferBytesCount() > 0 )
+                               {
+                                       com.serialPort.readBytes();
+                               }
+                               com.serialPort.writeByte((byte)'X');
+                               com.serialPort.writeByte((byte)'i');
+
+
+                               int idx=0;
+                               while(System.currentTimeMillis() - timeStamp < 6000L)
+                               {
+                                       if( com.serialPort.getInputBufferBytesCount() > 0 )
+                                       {
+                                               data += com.serialPort.readString();
+                                       }
+                                       if( state==0 && data.contains("[RDY]\r\n") )
+                                       {
+                                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_WAITING, "WAITING",null,'i') );
+                                               data="";
+                                               state=1;
+                                       }
+
+                                       if( state==1 && data.contains("[NO]") )
+                                       {
+                                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ABORTED, "",null,'i') );
+                                               return;
+                                       }
+
+                                       if( state==1 && data.contains("[OK]\r\n") )
+                                       {
+                                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_WORKING, "WORKING",null,'i') );
+                                               state=2;
+
+                                               //Don't reset data, state2 may need some of it
+                                       }
+
+                                       //Catch E = END, F=Fail, C=CRC Fail O=CRC OK
+                                       if( data.contains("E") )
+                                       {
+                                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_OKAY, "Done.",null,'i') );
+                                               return;
+                                       }
+
+                                       if( data.contains("F") )
+                                       {
+                                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "Device reported read-error.",null,'i') );
+                                               return;
+                                       }
+
+                                       if( data.contains("C") )
+                                       {
+                                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "Device reported CRC-error.",null,'i') );
+                                               return;
+                                       }
+
+                                       //Device ready for more data
+                                       if( state==2 && data.contains("R") )
+                                       {
+                                               data="";
+                                               timeStamp = System.currentTimeMillis();
+
+                                               byte[] outBuf = new byte[33];
+
+                                               for(int i=0;i<33;i++)
+                                               {
+                                                       outBuf[i] = bin[idx++];
+                                               }
+
+                                               com.serialPort.writeBytes(outBuf);
+
+                                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.PROGRESS_UPDATE, ""+idx ,null,'i') );
+
+                                               state=3;
+                                       }
+
+                                       if( state == 3 && data.contains("W") && data.contains("O") )
+                                       {
+                                               state=2;
+                                       }
+
+                               }
+                       } catch(Exception e)
+                       {
+                               Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "Exception: "+e.getLocalizedMessage(),null,'i') );
+                               return;
+
+                       }
+
+                       Display.getDefault().asyncExec( new FkActionEventMsg(delegate, FkActionEventListener.FkActionEventType.ACTION_ERROR, "No data from device.",null,'i') );
+
+               }
+       }
+
+       public void restore(File f, FkActionEventListener delegate) {
+               RestoreTask rt = new RestoreTask(f, delegate );
+               new Thread(rt).start();
+       }
+
+       public void disconnect() {
+               com.disconnect();
+       }
 
 }
index 225fc10..7691313 100644 (file)
@@ -1,9 +1,7 @@
 package fkgui;
 
 
-import java.io.IOException;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.Map;
 import java.util.Vector;
 import  java.util.prefs.*;
@@ -11,7 +9,6 @@ import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
-import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 
 import org.eclipse.jface.viewers.DoubleClickEvent;
@@ -27,8 +24,6 @@ import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.ControlEvent;
-import org.eclipse.swt.events.ControlListener;
 import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.FocusListener;
 import org.eclipse.swt.events.KeyEvent;
@@ -38,8 +33,6 @@ import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.events.ShellEvent;
-import org.eclipse.swt.events.ShellListener;
 import org.eclipse.swt.events.VerifyEvent;
 import org.eclipse.swt.events.VerifyListener;
 import org.eclipse.swt.graphics.Color;
@@ -61,8 +54,6 @@ import org.eclipse.swt.layout.FormData;
 import org.eclipse.swt.layout.FormAttachment;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.List;
-import org.eclipse.swt.awt.SWT_AWT;
-import org.eclipse.swt.custom.CCombo;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Group;
 
index 08a68a1..c03a62c 100644 (file)
@@ -932,6 +932,7 @@ public class NewAccountDialog extends Dialog implements FkActionEventListener {
 
        }
 
+       @SuppressWarnings("incomplete-switch")
        @Override
        public void fkActionEvent(FkActionEvent event) {
                //System.out.println( "Event data:"+ event.data );
index 0d12bc3..f8257d6 100644 (file)
@@ -6,9 +6,7 @@ import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.FormLayout;
-import org.eclipse.swt.widgets.Group;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.FormData;
 import org.eclipse.swt.layout.FormAttachment;