Author: jflesch
Date: 2007-05-01 05:09:49 +0000 (Tue, 01 May 2007)
New Revision: 13072

Added:
   trunk/apps/Thaw/src/thaw/plugins/TransferLogs.java
Modified:
   trunk/apps/Thaw/src/thaw/core/PluginManager.java
   trunk/apps/Thaw/src/thaw/gui/Table.java
   trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
Log:
Add a plugin logging all the transfers started and/or finished with Thaw

Modified: trunk/apps/Thaw/src/thaw/core/PluginManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-05-01 02:23:07 UTC 
(rev 13071)
+++ trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-05-01 05:09:49 UTC 
(rev 13072)
@@ -16,7 +16,8 @@
                "thaw.plugins.ThemeSelector",
                "thaw.plugins.Hsqldb",
                "thaw.plugins.IndexBrowser",
-               "thaw.plugins.IndexExporter"
+               "thaw.plugins.IndexExporter",
+               "thaw.plugins.TransferLogs"
        };

        private final static String[] knownPlugins = {
@@ -31,6 +32,7 @@
                "thaw.plugins.IndexBrowser",
                "thaw.plugins.IndexExporter",
                "thaw.plugins.Restarter",
+               "thaw.plugins.TransferLogs",
                "thaw.plugins.SqlConsole",
                "thaw.plugins.LogConsole"
        };

Modified: trunk/apps/Thaw/src/thaw/gui/Table.java
===================================================================
--- trunk/apps/Thaw/src/thaw/gui/Table.java     2007-05-01 02:23:07 UTC (rev 
13071)
+++ trunk/apps/Thaw/src/thaw/gui/Table.java     2007-05-01 05:09:49 UTC (rev 
13072)
@@ -12,6 +12,7 @@
 import javax.swing.ListSelectionModel;
 import javax.swing.JTable;
 import javax.swing.JProgressBar;
+import javax.swing.JTextArea;
 import java.awt.Color;
 import java.awt.Component;

@@ -192,8 +193,17 @@
                                                                           
thaw.gui.GUIHelper.getPrintableSize(((Long)value).longValue()),
                                                                           
isSelected, hasFocus, row, column);

+                       } else if (value instanceof String && 
((String)value).indexOf("\n") >= 0) {
+                               JTextArea area = new JTextArea((String)value);
+                               area.setLineWrap(true);
+                               area.setWrapStyleWord(true);
+
+                               if (getRowHeight(row) < 
area.getPreferredSize().getHeight())
+                                       
setRowHeight((int)area.getPreferredSize().getHeight());
+
+                               cell = area;
+
                        } else {
-
                                cell = 
super.getTableCellRendererComponent(table, value,
                                                                           
isSelected, hasFocus,
                                                                           row, 
column);

Modified: trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2007-05-01 
02:23:07 UTC (rev 13071)
+++ trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2007-05-01 
05:09:49 UTC (rev 13072)
@@ -431,3 +431,12 @@
 thaw.zeroconf.searchingNode=Recherche de noeuds Freenet ...
 thaw.zeroconf.nodeList=Noeuds trouv?s :

+
+# Transfer logs
+
+thaw.plugin.transferLogs.transferLogs=Journal des transfers
+thaw.plugin.transferLogs.transferLogsShort=Journal
+
+thaw.plugin.transferLogs.purgeLogs=Purger le journal
+thaw.plugin.transferLogs.copyKey=Copier la clef li?e ? cette entr?e
+

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2007-05-01 02:23:07 UTC 
(rev 13071)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2007-05-01 05:09:49 UTC 
(rev 13072)
@@ -439,3 +439,28 @@

 thaw.zeroconf.searchingNode=Searching freenet nodes ...
 thaw.zeroconf.nodeList=Nodes found:
+
+
+# Transfer logs
+
+thaw.plugin.transferLogs.transferLogs=Transfer logs
+thaw.plugin.transferLogs.transferLogsShort=Logs
+
+thaw.plugin.transferLogs.purgeLogs=Purge logs
+thaw.plugin.transferLogs.copyKey=Copy the key related to this entry
+
+thaw.plugin.transferLogs.date=Date
+thaw.plugin.transferLogs.message=Message
+thaw.plugin.transferLogs.key=Key
+thaw.plugin.transferLogs.isDup=Is the key duplicated ?
+
+thaw.plugin.transferLogs.download.added=Download added
+thaw.plugin.transferLogs.download.failed=Download failed
+thaw.plugin.transferLogs.download.successful=Download successful
+
+thaw.plugin.transferLogs.insertion.added=Insertion added
+thaw.plugin.transferLogs.insertion.failed=Insertion failed
+thaw.plugin.transferLogs.insertion.successful=Insertion successful
+
+thaw.plugin.transferLogs.finalStatus=Final status
+thaw.plugin.transferLogs.none=(none)

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2007-05-01 02:23:07 UTC 
(rev 13071)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2007-05-01 05:09:49 UTC 
(rev 13072)
@@ -402,6 +402,7 @@

 thaw.plugin.signature.nickname=Pseudo
 thaw.plugin.signature.trustLevel=Status
+thaw.plugin.signature.isDup=Est un duplicata ?

 thaw.plugin.signature.addIdentity=Generer une nouvelle identit\u00e9
 thaw.plugin.signature.removeIdentity=Supprimer l'identit\u00e9 
s\u00e9lectionn\u00e9e
@@ -419,6 +420,9 @@
 thaw.plugin.signature.trustLevel.none=(aucun)
 thaw.plugin.signature.trustLevel.me=MOI

+thaw.plugin.signature.import=Importer une identit\u00e9
+thaw.plugin.signature.export=Exporter une identit\u00e9
+
 thaw.plugin.signature.ignoreLowerThan=Ignorer les signatures avec un status 
inf\u00e9rieur \u00e0


@@ -427,3 +431,8 @@
 thaw.zeroconf.searchingNode=Recherche de noeuds Freenet ...
 thaw.zeroconf.nodeList=Noeuds trouv\u00e9s :

+
+# Transfer logs
+
+thaw.plugin.transferLogs.transferLogs=Journal des transfers
+thaw.plugin.transferLogs.transferLogsShort=Journal

Added: trunk/apps/Thaw/src/thaw/plugins/TransferLogs.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/TransferLogs.java                          
(rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/TransferLogs.java  2007-05-01 05:09:49 UTC 
(rev 13072)
@@ -0,0 +1,545 @@
+package thaw.plugins;
+
+import java.awt.BorderLayout;
+import java.awt.GridLayout;
+
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+import javax.swing.JPanel;
+import javax.swing.JLabel;
+import javax.swing.JButton;
+import javax.swing.JTable;
+import javax.swing.JScrollPane;
+
+import javax.swing.event.TableModelEvent;
+
+import java.util.Observer;
+import java.util.Observable;
+
+import java.util.Vector;
+import java.util.Iterator;
+
+import java.text.DateFormat;
+
+import java.sql.*;
+
+
+import thaw.core.Core;
+import thaw.core.Logger;
+import thaw.gui.IconBox;
+import thaw.gui.Table;
+import thaw.gui.GUIHelper;
+import thaw.core.I18n;
+import thaw.core.Plugin;
+
+import thaw.fcp.FCPQueueManager;
+import thaw.fcp.FCPTransferQuery;
+import thaw.fcp.FreenetURIHelper;
+
+
+/**
+ * Quick and dirty plugin to log the transfers
+ */
+public class TransferLogs implements Plugin, ActionListener, Observer {
+       public final static String MAX_DISPLAYED = "1000";
+
+
+       private Core core;
+       private Hsqldb db;
+
+       private JPanel tab;
+
+
+       private JButton purgeLogs;
+       private JButton copyKey;
+
+       private Table table;
+       private EventListModel model;
+
+       public TransferLogs() {
+
+       }
+
+
+       public boolean run(final Core core) {
+               this.core = core;
+
+               /* loading hsqldb */
+
+               if(core.getPluginManager().getPlugin("thaw.plugins.Hsqldb") == 
null) {
+                       Logger.info(this, "Loading Hsqldb plugin");
+
+                       
if(core.getPluginManager().loadPlugin("thaw.plugins.Hsqldb") == null
+                          || 
!core.getPluginManager().runPlugin("thaw.plugins.Hsqldb")) {
+                               Logger.error(this, "Unable to load 
thaw.plugins.Hsqldb !");
+                               return false;
+                       }
+               }
+
+               db = 
(Hsqldb)core.getPluginManager().getPlugin("thaw.plugins.Hsqldb");
+
+               createTables();
+
+
+               /* making GUI */
+
+               tab = new JPanel(new BorderLayout(5, 5));
+
+               JLabel topLabel = new 
JLabel(I18n.getMessage("thaw.plugin.transferLogs.transferLogs") + " :");
+               topLabel.setIcon(IconBox.file);
+
+               model = new EventListModel();
+               model.reloadList();
+
+               table = new Table(core.getConfig(), "transfer_log_table",
+                                       model);
+
+               purgeLogs = new 
JButton(I18n.getMessage("thaw.plugin.transferLogs.purgeLogs"), 
IconBox.minDelete);
+               copyKey = new 
JButton(I18n.getMessage("thaw.plugin.transferLogs.copyKey"), IconBox.minCopy );
+               purgeLogs.addActionListener(this);
+               copyKey.addActionListener(this);
+
+
+               JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
+               buttonPanel.add(purgeLogs);
+               buttonPanel.add(copyKey);
+
+               JPanel southPanel = new JPanel(new BorderLayout());
+               southPanel.add(buttonPanel, BorderLayout.WEST);
+               southPanel.add(new JLabel(""), BorderLayout.CENTER);
+
+               tab.add(topLabel, BorderLayout.NORTH);
+               tab.add(new JScrollPane(table), BorderLayout.CENTER);
+               tab.add(southPanel, BorderLayout.SOUTH);
+
+               core.getQueueManager().addObserver(this);
+
+               setAsObserverEverywhere(true);
+
+               
core.getMainWindow().addTab(I18n.getMessage("thaw.plugin.transferLogs.transferLogsShort"),
+                                           thaw.gui.IconBox.minFile,
+                                           tab);
+
+               return true;
+       }
+
+
+       public boolean stop() {
+               core.getMainWindow().removeTab(tab);
+               setAsObserverEverywhere(false);
+
+               db.unregisterChild(this);
+
+               return false;
+       }
+
+
+       public String getNameForUser() {
+               return I18n.getMessage("thaw.plugin.transferLogs.transferLogs");
+       }
+
+
+       public javax.swing.ImageIcon getIcon() {
+               return thaw.gui.IconBox.file;
+       }
+
+
+       protected void createTables() {
+               /*
+                * this db structure is dirty, I know it, and at the moment,
+                * I don't care => I will fix it later
+                */
+               sendQuery("CREATE CACHED TABLE transferEvents ("
+                         + "id INTEGER IDENTITY NOT NULL,"
+                         + "date DATE NOT NULL,"
+                         + "msg VARCHAR(500) NOT NULL,"
+                         + "key VARCHAR(500) NULL,"
+                         + "isDup BOOLEAN NOT NULL, "
+                         + "isSuccess BOOLEAN NOT NULL, "
+                         + "PRIMARY KEY (id))");
+       }
+
+
+       /**
+        * Returns no error / Throws no exception.
+        * @return false if an exception happened
+        */
+       protected boolean sendQuery(final String query) {
+               try {
+                       db.executeQuery(query);
+                       return true;
+               } catch(final SQLException e) {
+                       Logger.notice(this, "While (re)creating sql tables: 
"+e.toString());
+                       return false;
+               }
+       }
+
+
+       /**
+        * Add the current plugin as an observer on all the running query
+        * @param really if set to false, will deleteObserver() instead of 
addObserver()
+        */
+       public void setAsObserverEverywhere(boolean really) {
+               if (really)
+                       core.getQueueManager().addObserver(this);
+               else
+                       core.getQueueManager().addObserver(this);
+
+               for (Iterator it = 
core.getQueueManager().getRunningQueue().iterator();
+                    it.hasNext();) {
+                       FCPTransferQuery query = (FCPTransferQuery)it.next();
+
+                       if (query.isFinished() && !isDup(query.getFileKey()))
+                               notifyEnd(query);
+
+                       if (query instanceof Observable)
+                               ((Observable)query).addObserver(this);
+               }
+       }
+
+
+       protected boolean isDup(String key) {
+               try {
+                       synchronized(db.dbLock) {
+
+                               PreparedStatement st;
+
+                               st = 
db.getConnection().prepareStatement("SELECT id FROM transferEvents "+
+                                                                        "WHERE 
LOWER(key) LIKE ? AND isSuccess = TRUE");
+                               st.setString(1, 
FreenetURIHelper.getComparablePart(key)+"%");
+                               ResultSet set = st.executeQuery();
+                               return set.next();
+
+                       }
+               } catch(SQLException e) {
+                       Logger.error(this, "Unable to know if a key is dup in 
the event because : "+e.toString());
+               }
+
+               return false;
+       }
+
+
+       protected void notifyAddition(FCPTransferQuery query) {
+               String str;
+               String key;
+
+               if (query.getQueryType() == 0)
+                       return;
+
+               if (query.getQueryType() == 1) {
+                       str = 
I18n.getMessage("thaw.plugin.transferLogs.download.added") + " : "
+                               + query.getFilename();
+                       key = query.getFileKey();
+               } else {
+                       str =  
I18n.getMessage("thaw.plugin.transferLogs.insertion.added") + " : "
+                               + query.getPath();
+                       key = null;
+               }
+
+               java.sql.Date date = new java.sql.Date((new 
java.util.Date()).getTime());
+
+               boolean isDup;
+
+               if (key != null)
+                       isDup = isDup(key);
+               else
+                       isDup = false;
+
+               try {
+                       synchronized(db.dbLock) {
+                               PreparedStatement st;
+
+
+                               st = 
db.getConnection().prepareStatement("INSERT INTO transferEvents "+
+                                                                        
"(date, msg, key, isDup, isSuccess) "+
+                                                                        " 
VALUES "+
+                                                                        "(?, 
?, ?, ?, FALSE)");
+                               st.setDate(1, date);
+                               st.setString(2, str);
+                               st.setString(3, key);
+                               st.setBoolean(4, isDup);
+
+                               st.execute();
+                       }
+               } catch(SQLException e) {
+                       Logger.error(this, "Error while adding an event to the 
logs: "+e.toString());
+               }
+
+               model.reloadList();
+       }
+
+
+       protected void notifyEnd(FCPTransferQuery query) {
+               String str;
+               String key;
+
+               if (query.getQueryType() == 0 || !query.isFinished())
+                       return;
+
+               if (query.getQueryType() == 1)
+                       str = "thaw.plugin.transferLogs.download.";
+               else
+                       str = "thaw.plugin.transferLogs.insertion.";
+
+               if (query.isSuccessful())
+                       str = I18n.getMessage(str+"successful");
+               else
+                       str = I18n.getMessage(str+"failed");
+
+               key = query.getFileKey();
+
+               str += " : "+query.getFilename();
+
+               str += "\n" + 
I18n.getMessage("thaw.plugin.transferLogs.finalStatus") + " : 
"+query.getStatus();
+
+               boolean isDup;
+               boolean isSuccess = query.isSuccessful();
+
+               if (key != null)
+                       isDup = isDup(key);
+               else
+                       isDup = false;
+
+               java.sql.Date date = new java.sql.Date((new 
java.util.Date()).getTime());
+
+
+               try {
+                       synchronized(db.dbLock) {
+                               PreparedStatement st;
+
+
+                               st = 
db.getConnection().prepareStatement("INSERT INTO transferEvents "+
+                                                                        
"(date, msg, key, isDup, isSuccess) "+
+                                                                        " 
VALUES "+
+                                                                        "(?, 
?, ?, ?, ?)");
+                               st.setDate(1, date);
+                               st.setString(2, str);
+                               st.setString(3, key);
+                               st.setBoolean(4, isDup);
+                               st.setBoolean(5, isSuccess);
+
+                               st.execute();
+                       }
+               } catch(SQLException e) {
+                       Logger.error(this, "Error while adding an event to the 
logs: "+e.toString());
+               }
+
+
+               model.reloadList();
+       }
+
+
+       public void update(Observable o, Object param) {
+
+               if (o instanceof FCPQueueManager) {
+                       FCPQueueManager queue = (FCPQueueManager)o;
+
+                       if (param == null)
+                               return;
+
+                       FCPTransferQuery query = (FCPTransferQuery)param;
+
+                       if(core.getQueueManager().isInTheQueues(query)) { // 
then it's an addition
+                               if (query instanceof Observable)
+                                       ((Observable)query).addObserver(this);
+                               if 
(core.getQueueManager().isQueueCompletlyLoaded())
+                                       notifyAddition(query);
+                               return;
+                       }
+               }
+
+
+               if (o instanceof FCPTransferQuery) {
+                       FCPTransferQuery query = (FCPTransferQuery)o;
+
+                       if (query.isFinished()) {
+                               notifyEnd(query);
+                               return;
+                       }
+               }
+       }
+
+
+       private class Event {
+               private java.sql.Date date;
+               private String msg;
+               private String key;
+               private boolean isDup;
+
+               public Event(java.sql.Date date, String message, String key, 
boolean isDup) {
+                       this.date = date;
+                       this.msg = message;
+                       this.key = key;
+                       this.isDup = isDup;
+               }
+
+               public java.sql.Date getDate() {
+                       return date;
+               }
+
+               public String getMsg() {
+                       return msg;
+               }
+
+               public String getKey() {
+                       return key;
+               }
+
+               public boolean isDup() {
+                       return isDup;
+               }
+       }
+
+       private class EventListModel extends 
javax.swing.table.AbstractTableModel {
+               private static final long serialVersionUID = 1L;
+
+               private DateFormat dateFormat;
+
+               public String[] columnNames =
+               {
+                       I18n.getMessage("thaw.plugin.transferLogs.date"),
+                       I18n.getMessage("thaw.plugin.transferLogs.message"),
+                       I18n.getMessage("thaw.plugin.transferLogs.key"),
+                       I18n.getMessage("thaw.plugin.transferLogs.isDup")
+               };
+
+
+               public Vector events = null; /* thaw.plugins.index.File Vector 
*/
+
+
+               public EventListModel() {
+                       super();
+                       dateFormat = DateFormat.getDateInstance();
+               }
+
+               public void reloadList() {
+                       events = null;
+
+                       try {
+                               synchronized(db.dbLock) {
+                                       PreparedStatement st;
+
+                                       st = 
db.getConnection().prepareStatement("SELECT date, msg, key, isDup FROM "+
+                                                                               
 "transferEvents ORDER BY date DESC LIMIT "+MAX_DISPLAYED);
+
+                                       ResultSet set = st.executeQuery();
+
+                                       events = new Vector();
+
+                                       while(set.next()) {
+                                               events.add(new 
Event(set.getDate("date"),
+                                                                    
set.getString("msg"),
+                                                                    
set.getString("key"),
+                                                                    
set.getBoolean("isDup")));
+                                       }
+                               }
+                       } catch(SQLException e) {
+                               Logger.error(this, "Error while getting the 
list of events from the logs: "+e.toString());
+                       }
+
+                       refresh();
+               }
+
+
+               protected void refresh() {
+                       final TableModelEvent event = new TableModelEvent(this);
+                       fireTableChanged(event);
+               }
+
+               public int getRowCount() {
+                       if (events == null)
+                               return 0;
+
+                       return events.size();
+               }
+
+               public int getColumnCount() {
+                       return columnNames.length;
+               }
+
+               public String getColumnName(final int column) {
+                       return columnNames[column];
+
+               }
+
+               public Object getValueAt(final int row, final int column) {
+                       if (events == null)
+                               return null;
+
+                       if (column == 0)
+                               return 
dateFormat.format(((Event)events.get(row)).getDate());
+
+                       if (column == 1)
+                               return ((Event)events.get(row)).getMsg();
+
+                       if (column == 2) {
+                               String key = ((Event)events.get(row)).getKey();
+                               return key != null ? key : 
I18n.getMessage("thaw.plugin.transferLogs.none");
+                       }
+
+                       if (column == 3)
+                               return ((Event)events.get(row)).isDup() ? "X" : 
"";
+
+                       return null;
+               }
+
+
+               public Vector getSelectedRows(Table table) {
+                       int[] selectedRows = table.getSelectedRows();
+
+                       if (selectedRows == null)
+                               return null;
+
+                       Vector r = new Vector();
+
+                       for (int i = 0 ; i < selectedRows.length ; i++) {
+                               r.add(events.get(selectedRows[i]));
+                       }
+
+                       return r;
+               }
+       }
+
+
+       public void actionPerformed(ActionEvent e) {
+
+               if (e.getSource() == purgeLogs) {
+                       try {
+                               synchronized(db.dbLock) {
+                                       PreparedStatement st;
+
+                                       st = 
db.getConnection().prepareStatement("DELETE FROM transferEvents");
+                                       st.execute();
+                               }
+                       } catch(SQLException exc) {
+                               Logger.error(this, "Unable to purge the logs 
because: "+exc.toString());
+                       }
+
+                       model.reloadList();
+
+                       return;
+               }
+
+               if (e.getSource() == copyKey) {
+                       Vector v = model.getSelectedRows(table);
+
+                       if (v == null)
+                               return;
+
+                       String str = "";
+
+                       for (Iterator it = v.iterator();
+                            it.hasNext();) {
+                               String key = ((Event)it.next()).getKey();
+
+                               if (key != null)
+                                       str += key + "\n";
+                       }
+
+                       GUIHelper.copyToClipboard(str);
+
+                       return;
+               }
+       }
+}


Reply via email to