Author: jflesch
Date: 2006-10-22 18:51:51 +0000 (Sun, 22 Oct 2006)
New Revision: 10687

Added:
   trunk/apps/Thaw/images/index-existing.png
Modified:
   trunk/apps/Thaw/src/thaw/core/IconBox.java
   trunk/apps/Thaw/src/thaw/core/MainWindow.java
   trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java
   trunk/apps/Thaw/src/thaw/fcp/FCPQueueLoader.java
   trunk/apps/Thaw/src/thaw/fcp/FCPQueueManager.java
   trunk/apps/Thaw/src/thaw/i18n/thaw.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
   trunk/apps/Thaw/src/thaw/plugins/FetchPlugin.java
   trunk/apps/Thaw/src/thaw/plugins/index/File.java
   trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java
   trunk/apps/Thaw/src/thaw/plugins/index/Index.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexSelecter.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexTreeNode.java
   trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java
Log:
It's now possible to reimport indexes from freenet

Added: trunk/apps/Thaw/images/index-existing.png
===================================================================
(Binary files differ)


Property changes on: trunk/apps/Thaw/images/index-existing.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: trunk/apps/Thaw/src/thaw/core/IconBox.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/IconBox.java  2006-10-22 18:27:14 UTC (rev 
10686)
+++ trunk/apps/Thaw/src/thaw/core/IconBox.java  2006-10-22 18:51:51 UTC (rev 
10687)
@@ -39,7 +39,9 @@
        public static ImageIcon makeALinkAction;

        public static ImageIcon minIndex;
+
        public static ImageIcon indexNew;
+       public static ImageIcon indexReuse;

        public static ImageIcon refreshAction;

@@ -96,9 +98,14 @@

                        minIndex =
                                new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("index.png"));
+
                        indexNew =
                                new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("index-new.png"));

+                       indexReuse = 
+                               new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("index-existing.png"));
+
+
                        downloads =
                                new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("go-first.png"));
                        minDownloads =
@@ -138,7 +145,7 @@
                                new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("min-view-refresh.png"));

                        refreshAction =
-                               new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("index-refresh.png"));
+                               new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("view-refresh.png"));

                        quitAction =
                                new ImageIcon((new 
IconBox()).getClass().getClassLoader().getResource("system-log-out.png"));

Modified: trunk/apps/Thaw/src/thaw/core/MainWindow.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/MainWindow.java       2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/core/MainWindow.java       2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -391,7 +391,7 @@
        }

        public void windowClosed(WindowEvent e) {
-               // add potential warnings here
+               // gni
        }

        public void windowDeactivated(WindowEvent e) {

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java      2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPClientPut.java      2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -85,9 +85,15 @@
                            boolean getCHKOnly) {
                this.getCHKOnly = getCHKOnly;
                this.localFile = file;
-               this.name = file.getName();
-               fileSize = file.length();

+               if (file != null) {
+                       this.name = file.getName();
+                       fileSize = file.length();
+               } else {
+                       this.name = name;
+                       fileSize = 0;
+               } 
+
                this.keyType = keyType;
                this.rev = rev;

@@ -334,7 +340,10 @@

                status = "Sending to the node";

-               identifier = queueManager.getAnID() + "-"+ localFile.getName();
+               if (localFile != null)
+                       identifier = queueManager.getAnID() + "-"+ 
localFile.getName();
+               else
+                       identifier = queueManager.getAnID();

                setChanged();
                notifyObservers();
@@ -370,8 +379,10 @@
                        msg.setValue("Global", "true");
                else
                        msg.setValue("Global", "false");
-               msg.setValue("ClientToken", localFile.getPath());

+               if (localFile != null)
+                       msg.setValue("ClientToken", localFile.getPath());
+
                switch(persistence) {
                case(0): msg.setValue("Persistence", "forever"); break;
                case(1): msg.setValue("Persistence", "reboot"); break;
@@ -379,12 +390,15 @@
                default: Logger.notice(this, "Unknow persistence !?"); break;
                }

-               msg.setValue("TargetFilename", localFile.getName());
+               if (localFile != null)
+                       msg.setValue("TargetFilename", localFile.getName());
+               else
+                       msg.setValue("TargetFilename", name);

                msg.setValue("UploadFrom", "direct");

-               msg.setAmountOfDataWaiting(localFile.length());
-               Logger.info(this, "Sending "+(new Long(localFile.length()))+" 
bytes on socket ...");
+               msg.setAmountOfDataWaiting(fileSize);
+               Logger.info(this, "Sending "+Long.toString(fileSize)+" bytes on 
socket ...");

                queueManager.getQueryManager().writeMessage(msg, false);

@@ -432,11 +446,16 @@
        private boolean sendFile() {
                FCPConnection connection = 
queueManager.getQueryManager().getConnection();

-               long remaining = localFile.length();
+               long remaining = fileSize;
                byte[] data = null;

                FileInputStream in = null;

+               if (localFile == null) {
+                       toTheNodeProgress = 100;
+                       return true;
+               }
+
                try {
                        in = new FileInputStream(localFile);
                } catch(java.io.FileNotFoundException e) {

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPQueueLoader.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPQueueLoader.java    2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPQueueLoader.java    2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -131,7 +131,7 @@
                if(msg.getMessageName().equals("EndListPersistentRequests")) {
                        Logger.info(this, "End Of ListPersistentRequests.");
                        
queueManager.getQueryManager().getConnection().unlockWriting();
-
+                       queueManager.setQueueCompleted();
                        return;
                }
        }

Modified: trunk/apps/Thaw/src/thaw/fcp/FCPQueueManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/fcp/FCPQueueManager.java   2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/fcp/FCPQueueManager.java   2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -28,6 +28,7 @@
        private int lastId;
        private String thawId;

+       private boolean queueCompleted;

        /**
         * Calls setQueryManager() and then resetQueues().
@@ -36,6 +37,8 @@
                               String thawId,
                               int maxDownloads, int maxInsertions) {
                lastId = 0;
+               queueCompleted = false;
+
                setThawId(thawId);
                setMaxDownloads(maxDownloads);
                setMaxInsertions(maxInsertions);
@@ -46,6 +49,14 @@
                queryManager.getConnection().addObserver(this);
        }

+       public boolean isQueueCompletlyLoaded() {
+               return queueCompleted;
+       }
+
+       public void setQueueCompleted() {
+               queueCompleted = true;
+       }
+
        /**
         * Use it if you want to bypass the queue.
         */
@@ -277,6 +288,9 @@

                Iterator it;

+               if (key == null)
+                       return null;
+
                while(interrupted) {
                        interrupted = false;

@@ -313,6 +327,54 @@


        /**
+        * Not reliable
+        */
+       public FCPTransferQuery getTransferByFilename(String name) {
+               boolean interrupted=true;
+
+               Iterator it;
+
+               if (name == null)
+                       return null;
+
+               while(interrupted) {
+                       interrupted = false;
+
+                       try {
+                               for(it = runningQueries.iterator();
+                                   it.hasNext(); )
+                                       {
+                                               FCPTransferQuery plop = 
(FCPTransferQuery)it.next();
+
+                                               if (plop.getFilename() == name
+                                                   || 
name.equals(plop.getFilename()))
+                                                       return plop;
+                                       }
+                               
+                               for(int i = 0 ; i <= PRIORITY_MIN ; i++) {
+                                       for(it = pendingQueries[i].iterator();
+                                           it.hasNext(); )
+                                               {
+                                                       FCPTransferQuery plop = 
(FCPTransferQuery)it.next();
+
+                                                       if (plop.getFilename() 
== name
+                                                           || 
name.equals(plop.getFilename()))
+                                                               return plop;
+                                               }
+                                       
+                               }
+                       } catch(java.util.ConcurrentModificationException e) {
+                               Logger.notice(this, "getTransferByFilename(): 
Collission. Reitering");
+                               interrupted = true;
+                       }
+
+               }
+
+               return null;
+       }
+
+
+       /**
         * Compare using the key.
         */
        public boolean isAlreadyPresent(FCPTransferQuery query) {

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -195,7 +195,7 @@

 thaw.plugin.index.category=Category

-thaw.plugin.index.editor=Your shared files
+thaw.plugin.index.editor=Index editor
 thaw.plugin.index.browser=Index browser

 thaw.plugin.index.yourIndexes=Your indexes
@@ -211,7 +211,10 @@
 thaw.plugin.index.insertIndexes=[Re]insert these indexes
 thaw.plugin.index.downloadIndex=[Re]download this index
 thaw.plugin.index.downloadIndexes=[Re]download these indexes
-thaw.plugin.index.copyKey=Copy index key(s) to clipboard
+thaw.plugin.index.copyKey=Copy index key to clipboard
+thaw.plugin.index.copyKeys=Copy index keys to clipboard
+thaw.plugin.index.copyPrivateKey=Copy index private key(s) to clipboard
+thaw.plugin.index.reloadFromFreenet=Reload index from freenet copy
 thaw.plugin.index.recalculateKeys=Recalculate keys

 thaw.plugin.index.categoryName=Folder Name ?
@@ -220,7 +223,10 @@
 thaw.plugin.index.newCategory=New folder
 thaw.plugin.index.newIndex=New index

+thaw.plugin.index.addAlreadyExisting=Add an already existing index
+
 thaw.plugin.index.indexKey=Index key:
+thaw.plugin.index.indexPrivateKey=Index private key:

 thaw.plugin.index.search.label=Search:
 thaw.plugin.index.search.apply=Search

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -202,7 +202,8 @@
 thaw.plugin.index.insertIndexes=Ins?rer / mettre ? jour ces indexes
 thaw.plugin.index.downloadIndex=T?l?charger / mettre ? jour cet index
 thaw.plugin.index.downloadIndexes=T?l?charger / mettre ? jour ces indexes
-thaw.plugin.index.copyKey=Copier les cl?s de l'index dans le presse-papiers
+thaw.plugin.index.copyKey=Copier la cl? de l'index dans le presse-papiers
+thaw.plugin.index.copyKeys=Copier les cl?s des indexes dans le presse-papiers
 thaw.plugin.index.recalculateKeys=Recalculer les cl?s

 thaw.plugin.index.categoryName=Nom du r?pertoire ?
@@ -211,7 +212,7 @@
 thaw.plugin.index.newCategory=Nouveau r?pertoire
 thaw.plugin.index.newIndex=Nouvel index

-thaw.plugin.index.indexKey=Cl? de l'index ?
+thaw.plugin.index.indexKey=Cl? de l'index:

 thaw.plugin.index.search.label=Rechercher dans ce r?pertoire / index
 thaw.plugin.index.search.apply=Rechercher

Modified: trunk/apps/Thaw/src/thaw/plugins/FetchPlugin.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/FetchPlugin.java   2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/FetchPlugin.java   2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -56,14 +56,7 @@

                        String[] subKey = keys[i].split("\\?"); /* Because of 
VolodyA :p */

-                       String key = 
subKey[0].replaceFirst("http://127.0.0.1:8888/";, "");
-                       key = key.replaceFirst("http://localhost/";, "");
-                       
-                       try {
-                               key = java.net.URLDecoder.decode(key, "UTF-8");
-                       } catch (java.io.UnsupportedEncodingException e) {
-                               Logger.warning(this, 
"UnsupportedEncodingException (UTF-8): "+e.toString());
-                       }
+                       String key = FreenetURIHelper.cleanURI(subKey[0]);

                        core.getQueueManager().addQueryToThePendingQueue(new 
FCPClientGet(key,
                                                                                
          priority,

Modified: trunk/apps/Thaw/src/thaw/plugins/index/File.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/File.java    2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/File.java    2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -11,6 +11,8 @@
 import java.util.Iterator;

 import thaw.core.Logger;
+import thaw.core.FreenetURIHelper;
+
 import thaw.fcp.*;
 import thaw.plugins.Hsqldb;

@@ -33,6 +35,8 @@

        private Hsqldb db;

+       private FCPQueueManager queueManager = null;
+
        /**
         * @param path Local path
         * @param transfer Corresponding tranfer (can be null).
@@ -93,14 +97,7 @@
        }

        public void deduceFilenameFromKey() {
-               if(publicKey.indexOf("/") < 0) {
-                       fileName = publicKey;
-                       publicKey = null;
-                       return;
-               }
-
-               String[] keyParts = publicKey.split("/");
-               fileName = keyParts[keyParts.length-1];
+               fileName = FreenetURIHelper.getFilenameFromKey(publicKey);
        }

        public void setOptions(NodeList list) {
@@ -183,10 +180,49 @@
                notifyObservers(query);
        }

+
+       public void recalculateCHK(FCPQueueManager queueManager) {
+               this.queueManager = queueManager;
+
+               FCPClientPut insertion = new FCPClientPut(new 
java.io.File(getLocalPath()), 0, 0, null,
+                                                         null, 4,
+                                                         true, 2, true); /* 
getCHKOnly */
+               queueManager.addQueryToThePendingQueue(insertion);
+               
+               setTransfer(insertion);
+       }
+
+       
+       public void download(String targetPath, FCPQueueManager queueManager) {
+               FCPClientGet clientGet = new FCPClientGet(getPublicKey(), 4, 0, 
true, -1, targetPath);
+
+               queueManager.addQueryToThePendingQueue(clientGet);
+               
+               setTransfer(clientGet);
+       }
+
+
+       public void insertOnFreenet(FCPQueueManager queueManager) {
+               FCPClientPut clientPut = new FCPClientPut(new 
java.io.File(getLocalPath()),
+                                                         0, 0, null, null, 4, 
true, 0);
+               queueManager.addQueryToThePendingQueue(clientPut);
+                               
+               setTransfer(clientPut);
+       }
+
+
        /* Try to find its download automagically */
        public void setTransfer(FCPQueueManager queueManager) {
-               if (publicKey != null) {
-                       setTransfer(queueManager.getTransfer(publicKey));
+               if (publicKey != null || fileName != null) {
+                       FCPTransferQuery trans;
+
+                       trans = queueManager.getTransfer(publicKey);
+
+                       if (trans == null) {
+                               trans = 
queueManager.getTransferByFilename(fileName);
+                       }
+
+                       setTransfer(trans);
                }

                setChanged();
@@ -347,6 +383,11 @@
        public void update(java.util.Observable o, Object param) {
                if(o == transfer) {
                        if(transfer.isFinished() && transfer instanceof 
FCPClientPut) {
+                               if (queueManager != null) {
+                                       queueManager.remove(transfer);
+                                       queueManager = null;
+                               }
+
                                ((FCPClientPut)transfer).deleteObserver(this);
                                setPublicKey(transfer.getFileKey());
                                update();

Modified: trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java       2006-10-22 
18:27:14 UTC (rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/FileTable.java       2006-10-22 
18:51:51 UTC (rev 10687)
@@ -218,7 +218,7 @@
                        row = parent.getFilePosition(file);

                        if (row < 0)
-                               Logger.notice(this, "File not found in the 
index ?! Index : "+parent.getKey()+" ; File: " +file.getPublicKey());
+                               Logger.notice(this, "File not found in the 
index ?! Index : "+parent.getPublicKey()+" ; File: " +file.getPublicKey());
                        else
                                setSelectedRows(row, row);

@@ -262,12 +262,7 @@
                                Index index = (Index)fileList;

                                thaw.plugins.index.File file = 
index.getFile(selectedRows[i]);
-                               
-                               FCPClientPut clientPut = new FCPClientPut(new 
java.io.File(file.getLocalPath()),
-                                                                         0, 0, 
null, null, 4, true, 0);
-                               
queueManager.addQueryToThePendingQueue(clientPut);
-                               
-                               file.setTransfer(clientPut);
+                               file.insertOnFreenet(queueManager);
                        }


@@ -278,13 +273,7 @@
                                        Logger.notice(this, "File disappeared 
?");
                                        continue;
                                }                                       
-
-                               FCPClientGet clientGet = new 
FCPClientGet(file.getPublicKey(), 4, 0, true, -1,
-                                                                         
destination.getPath());
-
-                               
queueManager.addQueryToThePendingQueue(clientGet);
-                               
-                               file.setTransfer(clientGet);
+                               file.download(destination.getPath(), 
queueManager);
                        }

                        if(e.getSource() == copyFileKeys) {
@@ -296,12 +285,7 @@
                        if(e.getSource() == recalculateKeys) {
                                thaw.plugins.index.File file = 
fileList.getFile(selectedRows[i]);

-                               FCPClientPut insertion = new FCPClientPut(new 
java.io.File(file.getLocalPath()), 0, 0, null,
-                                                                         null, 
4,
-                                                                         true, 
2, true); /* getCHKOnly */
-                               insertion.start(queueManager);
-
-                               file.setTransfer(insertion);
+                               file.recalculateCHK(queueManager);
                        }

                }

Modified: trunk/apps/Thaw/src/thaw/plugins/index/Index.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2006-10-22 18:27:14 UTC 
(rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2006-10-22 18:51:51 UTC 
(rev 10687)
@@ -63,7 +63,12 @@

        private String author = null;

+       private boolean rewriteKey = true;
+       

+       private FCPClientPut publicKeyRecalculation = null;
+
+
        /**
         * The bigest constructor of the world ...
         */
@@ -84,17 +89,24 @@
                this.parent = parent;
                this.realName = realName;
                this.displayName = displayName;
-               this.modifiable = modifiable;
+               this.modifiable = (privateKey == null ? false : true);

+               this.privateKey = privateKey;
                this.publicKey = publicKey;

-               this.privateKey = privateKey;

+               if (modifiable == true && publicKey != null && 
publicKey.startsWith("USK@")) {
+                       String[] split = 
FreenetURIHelper.convertUSKtoSSK(publicKey).split("/");
+                       publicKey = split[0];
+               }
+
                this.revision = revision;

                this.author = author;

                treeNode.setUserObject(this);
+
+               setTransfer();
        }

        public void setParent(IndexCategory parent) {
@@ -222,24 +234,6 @@
        }


-       protected String changeRevision(String key, int move) {
-               try {
-                       String newKey;
-                       String[] split = key.split("/");
-
-                       newKey = split[0] + "/" + split[1] + "/"
-                               + 
Integer.toString(Integer.parseInt(split[2])+move) + "/"
-                               + split[3];
-                       
-                       return newKey;
-               } catch (Exception e) {
-                       Logger.warning(this, "Unable to add a revision to the 
key '"+key+"' because : "+e.toString());
-               }
-
-               return key;
-       }
-
-
        public void update() {
                targetFile = new java.io.File(toString()+".xml");

@@ -270,42 +264,115 @@
                        } else {
                                Logger.warning(this, "Index not generated !");
                        }
+
+                       setChanged();
+                       notifyObservers();
                } else {
-                       FCPClientGet clientGet;
-                       
-                       Logger.info(this, "Getting lastest version ...");
+                       updateFromFreenet(-1);
+               }
+               
+       }

-                       String key;
+       public void updateFromFreenet(int rev) {
+               FCPClientGet clientGet;
+               
+               Logger.info(this, "Getting lastest version ...");
+               
+               String key;
+               
+               /* We will trust the node for the incrementation
+                  execept if a rev is specified */

-                       /* We will trust the node for the incrementation */
-                       /*
-                         if (!isEmpty())
-                            key = changeRevision(publicKey, 1);
-                         else
-                            key = publicKey;
-                       */
+               
+               if (rev >= 0) {
+                       key = FreenetURIHelper.convertUSKtoSSK(publicKey);
+                       key = FreenetURIHelper.changeSSKRevision(key, rev, 0);
+                       rewriteKey = false;
+               } else {
+                       if (!modifiable) {
+                               key = publicKey;
+                               rewriteKey = true;
+                       } else {
+                               key = getPublicKey();
+                               rewriteKey = false;
+                       }
+               }
+               
+               Logger.info(this, "Key asked: "+key);
+               
+               clientGet = new FCPClientGet(key, 4, 2, false, 1, 
System.getProperty("java.io.tmpdir"));
+               transfer = clientGet;
+               clientGet.addObserver(this);
+               
+               queueManager.addQueryToThePendingQueue(clientGet);

-                       key = publicKey;
+               setChanged();
+               notifyObservers();
+       }

+       public boolean isUpdating() {
+               return (transfer != null && (!transfer.isFinished()));
+       }

-                       Logger.info(this, "Key asked: "+key);

-                       clientGet = new FCPClientGet(key, 4, 2, false, 1, 
System.getProperty("java.io.tmpdir"));
-                       transfer = clientGet;
-                       clientGet.addObserver(this);
+       protected void setTransfer(FCPTransferQuery query) {
+               transfer = query;

-                       queueManager.addQueryToThePendingQueue(clientGet);
+               if (transfer != null) {
+                       update(((java.util.Observable)transfer), null);
                }
+       }

+       protected void setTransfer() {
+               if (queueManager == null)
+                       return;
+
+               if (getPublicKey() != null) {
+                       if(!queueManager.isQueueCompletlyLoaded()) {
+                               Thread th = new Thread(new WaitCompleteQueue());
+                               th.start();
+                               return;
+                       }
+                       
+                       String key;
+
+                       if (modifiable)
+                               key = 
FreenetURIHelper.getPublicInsertionSSK(getPublicKey());
+                       else
+                               key = getPublicKey();
+
+                       Logger.notice(this, "Seeking "+key);
+                       setTransfer(queueManager.getTransfer(key));
+               }
+
                setChanged();
                notifyObservers();
-               
        }

-       public boolean isUpdating() {
-               return (transfer != null && (!transfer.isFinished()));
+       private class WaitCompleteQueue implements Runnable {
+               public WaitCompleteQueue() { }
+
+               public void run() {
+                       int tryNumber = 0;
+
+                       while(!queueManager.isQueueCompletlyLoaded() && 
tryNumber < 120 /* 1min */) {
+                               try {
+                                       Thread.sleep(500);
+                               } catch(java.lang.InterruptedException e) {
+                                       /* \_o< */
+                               }
+
+                               tryNumber++;
+                       }
+
+                       if (tryNumber == 120)
+                               return;
+
+                       setTransfer();
+               }
        }

+
        public void purgeLinkList() {
                try {
                        Connection c = db.getConnection();
@@ -365,13 +432,23 @@
                return id;
        }

-       public String getKey() {
-               if(modifiable)
+       public String getPublicKey() {
+               if (publicKey == null)
+                       return publicKey;
+
+               if(publicKey.startsWith("SSK@")) { /* as it should when 
modifiable == true */
                        return publicKey.replaceFirst("SSK@", 
"USK@")+realName+"/"+revision+"/"+realName+".xml";
-               else
+               } else
                        return publicKey;
        }

+       public String getPrivateKey() {
+               if (!modifiable)
+                       return null;
+
+               return privateKey;
+       }
+
        public String toString() {
                if(displayName != null)
                        return displayName;
@@ -382,7 +459,9 @@
                return true;
        }

+
        public void update(java.util.Observable o, Object p) {
+
                if(o == sskGenerator) {
                        sskGenerator.deleteObserver(this);
                        publicKey = sskGenerator.getPublicKey();
@@ -414,7 +493,8 @@

                                        Logger.info(this, "Updating index ...");

-                                       publicKey = transfer.getFileKey();
+                                       if (rewriteKey)
+                                               publicKey = 
transfer.getFileKey();

                                        Logger.info(this, "Most up-to-date key 
found: " + publicKey);

@@ -975,14 +1055,14 @@

        static String getNameFromKey(String key) {
                String name = null;
-               try {
-                       String[] cutcut = key.split("/");
-                       name = cutcut[cutcut.length-1];
-                       name = name.replaceAll(".xml", "");
-               } catch (Exception e) {
-                       Logger.warning(e, "thaw.plugins.index.Index: Error 
while parsing index key: "+key+" because: "+e.toString() );
-                       name = key;
-               }
+
+               name = FreenetURIHelper.getFilenameFromKey(key);
+
+               if (name == null)
+                       return null;
+
+               name = name.replaceAll(".xml", "");
+
                return name;
        }


Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java   2006-10-22 
18:27:14 UTC (rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexCategory.java   2006-10-22 
18:51:51 UTC (rev 10687)
@@ -107,7 +107,7 @@

                for(Iterator it = children.iterator();
                    it.hasNext();) {
-                       IndexTreeNode child = (IndexTreeNode)it.next();
+                       IndexTreeNode child = 
(IndexTreeNode)((DefaultMutableTreeNode)it.next()).getUserObject();
                        child.delete();
                }

@@ -143,18 +143,30 @@
                }
        }

-       public String getKey() {
+       public String getPublicKey() {
                String result = "";

                for(Iterator it = children.iterator();
                    it.hasNext();) {
                        IndexTreeNode node = 
(IndexTreeNode)((DefaultMutableTreeNode)it.next()).getUserObject();
-                       result = result + node.getKey() + "\n";
+                       result = result + node.getPublicKey() + "\n";
                }

                return result;
        }

+       public String getPrivateKey() {
+               String result = "";
+               
+               for(Iterator it = children.iterator();
+                   it.hasNext();) {
+                       IndexTreeNode node = 
(IndexTreeNode)((DefaultMutableTreeNode)it.next()).getUserObject();
+                       result = result + node.getPrivateKey() + "\n";
+               }
+               
+               return result;
+       }
+
        public void addObserver(java.util.Observer o) {
                if(children == null)
                        children = loadChildren();
@@ -172,6 +184,12 @@
                }
        }

+       public void updateFromFreenet(int rev) {
+               for (Enumeration e = children() ; e.hasMoreElements() ;) {
+                       
((IndexTreeNode)((DefaultMutableTreeNode)e.nextElement()).getUserObject()).updateFromFreenet(rev);
+               }
+       }
+
        public boolean isUpdating() {
                for (Enumeration e = children() ; e.hasMoreElements() ;) {
                        
if(((IndexTreeNode)((DefaultMutableTreeNode)e.nextElement())).isUpdating())
@@ -284,11 +302,13 @@

                                String author = result.getString("author");

-                               set(children, position, (new Index(db, 
queueManager, id, this,
-                                                                  realName, 
displayName,
-                                                                  publicKey, 
privateKey, revision,
-                                                                  author,
-                                                                  
modifiables)).getTreeNode());
+                               Index index = new Index(db, queueManager, id, 
this,
+                                                       realName, displayName,
+                                                       publicKey, privateKey, 
revision,
+                                                       author,
+                                                       modifiables);
+
+                               set(children, position, index.getTreeNode());
                        }
                } catch (java.sql.SQLException e) {
                        Logger.warning(this, "SQLException while getting child 
of index category '"+name+"': "+e.toString());

Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexSelecter.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexSelecter.java   2006-10-22 
18:27:14 UTC (rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexSelecter.java   2006-10-22 
18:51:51 UTC (rev 10687)
@@ -141,7 +141,7 @@
        public void update(java.util.Observable o, Object param) {
                if (param instanceof Index) {
                        Index index = (Index)param;
-                       selectedIndexKey = index.getKey();
+                       selectedIndexKey = index.getPublicKey();

                        Logger.info(this, "Selected index key: 
"+selectedIndexKey);
                        keyField.setText(selectedIndexKey);

Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java       2006-10-22 
18:27:14 UTC (rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexTree.java       2006-10-22 
18:51:51 UTC (rev 10687)
@@ -2,6 +2,7 @@

 import javax.swing.JPanel;
 import java.awt.BorderLayout;
+import java.awt.GridLayout;
 import javax.swing.JTree;
 import javax.swing.tree.TreeSelectionModel;
 import javax.swing.tree.TreeNode;
@@ -38,10 +39,13 @@

 import java.awt.Color;

+import javax.swing.JFrame;

 import javax.swing.JToolBar;
 import javax.swing.JButton;

+import javax.swing.JTextField;
+import javax.swing.JLabel;

 import thaw.plugins.Hsqldb;
 import thaw.core.*;
@@ -62,8 +66,10 @@

        private JToolBar toolBar;
        private JButton newIndex;
+       private JButton reuseIndex;
        private JButton refreshAll;

+
        private JPopupMenu indexCategoryMenu;
        private JPopupMenu indexMenu;

@@ -75,8 +81,12 @@
        private JMenuItem deleteIndex;
        private JMenuItem updateIndexCategory;
        private JMenuItem updateIndex;
-       private JMenuItem copyKeys;
-       private JMenuItem copyKey;
+       private JMenuItem copyPublicKeys;
+       private JMenuItem copyPublicKey;
+       private JMenuItem copyPrivateKeys;
+       private JMenuItem copyPrivateKey;
+       private JMenuItem reloadFromFreenet;
+       

        private boolean modifiables;
        private boolean selectionOnly;
@@ -88,6 +98,13 @@
        private Hsqldb db;
        private FCPQueueManager queueManager;

+
+       /** used for a special form ***/
+       private int formState;
+       private JButton okButton = null;
+       private JButton cancelButton = null;
+       
+
        /**
         * Menu is defined according to the 'modifiables' parameters.
         * @param modifiables If set to true, then only indexes having private 
keys will
@@ -131,21 +148,38 @@
                        updateIndexCategory = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.downloadIndexes"));
                }

-               copyKey = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.copyKey"));
-               copyKeys = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.copyKey"));
+               copyPublicKey = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.copyKey"));
+               copyPublicKeys = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.copyKeys"));

+               if (modifiables) {
+                       copyPrivateKey = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.copyPrivateKey"));
+                       copyPrivateKeys = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.copyPrivateKey"));
+                       copyPrivateKey.addActionListener(this);
+                       copyPrivateKeys.addActionListener(this);
+
+                       reloadFromFreenet = new 
JMenuItem(I18n.getMessage("thaw.plugin.index.reloadFromFreenet"));
+                       reloadFromFreenet.addActionListener(this);
+               }
+
                indexCategoryMenu.add(updateIndexCategory);
                indexCategoryMenu.add(addIndex);
                indexCategoryMenu.add(addIndexCategory);
-               indexCategoryMenu.add(copyKeys);
+               indexCategoryMenu.add(copyPublicKeys);
                indexCategoryMenu.add(updateIndex);
                indexCategoryMenu.add(renameIndexCategory);
                indexCategoryMenu.add(deleteIndexCategory);
+               if (modifiables) {
+                       indexCategoryMenu.add(copyPrivateKeys);
+               }

                indexMenu.add(updateIndex);
-               indexMenu.add(copyKey);
+               indexMenu.add(copyPublicKey);
                indexMenu.add(renameIndex);
                indexMenu.add(deleteIndex);
+               if (modifiables) {
+                       indexMenu.add(copyPrivateKey);
+                       indexMenu.add(reloadFromFreenet);
+               }

                addIndex.addActionListener(this);
                addIndexCategory.addActionListener(this);
@@ -154,8 +188,8 @@
                updateIndexCategory.addActionListener(this);

                updateIndex.addActionListener(this);
-               copyKey.addActionListener(this);
-               copyKeys.addActionListener(this);
+               copyPublicKey.addActionListener(this);
+               copyPublicKeys.addActionListener(this);
                renameIndex.addActionListener(this);
                deleteIndex.addActionListener(this);

@@ -183,8 +217,6 @@
                if (selectionOnly)
                        
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

-
-
                toolBar = new JToolBar();

                newIndex   = new JButton(IconBox.indexNew);
@@ -199,12 +231,18 @@
                        refreshAll = new JButton(IconBox.refreshAction);
                        
refreshAll.setToolTipText(I18n.getMessage("thaw.plugin.index.downloadIndexes"));
                        refreshAll.addActionListener(this);
+               } else {
+                       reuseIndex = new JButton(IconBox.indexReuse);
+                       
reuseIndex.setToolTipText(I18n.getMessage("thaw.plugin.index.addAlreadyExisting"));
+                       reuseIndex.addActionListener(this);
                }


                toolBar.add(newIndex);
                if (!modifiables)
                        toolBar.add(refreshAll);
+               else
+                       toolBar.add(reuseIndex);

                if (!selectionOnly)
                        panel.add(toolBar, BorderLayout.NORTH);
@@ -303,6 +341,7 @@
                   || e.getSource() == newIndex) {
                        String name = null;

+                       String keys[] = null;
                        String publicKey = null;

                        if(!modifiables) {
@@ -311,27 +350,21 @@
                                if (publicKey == null)
                                        return;

-                               publicKey = 
publicKey.replaceFirst("http://127.0.0.1/";, "");
-                               publicKey = 
publicKey.replaceFirst("http://localhost/";, "");
+                               publicKey = 
FreenetURIHelper.cleanURI(publicKey);

-                               try {
-                                       publicKey = 
java.net.URLDecoder.decode(publicKey, "UTF-8");
-                               } catch(java.io.UnsupportedEncodingException 
exc) {
-                                       Logger.warning(this, 
"UnsupportedEncodingException (UTF-8): "+exc.toString());
-                               }
-
                                name = Index.getNameFromKey(publicKey);

-                       } else
+                       } else {
                                name = 
askAName(I18n.getMessage("thaw.plugin.index.indexName"),
-                                               
I18n.getMessage("thaw.plugin.index.newIndex"));
+                                               
I18n.getMessage("thaw.plugin.index.newIndex"));                         
+                       }

                        if(name == null)
                                return;

                        IndexCategory parent;

-                       if (e.getSource() == addIndex)
+                       if (selectedNode != null)
                                parent = (IndexCategory)selectedNode;
                        else
                                parent = root;
@@ -347,6 +380,15 @@
                        treeModel.reload(parent);
                }

+
+               if (e.getSource() == reuseIndex) {
+                       Thread newThread = new Thread(new IndexReuser());
+
+                       newThread.start();
+               }
+
+
+
                if(e.getSource() == addIndexCategory) {
                        String name = 
askAName(I18n.getMessage("thaw.plugin.index.categoryName"), 
                                               
I18n.getMessage("thaw.plugin.index.newCategory"));
@@ -405,25 +447,144 @@
                   || e.getSource() == updateIndexCategory) {
                        selectedNode.update();
                }
+
+               if (e.getSource() == reloadFromFreenet) {
+                       selectedNode.updateFromFreenet(-1);
+               }

                if (e.getSource() == refreshAll) {
                        root.update();
                }

-               if(e.getSource() == copyKey
-                  || e.getSource() == copyKeys) {
+               if(e.getSource() == copyPublicKey
+                  || e.getSource() == copyPublicKeys) {
                        Toolkit tk = Toolkit.getDefaultToolkit();
-                       StringSelection st = new 
StringSelection(selectedNode.getKey());
+                       StringSelection st = new 
StringSelection(selectedNode.getPublicKey());
                        Clipboard cp = tk.getSystemClipboard();
                        cp.setContents(st, null);
                }

+               
+               if(e.getSource() == copyPrivateKey
+                  || e.getSource() == copyPrivateKeys) {
+                       Toolkit tk = Toolkit.getDefaultToolkit();
+                       StringSelection st = new 
StringSelection(selectedNode.getPrivateKey());
+                       Clipboard cp = tk.getSystemClipboard();
+                       cp.setContents(st, null);
+               }
+
+               if (e.getSource() == okButton) {
+                       formState = 1;
+               }
+
+               if (e.getSource() == cancelButton) {
+                       formState = 2;
+               }
        }

        public String askAName(String prompt, String defVal) {
                return JOptionPane.showInputDialog(prompt, defVal);
        }

+
+       /* It's gruiiicckk */
+       private class IndexReuser implements Runnable {
+               public IndexReuser() {
+
+               }
+
+               public void run() {
+                       String keys[];
+                       String publicKey = null;
+                       String privateKey = null;
+
+                       keys = askKeys(true);
+                       if (keys == null)
+                               return;
+
+                       publicKey = keys[0];
+                       privateKey = keys[1];
+
+                       try {
+                               publicKey = 
java.net.URLDecoder.decode(publicKey, "UTF-8");
+                       } catch(java.io.UnsupportedEncodingException exc) {
+                               Logger.warning(this, 
"UnsupportedEncodingException (UTF-8): "+exc.toString());
+                       }
+
+                       String name = Index.getNameFromKey(publicKey);
+                       
+                       IndexCategory parent;
+
+                       if (selectedNode != null && selectedNode instanceof 
IndexCategory)
+                               parent = (IndexCategory)selectedNode;
+                       else
+                               parent = root;
+
+                       Index index = new Index(db, queueManager, -2, parent, 
name, name, publicKey, privateKey, 0, null, modifiables);
+
+                       index.create();
+                       parent.insert(index.getTreeNode(), 0);
+                       
+                       treeModel.reload(parent);
+               }
+       }
+
+
+       public String[] askKeys(boolean askPrivateKey) {
+               //I18n.getMessage("thaw.plugin.index.indexKey")
+               formState = 0;
+
+               JFrame frame = new 
JFrame(I18n.getMessage("thaw.plugin.index.indexKey"));
+
+               frame.getContentPane().setLayout(new GridLayout(askPrivateKey ? 
3 : 2, 2));
+
+               JTextField publicKeyField = new JTextField("USK@");
+               JTextField privateKeyField = new JTextField("SSK@");
+
+               frame.getContentPane().add(new 
JLabel(I18n.getMessage("thaw.plugin.index.indexKey")));
+               frame.getContentPane().add(publicKeyField);
+
+               if (askPrivateKey) {
+                       frame.getContentPane().add(new 
JLabel(I18n.getMessage("thaw.plugin.index.indexPrivateKey")));
+                       frame.getContentPane().add(privateKeyField);
+               }
+
+               cancelButton = new 
JButton(I18n.getMessage("thaw.common.cancel"));
+               okButton = new JButton(I18n.getMessage("thaw.common.ok"));
+
+               cancelButton.addActionListener(this);
+               okButton.addActionListener(this);
+
+               frame.getContentPane().add(cancelButton);
+               frame.getContentPane().add(okButton);
+
+               frame.setSize(500, 100);
+               frame.setVisible(true);
+               
+               while(formState == 0) {
+                       try {
+                               Thread.sleep(500);
+                       } catch(InterruptedException e) {
+                               /* \_o< */
+                       }
+               }
+
+               frame.setVisible(false);
+
+               if (formState == 2)
+                       return null;
+
+               String[] keys = new String[2];
+
+               keys[0] = publicKeyField.getText();
+               if (askPrivateKey)
+                       keys[1] = privateKeyField.getText();
+               else
+                       keys[1] = null;
+
+               return keys;
+       }
+
        public void save() {
                root.save();
        }

Modified: trunk/apps/Thaw/src/thaw/plugins/index/IndexTreeNode.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexTreeNode.java   2006-10-22 
18:27:14 UTC (rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexTreeNode.java   2006-10-22 
18:51:51 UTC (rev 10687)
@@ -33,10 +33,16 @@
        public void delete();

        /**
-        * Update from freenet / Update the freenet version. (recursive)
+        * Update from freenet / Update the freenet version, depending of the 
index kind (recursive)
         */
        public void update();

+       /**
+        * Update from freenet using the given revision
+        * @param rev -1 means the lastest
+        */
+       public void updateFromFreenet(int rev);
+
        public boolean isUpdating();

        /**
@@ -45,11 +51,11 @@
        public void save();

        /**
-        * Get (public) key(s)
+        * Get key(s)
         */
-       public String getKey();
+       public String getPublicKey();
+       public String getPrivateKey();

-
        public Vector getIndexIds();

        public Index getIndex(int id);

Modified: trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java       2006-10-22 
18:27:14 UTC (rev 10686)
+++ trunk/apps/Thaw/src/thaw/plugins/index/LinkTable.java       2006-10-22 
18:51:51 UTC (rev 10687)
@@ -89,9 +89,11 @@
                addThisIndex.addActionListener(this);
                copyKey.addActionListener(this);

-               if (modifiables)
+               if (modifiables) {
                        rightClickMenu.add(removeLinks);
-               rightClickMenu.add(addThisIndex);
+                       rightClickMenu.add(addThisIndex);
+               }
+
                rightClickMenu.add(copyKey);

                table.addMouseListener(this);


Reply via email to