Dépôt officiel du code source de l'ERP OpenConcerto
Blame | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.config;
import org.openconcerto.utils.Base64;
import org.openconcerto.utils.FileUtils;
import org.openconcerto.utils.JImage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
public class NewsChecker {
public static class DateHash {
Long t;
String h;
public DateHash(Long time, String hash) {
this.t = time;
this.h = hash;
}
}
public static void check(Properties pTOS, File confDir, boolean useH2) {
if (!confDir.exists()) {
System.out.println("missing conf dir " + confDir.getAbsolutePath());
}
final boolean hasCloud = pTOS.getProperty("hasCloud", "false").equals("true");
final int launchCounter = Integer.parseInt(pTOS.getProperty("launchCounter", "0"));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
File historyFile = new File(confDir, "history.log");
if (!historyFile.exists()) {
FileUtils.writeUTF8("", historyFile);
}
List<DateHash> history = new ArrayList<>();
long today = System.currentTimeMillis();
// 60 days
long maxDate = today - 60 * 24 * 3600 * 1000;
try (BufferedReader reader = new BufferedReader(new FileReader(historyFile))) {
String line = reader.readLine();
while (line != null) {
Long t = Long.parseLong(line);
String h = reader.readLine();
if (t > maxDate) {
history.add(new DateHash(t, h));
}
line = reader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
byte[] data = download("https://www.ilm-informatique.fr/openconcerto/ocnews");
MessageDigest digester = MessageDigest.getInstance("SHA-256");
digester.update(data);
String h = Base64.encodeBytes(digester.digest());
boolean found = false;
for (DateHash d : history) {
if (d.h.equals(h)) {
found = true;
break;
}
}
DateHash newLine = new DateHash(System.currentTimeMillis(), h);
history.add(newLine);
if (!found) {
final JSONParser jsonParser = new JSONParser(JSONParser.MODE_PERMISSIVE);
final String jsonString = new String(data, StandardCharsets.UTF_8);
JSONObject obj = (JSONObject) jsonParser.parse(jsonString);
String title = obj.getOrDefault("title", "").toString();
String text = obj.getOrDefault("text", "").toString();
String imageURL = obj.getOrDefault("image", "").toString();
String end = obj.getOrDefault("end", "1/12/2133").toString();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateEnd = df.parse(end);
boolean includeCloud = (Boolean) obj.getOrDefault("include-cloud", Boolean.FALSE);
boolean includeMono = (Boolean) obj.getOrDefault("include-mono", Boolean.FALSE);
boolean includeMulti = (Boolean) obj.getOrDefault("include-multi", Boolean.FALSE);
boolean isMono = useH2;
boolean isMulti = !hasCloud && !isMono;
int minLaunch = (Integer) obj.getOrDefault("min-launch", 0);
if (launchCounter < minLaunch) {
System.out.println("launch counter too small (" + launchCounter + ")");
} else {
if (dateEnd.before(new Date())) {
System.out.println("outdated news");
} else {
if (includeCloud && hasCloud || includeMono && isMono || includeMulti && isMulti) {
BufferedImage image = null;
if (imageURL != null && imageURL.length() > 5) {
try {
byte[] imageData = download(imageURL);
image = ImageIO.read(new ByteArrayInputStream(imageData));
} catch (Exception e) {
e.printStackTrace();
}
}
showFrame(title, text, image, history, historyFile);
} else {
System.out.println("news not relevant");
}
}
}
} else {
System.out.println("news already displayed");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private byte[] download(String u) throws MalformedURLException, IOException {
URL url = new URL(u);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int length = 0;
while ((length = is.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
is.close();
out.close();
byte[] data = out.toByteArray();
return data;
}
});
t.setPriority(Thread.MIN_PRIORITY);
t.setName("News checker");
t.setDaemon(true);
t.start();
}
protected static void showFrame(String title, String text, BufferedImage image, List<DateHash> history, File historyFile) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setIconImages(Gestion.getFrameIcon());
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
if (title != null && !title.isEmpty()) {
f.setTitle(title);
} else {
f.setTitle("OpenConcerto");
}
JPanel p = new JPanel();
p.setBackground(Color.WHITE);
p.setLayout(new BorderLayout());
if (image != null) {
p.add(new JImage(image), BorderLayout.PAGE_START);
}
if (text != null && !text.isEmpty()) {
final String t = text.replace("\\n", "\n");
final JTextArea ta = new JTextArea(t);
ta.setBorder(BorderFactory.createLineBorder(Color.WHITE, 10));
Font font = new JTextField("hello").getFont();
font = font.deriveFont(font.getSize() + 2f);
ta.setFont(font);
ta.setEditable(false);
p.add(ta, BorderLayout.CENTER);
}
final JButton bOk = new JButton();
bOk.setEnabled(false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 10; i > 0; i--) {
final int c = i;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
bOk.setText(String.valueOf(c) + "s");
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Nothing
}
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
bOk.setText("OK");
bOk.setEnabled(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
});
}
});
t.start();
bOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// save history
if (history != null) {
try (FileOutputStream fOp = new FileOutputStream(historyFile)) {
PrintStream prt = new PrintStream(new BufferedOutputStream(fOp));
for (DateHash d : history) {
prt.println(d.t);
prt.println(d.h);
}
prt.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(historyFile.getAbsolutePath() + " saved");
}
// close
f.dispose();
}
});
final FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
final JPanel pButtons = new JPanel();
pButtons.setLayout(flowLayout);
pButtons.setOpaque(false);
pButtons.add(bOk);
p.add(pButtons, BorderLayout.PAGE_END);
f.setContentPane(p);
f.pack();
f.setMinimumSize(new Dimension(300, 400));
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
showFrame("Hello", "world\\nhhhf the window is enlarged or the center and the bottom.", null, null, null);
// showFrame("Hello", "world\\nhhh",
// ImageIO.read(this.getClass().getResourceAsStream("256.png")), null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}