import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import java.util.regex.Pattern;
public class SystemClean
{
public static final int UI_Width = 600;
public static final String ActionCommand_AddNewCleanEntry="Add New Clean Entry...";
public static final String ActionCommand_StartAllCleanTask="Start System Clean";
public static final String ActionCommand_StopAllCleanTask="Stop";
public static final String ActionCommand_ResetEntryToDefault="Reset Default Entry";
//public static final String ActionCommand_SaveEntryList="Save Entry List";
public static final String ActionCommand_MsgboxOK="OK";
public static final String ActionCommand_AddEntry="Done";
public static final String ActionCommand_DelEntry="Delete This Entry";
public static final String ActionCommand_EditEntry="Edit This Entry";
public static final String CleanType_1="Duplicate Files";
public static final String CleanType_2="Specified File in Folder";
public static final String Filesystem_EntryFolder="../entries/";
public static final String Filesystem_EntryExt=".ce";
public static final String Filesystem_CacheFolder="../cache/";
private static final Dimension Screen_Bounds = Toolkit.getDefaultToolkit().getScreenSize();
public static Frame mainWindow = new Frame();
public static Dialog msgBox = new Dialog(mainWindow, "", true);
public static Label msgLabel1 = new Label();
public static Label msgLabel2 = new Label();
public static OperationAdapter operater = new OperationAdapter();
public static ScrollPane scrollpane = new ScrollPane();
public static Panel cleanEntryContainer = new Panel();
public static int cleanEntryCount=0, runningEntryCount=0;
public static Button startTaskBtn = new Button(ActionCommand_StartAllCleanTask);
public static Button newEntryBtn = new Button(ActionCommand_AddNewCleanEntry);
public static Button resetDefaultEntryBtn = new Button(ActionCommand_ResetEntryToDefault);
public static Button addEntryBtn = new Button(ActionCommand_AddEntry);
public static CleanEntrySetupWindow cleanEntrySetupWindow = new CleanEntrySetupWindow();
public static boolean inScanning=false;
public static void main(String[] args)
{
// Main Window
mainWindow = new Frame();
mainWindow.setSize(UI_Width, 400);
mainWindow.setLayout(null);
mainWindow.setLocation((Screen_Bounds.width-UI_Width)/2, (Screen_Bounds.height-400)/2);
mainWindow.setResizable(false);
mainWindow.setTitle("System Cleaner - By Wafly");
mainWindow.addWindowListener(operater);
// Entry Container
cleanEntryContainer.setLocation(0,0);
cleanEntryContainer.setLayout(null);
scrollpane.setSize(UI_Width-20, 270);
scrollpane.setLocation(10, 80);
scrollpane.add(cleanEntryContainer);
mainWindow.add(scrollpane);
// Btn for Add New
newEntryBtn.setSize(150, 20);
newEntryBtn.setLocation(15, 50);
newEntryBtn.addActionListener(operater);
mainWindow.add(newEntryBtn);
// Btn for Reset Default
resetDefaultEntryBtn.setSize(150, 20);
resetDefaultEntryBtn.setLocation(180, 50);
resetDefaultEntryBtn.addActionListener(operater);
mainWindow.add(resetDefaultEntryBtn);
// Btn for Start/Stop
startTaskBtn.setSize(150, 20);
startTaskBtn.setLocation(UI_Width-165, 360);
startTaskBtn.addActionListener(operater);
mainWindow.add(startTaskBtn);
// Message Box
msgBox.setLayout(null);
msgBox.setSize(300, 200);
msgBox.setLocation((Screen_Bounds.width-300)/2, (Screen_Bounds.height-200)/2);
msgBox.addWindowListener(operater);
msgLabel1.setSize(300, 50);
msgLabel1.setLocation(0, 50);
msgLabel1.setAlignment(Label.CENTER);
msgLabel2.setSize(300, 50);
msgLabel2.setLocation(0, 100);
msgLabel2.setAlignment(Label.CENTER);
msgBox.add(msgLabel1);
msgBox.add(msgLabel2);
Button msgOKbtn = new Button(ActionCommand_MsgboxOK);
msgOKbtn.setSize(100, 20);
msgOKbtn.setLocation(100, 165);
msgOKbtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {
msgBox.setVisible(false);
msgBox.dispose();
}});
msgBox.add(msgOKbtn);
mainWindow.setVisible(true);
scrollpane.requestFocus();
File entryFolder = new File(Filesystem_EntryFolder);
if(!entryFolder.exists()&&!entryFolder.mkdirs())
showMsg("Fail to create entry store folder\nEntry(s) can not be saved.");
File cacheFolder = new File(Filesystem_CacheFolder);
if(!cacheFolder.exists()&&!cacheFolder.mkdirs())
showMsg("Fail to create cache folder\nEntry task can not process.");
else
{
File[] entrys = entryFolder.listFiles();
for(int i=0; i<entrys.length; i++)
{
Properties p = new Properties();
FileInputStream epfis = null;
try {
epfis = new FileInputStream(entrys[i]);
p.load(epfis);
String name = p.getProperty("NAME").trim();
if(name.equals(""))
continue;
int type = Integer.parseInt(p.getProperty("TYPE").trim());
if(type!=1&&type!=2)
continue;
String fileName = p.getProperty("FILE").trim();
String path = p.getProperty("PATH").trim();
String dateStr = p.getProperty("DATE");
Date date = null;
if(dateStr!=null)
{
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr.trim());
} catch (Exception e) { }
}
String enableStr = p.getProperty("DISABLE");
boolean enable=false;
if(enableStr==null || enableStr.equalsIgnoreCase("false"))
enable=true;
addCleanEntry(new CleanEntry(name, type, fileName, path, date, enable), p.getProperty("ENTRYFILE"));
} catch (Exception e1) {
} finally {
try {
if(epfis!=null)
epfis.close();
} catch (Exception e1) { }
}
}
}
}
private static void addCleanEntry(CleanEntry entry, String filename)
{
entry.setLocation(0, cleanEntryCount*entry.getHeight());
entry.setBackground(new Color((int)(Math.random()*0xffffff)));
if(filename==null)
entry.saveToFile();
else
entry.entryFileName=filename;
cleanEntryContainer.add(entry);
reorderEntrys();
}
private static void reorderEntrys()
{
Component[] entrys = cleanEntryContainer.getComponents();
cleanEntryCount=0;
for(;cleanEntryCount<entrys.length; cleanEntryCount++)
{
entrys[cleanEntryCount].setLocation(0, cleanEntryCount*entrys[cleanEntryCount].getHeight());
}
if(cleanEntryCount>0)
cleanEntryContainer.setPreferredSize(new Dimension(UI_Width-50, cleanEntryCount*entrys[0].getHeight()));
scrollpane.validate();
}
private static void showMsg(String msg)
{
String[] msgs = msg.split("\n");
msgLabel1.setText(msgs[0]);
if(msgs.length>1)
msgLabel2.setText(msgs[1]);
else
msgLabel2.setText("");
msgBox.setVisible(true);
msgBox.requestFocus();
}
private static String getMD5(File file)
{
try {
MessageDigest messagedigest = MessageDigest.getInstance("MD5");
InputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int numRead = 0;
while ((numRead = fis.read(buffer)) > 0)
messagedigest.update(buffer, 0, numRead);
fis.close();
byte bytes[] = messagedigest.digest();
StringBuffer stringbuffer = new StringBuffer(2 * bytes.length);
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
int k = bytes.length;
for (int l = 0; l < k; l++)
{
stringbuffer.append(hexDigits[(bytes[l] & 0xf0) >> 4]);
stringbuffer.append(hexDigits[bytes[l] & 0xf]);
}
return stringbuffer.toString();
} catch (Exception e) {
return null;
}
}
//======================================================================================================================//
//======================================================================================================================//
private static class CleanEntrySetupWindow extends Dialog
{
private static final long serialVersionUID = 1L;
private static TextField nameText = new TextField();
private static Choice entryTypeChoice = new Choice();
private static TextField fileNameText = new TextField();
private static TextField pathText = new TextField();
private static TextField dateText = new TextField();
private static CleanEntry entry=null;
public CleanEntrySetupWindow()
{
super(mainWindow, "System Cleaner - Clean Entry Setup - By Wafly", true);
setSize(400, 300);
setLayout(null);
setLocation((Screen_Bounds.width-400)/2, (Screen_Bounds.height-300)/2);
setResizable(false);
addWindowListener(operater);
Label entryNameLabel = new Label("Name:");
entryNameLabel.setSize(50, 20);
entryNameLabel.setLocation(20, 50);
add(entryNameLabel);
nameText.setSize(310, 20);
nameText.setLocation(70, 50);
add(nameText);
Label entryTypeLabel = new Label("Type:");
entryTypeLabel.setSize(50, 20);
entryTypeLabel.setLocation(20, 80);
add(entryTypeLabel);
entryTypeChoice.setSize(150, 20);
entryTypeChoice.setLocation(70, 80);
entryTypeChoice.add("");
entryTypeChoice.add(CleanType_1);
entryTypeChoice.add(CleanType_2);
add(entryTypeChoice);
Label fileNameLabel = new Label("File Name:");
fileNameLabel.setSize(100, 20);
fileNameLabel.setLocation(20, 150);
add(fileNameLabel);
fileNameText.setSize(260, 20);
fileNameText.setLocation(120, 150);
add(fileNameText);
Label pathLabel = new Label("Path:");
pathLabel.setSize(100, 20);
pathLabel.setLocation(20, 180);
add(pathLabel);
pathText.setSize(260, 20);
pathText.setLocation(120, 180);
add(pathText);
Label dateLabel = new Label("Date(yyyy-MM-dd):");
dateLabel.setSize(100, 20);
dateLabel.setLocation(20, 210);
add(dateLabel);
dateText.setSize(260, 20);
dateText.setLocation(120, 210);
add(dateText);
addEntryBtn.setSize(100, 20);
addEntryBtn.setLocation(280, 260);
addEntryBtn.addActionListener(operater);
add(addEntryBtn);
}
public static boolean addEntry()
{
String name = CleanEntrySetupWindow.nameText.getText().trim();
if(name.equals(""))
{
showMsg("Entry name can not be empty.");
return false;
}
int type = CleanEntrySetupWindow.entryTypeChoice.getSelectedIndex();
if(type==0)
{
showMsg("Clean type can not be empty.");
return false;
}
String file = CleanEntrySetupWindow.fileNameText.getText().trim();
if(file.equals(""))
file="*";
String path = CleanEntrySetupWindow.pathText.getText().trim();
if(path.equals("")||path.equals("*"))
path="*";
else
{
File targetDirectry = new File(path);
if(!targetDirectry.exists()||!targetDirectry.isDirectory())
{
showMsg("Invalid target path.");
return false;
}
}
Date date = null;
if(!CleanEntrySetupWindow.dateText.getText().equals(""))
{
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(CleanEntrySetupWindow.dateText.getText());
} catch (Exception e) {
showMsg("Date format error, should be (yyyy-MM-dd HH:mm:ss)\nPlease input such as \"1985-05-15 22:05:30\".");
return false;
}
}
addCleanEntry(new CleanEntry(name, type, file, path, date, true), null);
return true;
}
public static void forUpdate(CleanEntry entry)
{
cleanEntrySetupWindow.setVisible(true);
cleanEntrySetupWindow.requestFocus();
CleanEntrySetupWindow.entry=entry;
}
public static boolean isUpdate()
{
if(entry==null)
return false;
String name = CleanEntrySetupWindow.nameText.getText().trim();
if(name.equals(""))
{
showMsg("Entry name can not be empty.");
return true;
}
int type = CleanEntrySetupWindow.entryTypeChoice.getSelectedIndex();
if(type==0)
{
showMsg("Clean type can not be empty.");
return true;
}
String file = CleanEntrySetupWindow.fileNameText.getText().trim();
if(file.equals(""))
file="*";
String path = CleanEntrySetupWindow.pathText.getText().trim();
if(path.equals("")||path.equals("*"))
path="*";
else
{
File targetDirectry = new File(path);
if(!targetDirectry.exists()||!targetDirectry.isDirectory())
{
showMsg("Invalid target path.");
return true;
}
}
Date date = null;
if(!CleanEntrySetupWindow.dateText.getText().equals(""))
{
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(CleanEntrySetupWindow.dateText.getText());
} catch (Exception e) {
showMsg("Date format error, should be (yyyy-MM-dd HH:mm:ss)\nPlease input such as \"1985-05-15 22:05:30\".");
return true;
}
}
entry.name=name;
entry.type=type;
switch(type)
{
case 1:
entry.name_type_Label.setText("["+CleanType_1+"] "+name);
break;
case 2:
entry.name_type_Label.setText("["+CleanType_2+"] "+name);
break;
default:
entry.name_type_Label.setText("[Unknow Type] "+name);
break;
}
entry.fileName=file;
entry.fileLabel.setText("File Name: "+file);
entry.path=path;
entry.pathLabel.setText("Path: "+(path.equals("*")?"Whole System Disk":path));
entry.date=date;
entry.dateLabel.setText(date==null?"No Date Limit.":"Before "+date.toString());
Properties entryProp = new Properties();
entryProp.put("NAME", name);
entryProp.put("TYPE", String.valueOf(type));
entryProp.put("FILE", file);
entryProp.put("PATH", path);
entryProp.put("ENTRYFILE", entry.entryFileName);
if(date!=null)
entryProp.put("DATE", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
File ceFile = new File(Filesystem_EntryFolder+entry.entryFileName+Filesystem_EntryExt);
ceFile.delete();
FileOutputStream epfos = null;
try {
epfos = new FileOutputStream(ceFile);
entryProp.store(epfos,null);
} catch (Exception e) {
showMsg("Fail to save entry\n"+e.getMessage());
} finally {
try {
if(epfos!=null)
epfos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
entry=null;
return true;
}
}
private static class CleanEntry extends Panel
{
private static final long serialVersionUID = 1L;
private Color foreColor;
private Label name_type_Label;
private Label fileLabel;
private Label pathLabel;
private Label dateLabel;
private String name;
private int type;
private String fileName;
private String path;
private Date date;
private String entryFileName;
Button detailsBtn = new Button("View Scan Result");
Button selectedBtn;
private int totalFileCount=0;
private int matchedFileCount=0;
public boolean isEnable=true;
public CleanEntry(String name, int type, String fileName, String path, Date date, boolean isEnable)
{
this.name=name;
this.type=type;
this.fileName=fileName;
this.path=path;
this.date=date;
this.isEnable=isEnable;
setSize(UI_Width, 110);
setLayout(null);
switch(type)
{
case 1:
name_type_Label = new Label("["+CleanType_1+"] "+name);
break;
case 2:
name_type_Label = new Label("["+CleanType_2+"] "+name);
break;
default:
name_type_Label = new Label("[Unknow Type] "+name);
break;
}
name_type_Label.setSize(350, 20);
name_type_Label.setLocation(60, 5);
add(name_type_Label);
fileLabel = new Label("File Name: "+fileName);
fileLabel.setSize(350, 20);
fileLabel.setLocation(60, 30);
add(fileLabel);
pathLabel = new Label("Path: "+(path.equals("*")?"Whole System Disk":path));
pathLabel.setSize(350, 20);
pathLabel.setLocation(60, 55);
add(pathLabel);
dateLabel = new Label(date==null?"No Date Limit.":"Before "+date.toString());
dateLabel.setSize(350, 20);
dateLabel.setLocation(60, 80);
add(dateLabel);
Button delBtn = new Button(ActionCommand_DelEntry);
delBtn.setSize(140, 20);
delBtn.setLocation(410, 5);
delBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
cleanEntryContainer.remove(CleanEntry.this);
new File(Filesystem_EntryFolder+entryFileName+Filesystem_EntryExt).delete();
new File(Filesystem_CacheFolder+entryFileName).delete();
reorderEntrys();
}});
add(delBtn);
Button editBtn = new Button(ActionCommand_EditEntry);
editBtn.setSize(140, 20);
editBtn.setLocation(410, 30);
editBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
CleanEntrySetupWindow.nameText.setText(CleanEntry.this.name);
CleanEntrySetupWindow.entryTypeChoice.select(CleanEntry.this.type);
CleanEntrySetupWindow.fileNameText.setText(CleanEntry.this.fileName);
CleanEntrySetupWindow.pathText.setText(CleanEntry.this.path);
if(CleanEntry.this.date!=null)
CleanEntrySetupWindow.dateText.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(CleanEntry.this.date));
CleanEntrySetupWindow.forUpdate(CleanEntry.this);
}});
add(editBtn);
if(this.isEnable)
selectedBtn=new Button("Disable");
else
selectedBtn=new Button("Enable");
selectedBtn.setSize(45, 100);
selectedBtn.setLocation(10, 5);
selectedBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(Filesystem_EntryFolder+entryFileName+Filesystem_EntryExt);
Properties ep = new Properties();
ep.load(fis);
fis.close();
ep.setProperty("DISABLE", String.valueOf(CleanEntry.this.isEnable));
fos = new FileOutputStream(Filesystem_EntryFolder+entryFileName+Filesystem_EntryExt);
ep.store(fos,null);
fos.close();
} catch(Exception ex1){
showMsg("Fail to store status\n"+ex1.getMessage());
}
if(CleanEntry.this.isEnable)
{
CleanEntry.this.isEnable=false;
selectedBtn.setLabel("Enable");
}
else
{
selectedBtn.setLabel("Disable");
CleanEntry.this.isEnable=true;
}
}});
add(selectedBtn);
detailsBtn.setSize(140, 20);
detailsBtn.setLocation(410, 80);
detailsBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
final Dialog detailsWIndow = new Dialog(mainWindow, "System Cleaner - Result Details - By Wafly", true);
detailsWIndow.setSize(mainWindow.getSize());
detailsWIndow.setLocation(mainWindow.getLocation());
detailsWIndow.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e) {
detailsWIndow.setVisible(false);
detailsWIndow.dispose();
}});
detailsWIndow.setLayout(null);
final TextArea resultTA = new TextArea();
resultTA.setSize(detailsWIndow.getWidth()-20, detailsWIndow.getHeight()-100);
resultTA.setLocation(10, 50);
try {
FileReader fr = new FileReader(Filesystem_CacheFolder+CleanEntry.this.entryFileName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
resultTA.setText(line);
while((line=br.readLine())!=null)
resultTA.setText(resultTA.getText()+"\r\n"+line);
br.close();
fr.close();
} catch (Exception e1) {
showMsg("Fail to load scan details.\n"+e1.getMessage());
}
detailsWIndow.add(resultTA);
Button saveBtn = new Button("Save");
saveBtn.setSize(200, 20);
saveBtn.setLocation(detailsWIndow.getWidth()-220, detailsWIndow.getHeight()-40);
saveBtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {
try {
FileWriter fw = new FileWriter(Filesystem_CacheFolder+CleanEntry.this.entryFileName);
fw.write(resultTA.getText());
fw.close();
} catch (Exception e1) {
showMsg("Fail to save scan details.\n"+e1.getMessage());
}
detailsWIndow.setVisible(false);
detailsWIndow.dispose();
}});
detailsWIndow.add(saveBtn);
detailsWIndow.setVisible(true);
}});
add(detailsBtn);
}
public void setBackground(Color color)
{
super.setBackground(color);
foreColor = new Color(0xffffff-getBackground().getRGB());
name_type_Label.setForeground(foreColor);
fileLabel.setForeground(foreColor);
pathLabel.setForeground(foreColor);
dateLabel.setForeground(foreColor);
}
public void saveToFile()
{
Properties entryProp = new Properties();
entryProp.put("NAME", name);
entryProp.put("TYPE", String.valueOf(type));
entryProp.put("FILE", fileName);
entryProp.put("PATH", path);
if(date!=null)
entryProp.put("DATE", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
entryFileName=String.valueOf((int)(Math.random()*65535));
entryProp.put("ENTRYFILE", entryFileName);
FileOutputStream epfos = null;
try {
epfos = new FileOutputStream(Filesystem_EntryFolder+entryFileName+Filesystem_EntryExt);
entryProp.store(epfos,null);
} catch (Exception e) {
showMsg("Fail to save entry\n"+e.getMessage());
} finally {
try {
if(epfos!=null)
epfos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void addFileCount()
{
totalFileCount++;
detailsBtn.setLabel("Result: "+matchedFileCount+"/"+totalFileCount);
}
public void addMatchedFileCount()
{
matchedFileCount++;
detailsBtn.setLabel("Result: "+matchedFileCount+"/"+totalFileCount);
}
public void resetDefault()
{
detailsBtn.setLabel("View Scan Result");
}
}
private static class OperationAdapter implements ActionListener,WindowListener
{
@Override
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if(source.equals(newEntryBtn))
{
cleanEntrySetupWindow.setVisible(true);
cleanEntrySetupWindow.requestFocus();
}
else if(source.equals(resetDefaultEntryBtn))
{
showMsg("Reset to default entrys.");
}
else if(source.equals(addEntryBtn))
{
if(CleanEntrySetupWindow.isUpdate())
{
cleanEntrySetupWindow.setVisible(false);
cleanEntrySetupWindow.dispose();
}
else if(CleanEntrySetupWindow.addEntry())
{
cleanEntrySetupWindow.setVisible(false);
cleanEntrySetupWindow.dispose();
}
}
else if(source.equals(startTaskBtn))
{
if(startTaskBtn.getLabel().equals(ActionCommand_StartAllCleanTask))
{
startTaskBtn.setLabel(ActionCommand_StopAllCleanTask);
newEntryBtn.setEnabled(false);
resetDefaultEntryBtn.setEnabled(false);
addEntryBtn.setEnabled(false);
Component[] cs = cleanEntryContainer.getComponents();
inScanning=true;
ArrayList<Thread> taskThreads = new ArrayList<Thread>();
for(int i=0; i<cs.length; i++)
{
final CleanEntry entry = (CleanEntry)cs[i];
entry.setEnabled(false);
if(entry.isEnable)
{
runningEntryCount++;
taskThreads.add(new Thread(){public void run(){
cleanTesk(entry);
runningEntryCount--;
if(runningEntryCount<=0)
{
startTaskBtn.setLabel(ActionCommand_StartAllCleanTask);
newEntryBtn.setEnabled(true);
resetDefaultEntryBtn.setEnabled(true);
addEntryBtn.setEnabled(true);
Component[] cs = cleanEntryContainer.getComponents();
for(int i=0; i<cs.length; i++)
cs[i].setEnabled(true);
}
}});
}
}
runningEntryCount=taskThreads.size();
for(int i=0; i<taskThreads.size(); i++)
taskThreads.get(i).start();
}
else
{
startTaskBtn.setEnabled(false);
inScanning=false;
}
}
}
@Override
public void windowClosing(WindowEvent e) {
if(e.getSource().equals(mainWindow))
{
System.exit(0);
}
else if(e.getSource().equals(cleanEntrySetupWindow))
{
cleanEntrySetupWindow.setVisible(false);
cleanEntrySetupWindow.dispose();
}
else if(e.getSource().equals(msgBox))
{
msgBox.setVisible(false);
msgBox.dispose();
}
}
@Override
public void windowDeactivated(WindowEvent e) { }
@Override
public void windowDeiconified(WindowEvent e) { }
@Override
public void windowIconified(WindowEvent e) { }
@Override
public void windowOpened(WindowEvent e) { }
@Override
public void windowActivated(WindowEvent e) { };
@Override
public void windowClosed(WindowEvent e) { }
}
public static void cleanTesk(CleanEntry entry)
{
File[] roots;
if(entry.path.equals("*"))
roots = File.listRoots();
else
roots = new File[]{new File(entry.path)};
if(entry.type==1)
{
HashMap<String, String> allFileList = new HashMap<String, String>();
Properties result = new Properties();
String compdresult = entry.fileName
.replace(".?", "?").replace("?", ".?")
.replace(".*", "*").replace("*", ".*");
Pattern fileNamePattern = Pattern.compile(compdresult);
for(int i=0; i<roots.length; i++)
{
getDuplicateFiles(entry, roots[i], fileNamePattern, result, allFileList);
}
allFileList.clear();
try{
FileOutputStream fos = new FileOutputStream(Filesystem_CacheFolder+entry.entryFileName);
result.store(fos, "");
fos.close();
result.clear();
}catch(Exception e){
showMsg("Fail to percess the entry ["+entry.name+"]\n"+e.getMessage());
e.printStackTrace();
}
}
else if(entry.type==2)
{
BufferedWriter bw = null;
try {
File cacheFile = new File(Filesystem_CacheFolder+entry.entryFileName);
if(cacheFile.exists())
cacheFile.delete();
cacheFile.createNewFile();
bw = new BufferedWriter(new FileWriter(Filesystem_CacheFolder+entry.entryFileName));
bw.write("## This is the cache for store the entry scan result.");
bw.newLine();
String result = entry.fileName
.replace(".?", "?").replace("?", ".?")
.replace(".*", "*").replace("*", ".*");
Pattern fileNamePattern = Pattern.compile(result);
for(int i=0; i<roots.length; i++)
{
getFiles(entry, roots[i], fileNamePattern, bw);
}
} catch (Exception e) {
showMsg("Fail to percess the entry ["+entry.name+"]\n"+e.getMessage());
e.printStackTrace();
} finally {
try {
if(bw!=null)
bw.close();
} catch (Exception e) { }
}
}
entry.resetDefault();
}
private static void getFiles(CleanEntry entry, File folder, Pattern filePattern, BufferedWriter bw)
{
File[] files = folder.listFiles();
if(files==null)
return;
for(int i=0; i<files.length&&inScanning; i++)
{
if(files[i].isDirectory())
{
getFiles(entry, files[i], filePattern, bw);
}
else
{
entry.addFileCount();
if(filePattern.matcher(files[i].getName()).matches())
{
if(entry.date!=null && new Date(files[i].lastModified()).compareTo(entry.date)>0)
continue;
try {
bw.write(files[i].getAbsolutePath());
bw.newLine();
entry.addMatchedFileCount();
} catch (Exception e) { }
}
}
}
}
private static void getDuplicateFiles(CleanEntry entry, File folder, Pattern filePattern, Properties result, HashMap<String, String> allFileList)
{
File[] files = folder.listFiles();
if(files==null)
return;
for(int i=0; i<files.length&&inScanning; i++)
{
if(files[i].isDirectory())
{
getDuplicateFiles(entry, files[i], filePattern, result, allFileList);
}
else
{
entry.addFileCount();
if(filePattern.matcher(files[i].getName()).matches())
{
if(entry.date!=null && new Date(files[i].lastModified()).compareTo(entry.date)>0)
continue;
String md5sum = getMD5(files[i]);
if(allFileList.containsKey(md5sum))
{
String initFile = result.getProperty(md5sum);
if(initFile==null)
result.setProperty(md5sum, allFileList.get(md5sum)+";"+files[i].getAbsolutePath());
else
result.setProperty(md5sum, initFile+";"+files[i].getAbsolutePath());
entry.addMatchedFileCount();
}
else
allFileList.put(md5sum, files[i].getAbsolutePath());
}
}
}
}
}