Siba/src/org/dclermonte/siba/gui/CheckPanel.java

212 lines
5.9 KiB
Java
Raw Normal View History

2016-04-19 22:34:25 +02:00
/*
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
* Didier Clermonté <dclermonte@april.org>
*
* This file is part of Siba.
*
* Siba is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2016-03-29 23:25:18 +02:00
package org.dclermonte.siba.gui;
2016-04-09 11:33:15 +02:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2016-03-29 23:25:18 +02:00
import java.io.File;
2016-04-07 00:54:03 +02:00
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
2016-04-09 11:33:15 +02:00
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
2016-03-29 23:25:18 +02:00
import com.jgoodies.forms.layout.ColumnSpec;
2016-04-09 11:33:15 +02:00
import com.jgoodies.forms.layout.FormLayout;
2016-03-29 23:25:18 +02:00
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;
2016-04-09 11:33:15 +02:00
/**
*
* @author papa
*/
2016-03-29 23:25:18 +02:00
public class CheckPanel extends JPanel
{
2016-04-09 11:33:15 +02:00
private static final long serialVersionUID = -1580485684838045920L;
2016-03-29 23:25:18 +02:00
private JTextField textField;
2016-04-12 00:41:43 +02:00
private File choosenFile;
2016-03-29 23:25:18 +02:00
/**
* Create the panel.
*/
public CheckPanel()
{
2016-04-09 11:33:15 +02:00
setLayout(new FormLayout(
new ColumnSpec[] { ColumnSpec.decode("2dlu"),
new ColumnSpec(ColumnSpec.LEFT,
Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true),
Sizes.constant("60dlu", true)),
1),
new ColumnSpec(ColumnSpec.LEFT,
Sizes.bounded(Sizes.PREFERRED, Sizes.constant("50dlu", true),
Sizes.constant("100dlu", true)),
1),
ColumnSpec.decode("30dlu"), },
new RowSpec[] { FormSpecs.LINE_GAP_ROWSPEC, RowSpec.decode("25px"), FormSpecs.RELATED_GAP_ROWSPEC,
FormSpecs.DEFAULT_ROWSPEC, }));
2016-03-29 23:25:18 +02:00
JLabel lblNewLabel = new JLabel("Sauvegarde à vérifier");
add(lblNewLabel, "2, 2, left, center");
2016-04-09 11:33:15 +02:00
2016-03-29 23:25:18 +02:00
JButton btnNewButton = new JButton("...");
2016-04-19 22:34:25 +02:00
btnNewButton.addActionListener(new ActionListener()
2016-04-09 11:33:15 +02:00
{
2016-03-29 23:25:18 +02:00
@Override
2016-04-19 22:34:25 +02:00
public void actionPerformed(final ActionEvent e)
2016-04-09 11:33:15 +02:00
{
2016-04-07 00:54:03 +02:00
try
{
2016-04-19 22:34:25 +02:00
choosenDirectory();
2016-04-07 00:54:03 +02:00
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
2016-03-29 23:25:18 +02:00
}
});
2016-04-09 11:33:15 +02:00
this.textField = new JTextField();
add(this.textField, "3, 2, fill, default");
this.textField.setColumns(10);
2016-03-29 23:25:18 +02:00
add(btnNewButton, "4, 2, left, top");
2016-04-09 11:33:15 +02:00
2016-03-29 23:25:18 +02:00
JButton btnNewButton_1 = new JButton("Check");
2016-04-09 11:33:15 +02:00
btnNewButton_1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent arg0)
{
try
{
check();
}
catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
2016-03-29 23:25:18 +02:00
add(btnNewButton_1, "3, 4");
}
2016-04-09 11:33:15 +02:00
final void check() throws IOException, NoSuchAlgorithmException
{
2016-04-12 00:41:43 +02:00
FileReader fileReader = new FileReader(this.choosenFile);
2016-04-09 11:33:15 +02:00
char[] md5 = new char[32];
byte[] md5byte = new byte[32];
2016-04-12 00:41:43 +02:00
char[] fileToCheck = new char[(int) (this.choosenFile.length()) - 32];
String md5String = new String();
2016-04-09 11:33:15 +02:00
for (int index = 0; index < 32; index++)
{
md5[index] = (char) fileReader.read();
md5byte[index] = (byte) md5[index];
2016-04-12 00:41:43 +02:00
md5String = md5String + md5[index];
2016-04-09 11:33:15 +02:00
}
fileReader.read();
String fileNameToString = new String();
2016-04-19 22:34:25 +02:00
for (int index = 36; index < (this.choosenFile.length() + 2); index++)
2016-04-09 11:33:15 +02:00
{
fileToCheck[index - 36] = (char) fileReader.read();
2016-04-12 00:41:43 +02:00
fileNameToString = fileNameToString + fileToCheck[index - 36];
2016-04-09 11:33:15 +02:00
}
2016-04-12 00:41:43 +02:00
2016-04-09 11:33:15 +02:00
fileReader.close();
2016-04-12 00:41:43 +02:00
File fileToCheck1 = new File(this.choosenFile.getParent() + "/" + fileNameToString);
byte[] bytedirectoryToSave = new byte[(int) fileToCheck1.length()];
FileInputStream fileInputStream = new FileInputStream(fileToCheck1);
2016-04-19 22:34:25 +02:00
for (int index1 = 0; index1 < (fileToCheck1.length()); index1++)
2016-04-09 11:33:15 +02:00
{
bytedirectoryToSave[index1] = (byte) fileInputStream.read();
}
fileInputStream.close();
byte[] hash = null;
2016-04-12 00:41:43 +02:00
StringBuilder hashString = new StringBuilder();
2016-04-09 11:33:15 +02:00
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
hash = messageDigest.digest(bytedirectoryToSave);
2016-04-12 00:41:43 +02:00
for (int index = 0; index < hash.length; index++)
2016-04-09 11:33:15 +02:00
{
2016-04-12 00:41:43 +02:00
String hex = Integer.toHexString(hash[index]);
if (hex.length() == 1)
{
hashString.append('0');
hashString.append(hex.charAt(hex.length() - 1));
}
else
{
hashString.append(hex.substring(hex.length() - 2));
}
}
if (md5String.equals(hashString.toString()))
{
CheckDialog.main("Vérification OK");
2016-04-09 11:33:15 +02:00
}
else
{
2016-04-12 00:41:43 +02:00
CheckDialog.main("Il y a une erreur");
2016-04-09 11:33:15 +02:00
}
}
/**
*
* @throws IOException
* @throws NoSuchAlgorithmException
*/
2016-04-19 22:34:25 +02:00
final void choosenDirectory() throws IOException, NoSuchAlgorithmException
2016-04-09 11:33:15 +02:00
{
2016-03-29 23:25:18 +02:00
final JFileChooser fc = new JFileChooser();
2016-04-09 11:33:15 +02:00
FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichiers md5", "md5");
fc.setAcceptAllFileFilterUsed(false);
2016-04-07 00:54:03 +02:00
fc.addChoosableFileFilter(filter);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
2016-04-09 11:33:15 +02:00
int returnVal = fc.showDialog(CheckPanel.this, "Sauvegarde à vérifier");
if (returnVal == JFileChooser.APPROVE_OPTION)
{
2016-04-12 00:41:43 +02:00
this.choosenFile = fc.getSelectedFile();
this.textField.setText(this.choosenFile.getName());
2016-03-29 23:25:18 +02:00
}
}
2016-04-09 11:33:15 +02:00
}