Author: jflesch
Date: 2008-02-12 18:35:06 +0000 (Tue, 12 Feb 2008)
New Revision: 17842

Added:
   trunk/apps/Thaw/src/thaw/plugins/webOfTrust/TrustListUploader.java
Modified:
   trunk/apps/Thaw/src/thaw/core/ConfigWindow.java
   trunk/apps/Thaw/src/thaw/core/Core.java
   trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java
   trunk/apps/Thaw/src/thaw/fcp/FCPGenerateSSK.java
   trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw.properties
   trunk/apps/Thaw/src/thaw/plugins/Signatures.java
   trunk/apps/Thaw/src/thaw/plugins/WebOfTrust.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigDialog.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
   trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java
   trunk/apps/Thaw/src/thaw/plugins/miniFrost/BoardSelecter.java
   trunk/apps/Thaw/src/thaw/plugins/miniFrost/DraftPanel.java
   trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java
   trunk/apps/Thaw/src/thaw/plugins/miniFrost/frostKSK/SSKBoardFactory.java
   trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
   trunk/apps/Thaw/src/thaw/plugins/webOfTrust/WebOfTrustConfigTab.java
Log:
Web of Trust : Trust list insertion is now working

Modified: trunk/apps/Thaw/src/thaw/core/ConfigWindow.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/ConfigWindow.java     2008-02-12 17:43:17 UTC 
(rev 17841)
+++ trunk/apps/Thaw/src/thaw/core/ConfigWindow.java     2008-02-12 18:35:06 UTC 
(rev 17842)
@@ -304,8 +304,10 @@
                                        core.getConfig().applyChanges();
                        }

-                       if (resetConnection)
+                       if (resetConnection) {
                                dialog.setVisible(false);
+                               dialog.dispose();
+                       }
                }

                public void run() {

Modified: trunk/apps/Thaw/src/thaw/core/Core.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/Core.java     2008-02-12 17:43:17 UTC (rev 
17841)
+++ trunk/apps/Thaw/src/thaw/core/Core.java     2008-02-12 18:35:06 UTC (rev 
17842)
@@ -614,7 +614,7 @@

                                subDisconnect();

-                               while(running) {
+                               while(running && !isStopping()) {
                                        try {
                                                if (initialWait)
                                                        
Thread.sleep(Core.TIME_BETWEEN_EACH_TRY);
@@ -629,7 +629,7 @@
                                                break;
                                }

-                               if (running) {
+                               if (running && !isStopping()) {
                                        
getMainWindow().setStatus(IconBox.minConnectAction,
                                                                  
I18n.getMessage("thaw.statusBar.ready"));
                                } else {
@@ -637,7 +637,7 @@
                                                                  
I18n.getMessage("thaw.statusBar.disconnected"), java.awt.Color.RED);
                                }

-                               if (running) {
+                               if (running && !isStopping()) {
                                        getPluginManager().loadAndRunPlugins();
                                }


Modified: trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java      2008-02-12 17:43:17 UTC 
(rev 17841)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java      2008-02-12 18:35:06 UTC 
(rev 17842)
@@ -53,7 +53,7 @@
        private int putFailedCode = -1;

        /**
-        * To resume query from file. (see core.QueueKeeper)
+        * To resume query from file. (see thaw.core.QueueKeeper)
         */
        public FCPClientPut(final FCPQueueManager queueManager, final HashMap 
parameters) {
                super(true);

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPGenerateSSK.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPGenerateSSK.java    2008-02-12 17:43:17 UTC 
(rev 17841)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPGenerateSSK.java    2008-02-12 18:35:06 UTC 
(rev 17842)
@@ -51,11 +51,11 @@
                        privateKey = privateKey.replaceFirst("freenet:", "");
                        publicKey = publicKey.replaceFirst("freenet:", "");

+                       stop(queueManager);
+                       
                        setChanged();
                        this.notifyObservers();

-                       stop(queueManager);
-
                        return;
                }


Modified: trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2008-02-12 
17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2008-02-12 
18:35:06 UTC (rev 17842)
@@ -626,7 +626,7 @@
 thaw.plugin.miniFrost.foldAll=Tout plier

 thaw.plugin.miniFrost.goBack=Retourner ? la liste des messages
-thaw.plugin.miniFrost.nextUnread=Prochain message non-lu sur cette board
+thaw.plugin.miniFrost.nextUnread=Prochain message non-lu sur cette board 
(Alt-N)

 thaw.plugin.miniFrost.selectAll=Tout s?lectionner
 thaw.plugin.miniFrost.selectNone=Ne plus rien s?lectionner

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2008-02-12 17:43:17 UTC 
(rev 17841)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2008-02-12 18:35:06 UTC 
(rev 17842)
@@ -623,7 +623,7 @@
 thaw.plugin.miniFrost.foldAll=Fold all

 thaw.plugin.miniFrost.goBack=Go back to the message list
-thaw.plugin.miniFrost.nextUnread=Next unread message on this board
+thaw.plugin.miniFrost.nextUnread=Next unread message on this board (Alt-N)

 thaw.plugin.miniFrost.newMessage=New Message


Modified: trunk/apps/Thaw/src/thaw/plugins/Signatures.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/Signatures.java    2008-02-12 17:43:17 UTC 
(rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/Signatures.java    2008-02-12 18:35:06 UTC 
(rev 17842)
@@ -1,7 +1,7 @@
 package thaw.plugins;

 import java.util.Iterator;
-import java.util.HashSet;
+import java.util.Vector;

 import thaw.core.I18n;
 import thaw.core.Core;
@@ -77,6 +77,9 @@
                core.getConfig().addListener("minTrustLevel", this);

                used++;
+               
+               if (observers.size() > 0)
+                       observers = new Vector();

                if(core.getPluginManager().getPlugin("thaw.plugins.Hsqldb") == 
null) {
                        Logger.info(this, "Loading Hsqldb plugin");
@@ -119,6 +122,7 @@
                used--;

                if (used == 0) {
+                       observers = new Vector();
                        db.unregisterChild(this);
                        db = null;
                }
@@ -129,6 +133,7 @@
                used--;

                if (used == 0) {
+                       observers = new Vector();
                        db.unregisterChild(this);
                        db = null;
                }
@@ -144,19 +149,51 @@


        public static interface SignaturesObserver {
+               /**
+                * Id in the db may not be defined => you may have to query the 
db to know it
+                * @param i
+                */
                public void publicIdentityAdded(Identity i);
+               
+               /**
+                * Id in the db may not be defined => you may have to query the 
db to know it
+                * @param i
+                */
                public void privateIdentityAdded(Identity i);
-               public void trustLevelUpdated(Identity i);
+
+               public void identityUpdated(Identity i);
                /* we never remove identities ? ... hmmmm */
        }

-       private static HashSet observers = new HashSet();
+       private static Vector observers = new Vector();

-       public void addObserver(SignaturesObserver o) {
-               
+       public static void addObserver(SignaturesObserver o) {
+               if (!observers.contains(o))
+                       observers.add(o);
        }

-       public void removeObserver(SignaturesObserver o) {
-               
+       public static void deleteObserver(SignaturesObserver o) {
+               while (observers.remove(o));
        }
+       
+       public static void notifyPublicIdentityAdded(Identity i) {
+               for (Iterator it = observers.iterator();
+                        it.hasNext();) {
+                       ((SignaturesObserver)it.next()).publicIdentityAdded(i);
+               }
+       }
+       
+       public static void notifyPrivateIdentityAdded(Identity i) {
+               for (Iterator it = observers.iterator();
+                        it.hasNext();) {
+                       ((SignaturesObserver)it.next()).privateIdentityAdded(i);
+               }
+       }
+       
+       public static void notifyIdentityUpdated(Identity i) {
+               for (Iterator it = observers.iterator();
+                        it.hasNext();) {
+                       ((SignaturesObserver)it.next()).identityUpdated(i);
+               }
+       }
 }

Modified: trunk/apps/Thaw/src/thaw/plugins/WebOfTrust.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/WebOfTrust.java    2008-02-12 17:43:17 UTC 
(rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/WebOfTrust.java    2008-02-12 18:35:06 UTC 
(rev 17842)
@@ -5,11 +5,15 @@
 import thaw.core.Core;
 import thaw.core.Logger;
 import thaw.core.I18n;
+import thaw.core.ThawThread;
+import thaw.core.ThawRunnable;
 import thaw.plugins.Hsqldb;
 import thaw.plugins.Signatures;
-import thaw.plugins.webOfTrust.WebOfTrustConfigTab;
+import thaw.plugins.webOfTrust.*;

 public class WebOfTrust extends thaw.core.LibraryPlugin {
+       public final static long UPLOAD_AFTER_MS = 30*60*1000; /* 30 min */ 
+       
        private Core core;
        private Hsqldb db;
        private Signatures sigs;
@@ -72,11 +76,73 @@
        public String getNameForUser() {
                return I18n.getMessage("thaw.plugin.wot");
        }
+       
+       private class TrucMucheThread implements ThawRunnable {
+               private boolean running = false;

+               public TrucMucheThread() {
+                       running = false;
+               }
+               
+               public void run() {
+                       running = true;
+                       
+                       while(running) {
+                               try {
+                                       Thread.sleep(1000);
+                               } catch(InterruptedException e) {
+                                       /* \_o< */
+                               }
+                               
+                               try {
+                                       if (running)
+                                               process();
+                               } catch(Exception e) {
+                                       Logger.error(this, "Exception in the 
web of trust plugin : "+e.toString());
+                                       e.printStackTrace();
+                               }
+                       }
+               }
+               
+               public void stop() {
+                       running = false;
+               }
+       }
+       
+       private TrucMucheThread thread;
+       private TrustListUploader trustListUploader = null;
+       
+       private void initThread() {
+               trustListUploader = new TrustListUploader(db, 
core.getQueueManager(), core.getConfig());
+               trustListUploader.init();
+
+               thread = new TrucMucheThread();
+               new ThawThread(thread, "WoT refresher", this).start();
+       }
+       
+       private void process() {
+               if (trustListUploader != null)
+                       trustListUploader.process();
+       }
+       
+       private void stopThread() {
+               if (thread != null) {
+                       thread.stop();
+                       thread = null;
+               }
+               
+               if (trustListUploader != null) {
+                       trustListUploader.stop();
+                       trustListUploader = null;
+               }
+       }
+
        public boolean run(Core core) {
                core.getConfig().addListener("wotActivated",        this);
                core.getConfig().addListener("wotIdentityUsed",     this);
                core.getConfig().addListener("wotNumberOfRefresh",  this);
+               core.getConfig().addListener("wotPrivateKey",       this);
+               core.getConfig().addListener("wotPublicKey",        this);

                used++;

@@ -91,6 +157,8 @@
                
core.getConfigWindow().addTab(I18n.getMessage("thaw.plugin.wot"),
                              thaw.gui.IconBox.minTrust,
                              configTab.getPanel());
+               
+               initThread();

                return true;
        }
@@ -103,8 +171,11 @@
                        configTab = null;
                }

-               if (used == 0)
+               if (used == 0) {
                        unloadDeps(core);
+               }
+               
+               stopThread();
        }

        public void realStart() {

Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigDialog.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigDialog.java       
2008-02-12 17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigDialog.java       
2008-02-12 18:35:06 UTC (rev 17842)
@@ -414,11 +414,13 @@

                        index.setCategory(categoryField.getText());
                        categoryDialog.setVisible(false);
+                       categoryDialog.dispose();
                        updateCategoryLabel();

                } else if (e.getSource() == categoryCancelButton) {

                        categoryDialog.setVisible(false);
+                       categoryDialog.dispose();

                }
        }

Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java   
2008-02-12 17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexManagementHelper.java   
2008-02-12 18:35:06 UTC (rev 17842)
@@ -1381,6 +1381,7 @@
                                final Vector keyVec = new Vector();

                                frame.setVisible(false);
+                               frame.dispose();

                                final String category = 
FileCategory.promptForACategory();

@@ -1399,6 +1400,7 @@

                        if (e.getSource() == cancelButton) {
                                frame.setVisible(false);
+                               frame.dispose();
                        }
                }

@@ -1701,6 +1703,7 @@

                        if (closeDialog) {
                                dialog.setVisible(false);
+                               dialog.dispose();
                                dialog = null;
                        }
                }
@@ -1812,6 +1815,7 @@
                        if (e.getSource() == closeButton) {
                                if (dialog != null) {
                                        dialog.setVisible(false);
+                                       dialog.dispose();
                                        dialog = null;
                                }
                        } else {

Modified: trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java       
2008-02-12 17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/mDns/MDNSDiscoveryPanel.java       
2008-02-12 18:35:06 UTC (rev 17842)
@@ -205,6 +205,7 @@

                this.setVisible(false);
                owner.setEnabled(true);
+               this.dispose();

                if (!cancelledByUser) {
                        Logger.debug(this, "We got something that looks valid 
from the UI : let's propagate changes to  the config");

Modified: trunk/apps/Thaw/src/thaw/plugins/miniFrost/BoardSelecter.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/miniFrost/BoardSelecter.java       
2008-02-12 17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/miniFrost/BoardSelecter.java       
2008-02-12 18:35:06 UTC (rev 17842)
@@ -123,6 +123,7 @@
                }

                dialog.setVisible(false);
+               dialog.dispose();

                Vector selected = null;


Modified: trunk/apps/Thaw/src/thaw/plugins/miniFrost/DraftPanel.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/miniFrost/DraftPanel.java  2008-02-12 
17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/miniFrost/DraftPanel.java  2008-02-12 
18:35:06 UTC (rev 17842)
@@ -530,8 +530,11 @@

                if (dialog == null)
                        mainPanel.displayMessageTable();
-               else
+               else {
                        dialog.setVisible(false);
+                       dialog.dispose();
+                       dialog = null;
+               }
        }



Modified: trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java    
2008-02-12 17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/miniFrost/MessageTreeTable.java    
2008-02-12 18:35:06 UTC (rev 17842)
@@ -291,7 +291,8 @@


        public void hided() {
-               nextUnread.setMnemonic(KeyEvent.VK_Z);
+               if (gmailView)
+                       nextUnread.setMnemonic(KeyEvent.VK_Z);
        }

        /**

Modified: 
trunk/apps/Thaw/src/thaw/plugins/miniFrost/frostKSK/SSKBoardFactory.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/miniFrost/frostKSK/SSKBoardFactory.java    
2008-02-12 17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/miniFrost/frostKSK/SSKBoardFactory.java    
2008-02-12 18:35:06 UTC (rev 17842)
@@ -204,6 +204,7 @@
                                this.notifyAll();
                        }
                        dialog.setVisible(false);
+                       dialog.dispose();
                }

                public void update(Observable o, Object param) {

Modified: trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java   2008-02-12 
17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java   2008-02-12 
18:35:06 UTC (rev 17842)
@@ -15,6 +15,7 @@
 import thaw.core.Logger;
 import thaw.core.I18n;
 import thaw.plugins.Hsqldb;
+import thaw.plugins.Signatures;
 import thaw.core.Config;


@@ -242,6 +243,8 @@
                        }

                        trustLevel = i;
+                       
+                       Signatures.notifyIdentityUpdated(this);

                } catch(SQLException e) {
                        Logger.error(this, "Unable to change trust level 
because: "+e.toString());
@@ -365,6 +368,9 @@

                                Identity i = new Identity(db, id, nick, 
publicKey, null, isDup, 0);
                                Logger.info(i, "New identity found");
+                               
+                               Signatures.notifyPublicIdentityAdded(i);
+                               
                                return i;

                        }
@@ -466,6 +472,8 @@
                                        st.setInt(3, id);

                                        st.execute();
+                                       
+                                       Signatures.notifyIdentityUpdated(this);
                                } else {

                                        st = 
db.getConnection().prepareStatement("INSERT INTO signatures "+
@@ -479,6 +487,11 @@
                                        st.setInt(5, trustLevel);

                                        st.execute();
+                                       
+                                       if (privateKey == null)
+                                               
Signatures.notifyPublicIdentityAdded(this);
+                                       else
+                                               
Signatures.notifyPrivateIdentityAdded(this);
                                }
                        }
                } catch(SQLException e) {
@@ -486,8 +499,28 @@
                        e.printStackTrace();
                }
        }
+       
+       
+       public static boolean hasAtLeastATrustDefined(Hsqldb db) {
+               try {
+                       synchronized(db.dbLock) {
+                               PreparedStatement st = 
db.getConnection().prepareStatement("SELECT id FROM signatures WHERE trustLevel 
!= ? and trustLevel < ?");
+                               st.setInt(1, 0);
+                               st.setInt(2, trustLevelInt[0]);
+                               
+                               ResultSet set = st.executeQuery();

+                               return set.next();                              
+                       }                       
+               } catch(SQLException e) {
+                       Logger.error(new Identity(), "Exception while accessing 
the signature table : "+e.toString());
+                       e.printStackTrace();
+               }
+               
+               return false;
+       }

+
        public void delete() {
                try {
                        synchronized(db.dbLock) {

Added: trunk/apps/Thaw/src/thaw/plugins/webOfTrust/TrustListUploader.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/webOfTrust/TrustListUploader.java          
                (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/webOfTrust/TrustListUploader.java  
2008-02-12 18:35:06 UTC (rev 17842)
@@ -0,0 +1,341 @@
+package thaw.plugins.webOfTrust;
+
+import thaw.plugins.Hsqldb;
+import thaw.plugins.Signatures;
+import thaw.plugins.WebOfTrust;
+import thaw.plugins.signatures.Identity;
+import thaw.plugins.signatures.TrustListParser;
+
+
+import thaw.core.Config;
+import thaw.core.Logger;
+import thaw.fcp.FCPQueueManager;
+import thaw.fcp.FCPClientPut;
+import thaw.fcp.FCPGenerateSSK;
+import thaw.fcp.FreenetURIHelper;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Date;
+import java.util.Observer;
+import java.util.Observable;
+import java.util.Vector;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class TrustListUploader implements Signatures.SignaturesObserver, 
Observer {
+       private final FCPQueueManager queueManager;
+       private final Config config;
+       private final Hsqldb db;
+       
+       private Date lastUpload = null;
+       private Date lastTrustChange = null;
+       
+       private Identity identity;
+       
+       private String privateKey;
+       private String publicKey;
+       
+       private FCPClientPut upload;
+
+       public TrustListUploader(Hsqldb db, FCPQueueManager queueManager, 
Config config) {
+               this.queueManager = queueManager;
+               this.config = config;
+               this.db = db;
+               
+               lastUpload = null;
+               lastTrustChange = null;
+               
+               identity = null;
+               
+               try {
+                       if (config.getValue("wotIdentityUsed") != null)
+                               identity = Identity.getIdentity(db, 
Integer.parseInt(config.getValue("wotIdentityUsed")));
+               } catch(Exception e) {
+                       Logger.error(this, "Error in the config : can't find 
the identity to use to upload the trust list (or its keys) => won't insert the 
trust list ; Exception throwed: "+e.toString());
+               }
+               
+               publicKey = null;
+               privateKey = null;
+       }
+       
+       public void init() {
+               lastUpload = null;
+               
+               if (config.getValue("wotLastUpload") != null)
+                       lastUpload = new 
Date(Long.parseLong(config.getValue("wotLastUpload")));
+               
+               lastTrustChange = null;
+               
+               if (config.getValue("wotLastTrustChange") != null)
+                       lastTrustChange = new 
Date(Long.parseLong(config.getValue("wotLastTrustChange")));
+               
+               if (config.getValue("wotPublicKey") != null)
+                       publicKey = config.getValue("wotPublicKey");
+               if (config.getValue("wotPrivateKey") != null)
+                       privateKey = config.getValue("wotPrivateKey");
+               
+               if (publicKey == null || privateKey == null) {
+                       regenerateKeys();
+               }
+               
+               Signatures.addObserver(this);
+       }
+       
+       public void regenerateKeys() {
+               publicKey = null;
+               privateKey = null;
+               lastUpload = null;
+               
+               Logger.notice(this, "Regenerating a key pair for the wot of 
trust");
+
+               FCPGenerateSSK sskGenerator = new FCPGenerateSSK();
+               sskGenerator.addObserver(this);
+               sskGenerator.start(queueManager);
+       }
+       
+       /**
+        * @return true if a trust change was done more than some times ago and 
no
+        *  trust list upload has been done between this change and now
+        */
+       private boolean mustUpload() {
+               /* never uploaded => we must do it for the first time */
+               if (lastUpload == null)
+                       return true;
+               
+               /* no change => no upload */
+               if (lastTrustChange == null)
+                       return false;
+               
+               /* if the last upload is older than the last trust change */
+               if (lastUpload.compareTo(lastTrustChange) < 0) {
+               
+                       /* last change was done more than UPLOAD_AFTER_MS ms 
(for example 30min) */
+                       if (new Date().getTime() - lastTrustChange.getTime() >= 
WebOfTrust.UPLOAD_AFTER_MS)
+                               return true;
+                       
+               }
+               
+               return false;
+       }
+       
+       /**
+        * @return true if the list was already uploaded once or if we have more
+        *   than 1 identity with a trust != 0 and != dev trust
+        */
+       private boolean hasSomethingToUpload() {
+               /* if no identity selected, can't insert */
+               if (identity == null)
+                       return false;
+               
+               /* if a trust was changed at a given time */
+               /* then we must upload, even if it's to say we trust nobody 
anymore */
+               if (lastUpload != null || lastTrustChange != null)
+                       return true;
+               
+               return Identity.hasAtLeastATrustDefined(db);
+       }
+       
+       private boolean addHeaders(Element rootEl, Document xmlDoc) {
+               Element trustListOwnerEl = 
xmlDoc.createElement("trustListOwner");
+
+               Element nickEl = xmlDoc.createElement("nick");
+               nickEl.appendChild(xmlDoc.createTextNode(identity.getNick()));
+               
+               Element publicKeyEl = xmlDoc.createElement("publicKey");
+               
publicKeyEl.appendChild(xmlDoc.createTextNode(identity.getPublicKey()));
+               
+               trustListOwnerEl.appendChild(nickEl);
+               trustListOwnerEl.appendChild(publicKeyEl);
+               
+               rootEl.appendChild(trustListOwnerEl);
+               
+               return true;
+       }
+       
+       private boolean exportTrustList(Vector identities, File file) {
+               try {
+                       FileOutputStream out = new FileOutputStream(file);
+                       
+                       StreamResult streamResult;
+
+                       streamResult = new StreamResult(out);
+
+                       Document xmlDoc;
+
+                       final DocumentBuilderFactory xmlFactory = 
DocumentBuilderFactory.newInstance();
+                       DocumentBuilder xmlBuilder;
+
+                       try {
+                               xmlBuilder = xmlFactory.newDocumentBuilder();
+                       } catch(final 
javax.xml.parsers.ParserConfigurationException e) {
+                               Logger.error(this, "Unable to generate the 
index because : "+e.toString());
+                               return false;
+                       }
+
+                       final DOMImplementation impl = 
xmlBuilder.getDOMImplementation();
+
+                       xmlDoc = impl.createDocument(null, "trustList", null);
+
+                       final Element rootEl = xmlDoc.getDocumentElement();
+
+                       /**** DOM Tree generation ****/
+                       addHeaders(rootEl, xmlDoc);
+                       TrustListParser.fillInRootElement(identities, rootEl, 
xmlDoc);
+
+
+                       /* Serialization */
+                       final DOMSource domSource = new DOMSource(xmlDoc);
+                       final TransformerFactory transformFactory = 
TransformerFactory.newInstance();
+
+                       Transformer serializer;
+
+                       try {
+                               serializer = transformFactory.newTransformer();
+                       } catch(final 
javax.xml.transform.TransformerConfigurationException e) {
+                               Logger.error(this, "Unable to save index 
because: "+e.toString());
+                               return false;
+                       }
+
+                       
serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
+                       serializer.setOutputProperty(OutputKeys.INDENT,"yes");
+
+                       /* final step */
+                       try {
+                               serializer.transform(domSource, streamResult);
+                       } catch(final javax.xml.transform.TransformerException 
e) {
+                               Logger.error(this, "Unable to save index 
because: "+e.toString());
+                               return false;
+                       }
+                       
+                       out.close();
+                       
+                       return true;
+               } catch(java.io.FileNotFoundException e) {
+                       Logger.error(this, "File not found exception ?!");
+               } catch(java.io.IOException e) {
+                       Logger.error(this, "IOException while generating the 
index: "+e.toString());
+               }
+               
+               return false;
+       }
+       
+       
+       /**
+        * called ~ each second
+        */
+       public synchronized void process() {
+               /* an upload is already running => can't upload */
+               if (upload != null)
+                       return;         
+               
+               /* no private key => can't upload anyway */
+               if (privateKey == null || identity == null)
+                       return;
+
+               
+               if (mustUpload() && hasSomethingToUpload()) {
+                       
+                       Logger.notice(this, "Uploading your trust list ...");
+                       
+                       try {
+                               File file = File.createTempFile("thaw-", 
"-trustList.xml");
+                               Vector ids = Identity.getOtherIdentities(db);
+
+                               if (!exportTrustList(ids, file))
+                                       return;
+                               
+                               upload = new FCPClientPut(file, 
FCPClientPut.KEY_TYPE_SSK, 0,
+                                                                               
        "trustList", FreenetURIHelper.convertSSKtoUSK(privateKey)+"/", /* the 
convertion fonction forget the '/' */
+                                                                               
        2, /* priority */
+                                                                               
    false, /* global */
+                                                                               
    FCPClientPut.PERSISTENCE_FOREVER); /* persistence */
+                               upload.addObserver(this);
+
+                               queueManager.addQueryToTheRunningQueue(upload);
+
+                       } catch(java.io.IOException e) {
+                               Logger.error(this, "Can't upload your trust 
list because : "+e.toString());
+                               e.printStackTrace();
+                       }
+               }
+       }
+       
+       
+       public void stop() {
+               Signatures.deleteObserver(this);
+               
+               if (lastUpload == null)
+                       config.setValue("wotLastUpload", null);
+               else
+                       config.setValue("wotLastUpload", 
Long.toString(lastUpload.getTime()));
+               
+               if (lastTrustChange == null)
+                       config.setValue("wotLastTrustChange", null);
+               else
+                       config.setValue("wotLastTrustChange", 
Long.toString(lastTrustChange.getTime()));
+       }
+
+       public void identityUpdated(Identity i) {
+               /* a dev was added => ignore */
+               if (i.getTrustLevel() == Identity.trustLevelInt[0])
+                       return;
+               
+               lastTrustChange = new Date();
+       }
+
+       public void privateIdentityAdded(Identity i) {
+               /* a dev was added => ignore */
+               if (i.getTrustLevel() == Identity.trustLevelInt[0])
+                       return;
+               
+               /* by default, new private identity have no trust, so we don't 
care */
+               identityUpdated(i);
+       }
+
+       public void publicIdentityAdded(Identity i) {
+               /* by default, new public identity have no trust, so we don't 
care */
+       }
+
+       public void update(Observable o, Object param) {
+               if (o instanceof FCPGenerateSSK) {
+                       Logger.notice(this, "Key pair generated");
+                       FCPGenerateSSK sskGenerator = (FCPGenerateSSK)o;
+                       publicKey = sskGenerator.getPublicKey();
+                       privateKey = sskGenerator.getPrivateKey();
+
+                       config.setValue("wotPrivateKey", privateKey);
+                       config.setValue("wotPublicKey", publicKey);
+
+               } else if (o instanceof FCPClientPut) {
+
+                       if (upload.isFinished()) {
+               
+                               upload.deleteObserver(this);
+                               
+                               queueManager.remove(upload);
+                               
+                               if (upload.isSuccessful()) {
+                                       Logger.notice(this, "Trust list 
inserted");
+                                       lastUpload = new Date();
+                               } else
+                                       Logger.warning(this, "Unable to insert 
trust list !");
+                               
+                               if (upload.getPath() != null)
+                                       new File(upload.getPath()).delete();
+                               
+                               upload = null;
+                       }
+               }
+       }
+}

Modified: trunk/apps/Thaw/src/thaw/plugins/webOfTrust/WebOfTrustConfigTab.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/webOfTrust/WebOfTrustConfigTab.java        
2008-02-12 17:43:17 UTC (rev 17841)
+++ trunk/apps/Thaw/src/thaw/plugins/webOfTrust/WebOfTrustConfigTab.java        
2008-02-12 18:35:06 UTC (rev 17842)
@@ -14,6 +14,7 @@

 import thaw.core.Config;
 import thaw.core.I18n;
+import thaw.core.Logger;
 import thaw.plugins.Hsqldb;
 import thaw.plugins.signatures.Identity;

@@ -68,7 +69,7 @@

                for (Iterator it = identities.iterator();
                        it.hasNext();) {
-                       identityUsed.addItem(it.next().toString());
+                       identityUsed.addItem(it.next());
                }
        }

@@ -79,21 +80,37 @@
                
identityUsed.setSelectedItem(I18n.getMessage("thaw.plugin.wot.usedIdentity.none"));

                /* loading values */
-               if (config.getValue("wotActivated") != null)
-                       
activated.setSelected(Boolean.valueOf(config.getValue("wotActivated")).booleanValue());
-               if (config.getValue("wotIdentityUsed") != null)
-                       
identityUsed.setSelectedItem(config.getValue("wotIdentityUsed"));
-               if (config.getValue("wotNumberOfRefresh") != null)
-                       numberOfRefresh.setSelectedItem(new 
Integer(config.getValue("wotNumberOfRefresh")));
+               try {
+                       if (config.getValue("wotActivated") != null)
+                               
activated.setSelected(Boolean.valueOf(config.getValue("wotActivated")).booleanValue());
+                       if (config.getValue("wotIdentityUsed") != null)
+                               
identityUsed.setSelectedItem(Identity.getIdentity(db, 
Integer.parseInt(config.getValue("wotIdentityUsed"))));
+                       if (config.getValue("wotNumberOfRefresh") != null)
+                               numberOfRefresh.setSelectedItem(new 
Integer(config.getValue("wotNumberOfRefresh")));
+               } catch(Exception e) {
+                       Logger.warning(this, "Error while reading config: 
"+e.toString());
+               }
        }

        protected void saveSettings() {
                config.setValue("wotActivated", 
Boolean.valueOf(activated.isSelected()).toString());

-               if (identityUsed.getSelectedIndex() <= 0)
-                       config.setValue("wotIdentityUsed", null);
-               else
-                       config.setValue("wotIdentityUsed", 
identityUsed.getSelectedItem().toString());
+               String prevIdentity = config.getValue("wotIdentityUsed");
+               String newIdentity = null;
+               
+               if (identityUsed.getSelectedIndex() > 0)
+                       newIdentity = 
Integer.toString(((Identity)identityUsed.getSelectedItem()).getId());
+               
+               config.setValue("wotIdentityUsed", newIdentity);
+               
+               if ( (prevIdentity != null && newIdentity == null)
+                               || (prevIdentity == null && newIdentity != null)
+                               || (!prevIdentity.equals(newIdentity)) ) {
+                       /* to force the regeneration of the keys */
+                       config.setValue("wotPrivateKey", null);
+                       config.setValue("wotPublicKey", null);
+               }
+               

                config.setValue("wotNumberOfRefresh", 
numberOfRefresh.getSelectedItem().toString());
        }


Reply via email to