da2558b99a948d5cb2fe08cc78901f82674ea20b
[FinalKeyGui.git] / src / fkgui / Backup.java
1 package fkgui;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.nio.file.Files;
8
9 import org.eclipse.core.runtime.Path;
10 import org.eclipse.swt.widgets.Dialog;
11 import org.eclipse.swt.widgets.Display;
12 import org.eclipse.swt.widgets.FileDialog;
13 import org.eclipse.swt.widgets.MessageBox;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.FormLayout;
20 import org.eclipse.swt.layout.FormData;
21 import org.eclipse.swt.layout.FormAttachment;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Button;
24
25 import com.sun.org.apache.bcel.internal.generic.CPInstruction;
26
27 public class Backup extends Dialog implements FkActionEventListener {
28
29         protected Object result;
30         protected Shell shell;
31
32         /**
33          * Create the dialog.
34          * @param parent
35          * @param style
36          */
37         public Backup(Shell parent, int style) {
38                 super(parent, style);
39         }
40
41         /**
42          * Open the dialog.
43          * @return the result
44          */
45         private boolean restoreBackup;
46         private File backupFile;
47         private String fileName;
48         private Backup mySelf; 
49         private Composite cmpBackupFileSelect;
50         
51         
52         public Object open(boolean _restoreBackup) {
53                 restoreBackup=_restoreBackup;
54                 createContents();
55                 shell.open();
56                 shell.layout();
57                 mySelf = this;
58
59                 if( _restoreBackup )
60                 {
61                         setText("FinalKey - Restore");
62                 } else {
63                         setText("FinalKey - Backup");
64                 }
65
66                 Display display = getParent().getDisplay();
67                 while (!shell.isDisposed()) {
68                         if (!display.readAndDispatch()) {
69                                 display.sleep();
70                         }
71                 }
72                 return result;
73         }
74
75         /**
76          * Create contents of the dialog.
77          */
78         private void createContents() {
79                 shell = new Shell(getParent(), SWT.DIALOG_TRIM);
80                 shell.setSize(510, 506);
81                 shell.setText(getText());
82                 shell.setLayout(new FormLayout());
83                 
84                 cmpBackupFileSelect = new Composite(shell, SWT.NONE);
85                 cmpBackupFileSelect.setLayout(new FormLayout());
86                 FormData fd_cmpBackupFileSelect = new FormData();
87                 fd_cmpBackupFileSelect.bottom = new FormAttachment(0, 481);
88                 fd_cmpBackupFileSelect.right = new FormAttachment(0, 506);
89                 fd_cmpBackupFileSelect.top = new FormAttachment(0);
90                 fd_cmpBackupFileSelect.left = new FormAttachment(0);
91                 cmpBackupFileSelect.setLayoutData(fd_cmpBackupFileSelect);
92                 
93                 Label lblNewLabel = new Label(cmpBackupFileSelect, SWT.NONE);
94                 FormData fd_lblNewLabel = new FormData();
95                 fd_lblNewLabel.top = new FormAttachment(0, 10);
96                 fd_lblNewLabel.left = new FormAttachment(0, 10);
97                 fd_lblNewLabel.bottom = new FormAttachment(0, 184);
98                 fd_lblNewLabel.right = new FormAttachment(0, 496);
99                 lblNewLabel.setLayoutData(fd_lblNewLabel);
100                 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");
101                 
102                 Button btnBrowse = new Button(cmpBackupFileSelect, SWT.NONE);
103                 FormData fd_btnBrowse = new FormData();
104                 fd_btnBrowse.right = new FormAttachment(100, -10);
105                 fd_btnBrowse.top = new FormAttachment(100, -53);
106                 fd_btnBrowse.bottom = new FormAttachment(100, -10);
107                 btnBrowse.setLayoutData(fd_btnBrowse);
108                 btnBrowse.setText("Browse ...");
109                 
110                 btnBrowse.addSelectionListener( new SelectionAdapter() {
111                         public void widgetSelected(SelectionEvent e) {
112                                 FileDialog fd = new FileDialog(shell, SWT.SAVE );
113                                 String[] ext = { "*.fkb", "*.*"  };
114                                 String[] extN = { "FinalKey Backup Files", "All Files"};
115                                 fd.setFilterExtensions( ext );
116                                 fd.setFilterNames(extN);
117                                 
118                                 fileName = fd.open();
119                                 System.out.println("FileName:" +fileName);
120                                 backupFile = new File(fileName);
121                                 if( !backupFile.exists() && !backupFile.isDirectory())
122                                 { 
123                                         try {
124                                                 backupFile.createNewFile();
125                                                 
126                                                 MessageBox m = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
127                                                 m.setText("Ready to take backup?");
128                                                 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 ?");
129                                                 
130                                                 if( m.open() == SWT.YES )
131                                                 {
132                                                         FkManager.getInstance().backup(backupFile, mySelf);
133                                                         
134                                                 } else {
135                                                         backupFile.delete();
136                                                 }
137
138                                         } catch (IOException e1) {
139                                                 MessageBox m = new MessageBox(shell, SWT.ERROR);
140                                                 m.setText("Error");
141                                                 m.setMessage("Could not create the file, do you have permissions to write there? Is there enough free space?");
142                                                 m.open();
143                                                 fileName=null;
144                                         }
145                                 } else {
146                                         MessageBox m = new MessageBox(shell, SWT.ERROR);
147                                         m.setText("Error");
148                                         m.setMessage("Will not overwrite an existing file, select another file-name or delete the existing file first.");
149                                         m.open();
150                                         fileName=null;
151                                 }
152                                 
153                                 System.out.println(fileName);
154                         }
155                         
156                 });
157                 
158                 Label lblPressbrowseAnd = new Label(cmpBackupFileSelect, SWT.NONE);
159                 fd_btnBrowse.left = new FormAttachment(0, 408);
160                 FormData fd_lblPressbrowseAnd = new FormData();
161                 fd_lblPressbrowseAnd.right = new FormAttachment(btnBrowse, -6);
162                 fd_lblPressbrowseAnd.bottom = new FormAttachment(100, -10);
163                 fd_lblPressbrowseAnd.left = new FormAttachment(0, 10);
164                 fd_lblPressbrowseAnd.top = new FormAttachment(100, -77);
165                 lblPressbrowseAnd.setLayoutData(fd_lblPressbrowseAnd);
166                 lblPressbrowseAnd.setText("Press \"Browse\" to select where to save the backup.");
167
168         }
169
170         @Override
171         public void fkActionEvent(FkActionEvent event) {
172                 MessageBox m;
173                 System.out.println(event);
174                 
175                 //Handle errors differently when we are taking bacup
176                 if( !restoreBackup )
177                 {
178                         switch( event.type )
179                         {
180                         case ACTION_ABORTED:
181                                 break;
182                         case ACTION_ERROR:
183                                 m = new MessageBox(shell, SWT.ICON_ERROR| SWT.YES| SWT.NO);
184                                 m.setText("Checksum Error");
185                                 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 ?");
186                                 if( m.open() == SWT.YES )
187                                 {
188                                         backupFile.delete();
189                                 } else {
190                                         backupFile.renameTo( new File(fileName+"-failed"));
191                                         File crashLog = new File(fileName+"-errorlog.txt");
192                                         FileOutputStream fout;
193                                         try {
194                                                 fout = new FileOutputStream( crashLog );
195                                                 fout.write( event.data.getBytes() );
196                                                 fout.close();
197                                         } catch (Exception e) {
198                                                 e.printStackTrace();
199                                         }
200
201                                 }
202                                 shell.close();
203                                 
204                                 break;
205                         case ACTION_OKAY:
206                                 //TODO:
207                                 m=new MessageBox(shell, SWT.ICON_INFORMATION);
208                                 m.setText("Backup successful");
209                                 m.setMessage("Your backup was verified and saved.");
210                                 m.open();
211                                 shell.close();
212                                 break;
213                         case ACTION_WAITING:
214                                 //TODO:
215                                 //Make visible icon
216                                 //Write text "waiting for button press"
217                                 break;
218                         case ACTION_WORKING:
219                                 //TODO:
220                                 //Switch to status-view (writing)
221                                 cmpBackupFileSelect.dispose();
222                                 break;
223                         case PROGRESS_UPDATE:
224                                 
225                                 break;
226                         case STATE_ERROR:
227                                 break;
228                         case UNEXPECTED_ACTION_RESULT_ERROR:
229                                 break;
230                         default:
231                                 break;
232                         
233                         }
234                 }
235                 
236         }
237
238 }